Sha256: 9fb5d2d811429a8c4079249ef1eb78c4773e8845c6296416a7409a1fdf06f964

Contents?: true

Size: 1.22 KB

Versions: 12

Compression:

Stored size: 1.22 KB

Contents

module Services
  module Hubspot
    module Webhooks
      class Handle
        def initialize(webhook:, request:)
          @webhook = webhook
          @request = request
        end

        def call
          validate_signature
          create_event
        end

        private

        def create_event
          event = Event.new(
            event_type: @webhook['subscriptionType'].split('.').last,
            object_id: @webhook['objectId'],
            event_id: @webhook['eventId'],
            occured_at: @webhook['occurredAt']
          )

          if event.event_type == 'propertyChange'
            event.assign_attributes(
              property_name: @webhook['propertyName'],
              property_value: @webhook['propertyValue']
            )
          end
          event.save!
        end

        def validate_signature
          ::Hubspot::Helpers::WebhooksHelper.validate_signature(
            signature: @request.headers['X-HubSpot-Signature'],
            signature_version: @request.headers['X-HubSpot-Signature-Version'],
            http_uri: @request.base_url,
            request_body: @request.raw_post,
            client_secret: ENV["HUBSPOT_CLIENT_SECRET"]
          )
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
hubspot-api-client-9.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-8.0.1 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-8.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-7.3.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-7.2.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-7.1.1 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-7.1.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-7.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-6.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-5.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-4.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb
hubspot-api-client-3.3.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/handle.rb