Sha256: 62def132615773ce43cede1c4755d61801580afef7dd204ffdae27f0c85553f6

Contents?: true

Size: 1.3 KB

Versions: 12

Compression:

Stored size: 1.3 KB

Contents

module Services
  module Hubspot
    module Webhooks
      class CreateOrActivateSubscription
        def initialize(app_id:, event_type:, property_name: nil)
          @app_id = app_id
          @event_type = event_type
          @property_name = property_name
        end

        def call
          begin
            request = subscription_create_request(event_type: @event_type, property_name: @property_name)
            ::Hubspot::Webhooks::SubscriptionsApi.new.create(
              @app_id,
              request
            )
          rescue => e
            existing_subscription_id = JSON.parse(JSON.parse(e.response_body)['context']['subscriptionIds'][0])[0]
            ::Hubspot::Webhooks::SubscriptionsApi.new.update(
              existing_subscription_id,
              @app_id,
              subscription_patch_request: subscription_patch_request
            )
          end
        end

        private

        def subscription_create_request(event_type:, property_name:)
          ::Hubspot::Webhooks::SubscriptionCreateRequest.new(
            active: true,
            event_type: event_type,
            property_name: property_name,
         )
        end

        def subscription_patch_request
          ::Hubspot::Webhooks::SubscriptionPatchRequest.new(enabled: true)
        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/create_or_activate_subscription.rb
hubspot-api-client-8.0.1 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-8.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-7.3.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-7.2.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-7.1.1 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-7.1.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-7.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-6.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-5.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-4.0.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb
hubspot-api-client-3.3.0 sample-apps/webhooks-contacts-app/app/lib/services/hubspot/webhooks/create_or_activate_subscription.rb