Sha256: 1d1a00f70a909e41a1a5d5a74d33741283aa2977d22571c8197e7fb659f133a9

Contents?: true

Size: 1.78 KB

Versions: 7

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module ShopifyApp
  class WebhooksManager
    class CreationFailed < StandardError; end

    class << self
      def queue(shop_domain, shop_token)
        ShopifyApp::WebhooksManagerJob.perform_later(
          shop_domain: shop_domain,
          shop_token: shop_token
        )
      end

      def create_webhooks(session:)
        return unless ShopifyApp.configuration.has_webhooks?
        ShopifyAPI::Webhooks::Registry.register_all(session: session)
      end

      def recreate_webhooks!(session:)
        destroy_webhooks
        return unless ShopifyApp.configuration.has_webhooks?
        add_registrations
        ShopifyAPI::Webhooks::Registry.register_all(session: session)
      end

      def destroy_webhooks
        return unless ShopifyApp.configuration.has_webhooks?

        ShopifyApp.configuration.webhooks.each do |attributes|
          ShopifyAPI::Webhooks::Registry.unregister(topic: attributes[:topic])
        end
      end

      def add_registrations
        return unless ShopifyApp.configuration.has_webhooks?

        ShopifyApp.configuration.webhooks.each do |attributes|
          ShopifyAPI::Webhooks::Registry.add_registration(
            topic: attributes[:topic],
            delivery_method: attributes[:delivery_method] || :http,
            path: attributes[:address],
            handler: webhook_job_klass(attributes[:topic]),
            fields: attributes[:fields]
          )
        end
      end

      private

      def webhook_job_klass(topic)
        webhook_job_klass_name(topic).safe_constantize || raise(ShopifyApp::MissingWebhookJobError)
      end

      def webhook_job_klass_name(topic)
        [ShopifyApp.configuration.webhook_jobs_namespace, "#{topic.gsub("/", "_")}_job"].compact.join("/").classify
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
shopify_app-20.0.2 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-20.0.1 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-20.0.0 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-19.1.0 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-19.0.2 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-19.0.1 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-19.0.0 lib/shopify_app/managers/webhooks_manager.rb