Sha256: fffc582122d60ebda8ac27f77a2314cd2e81d74441e895b81def26f83870ff02

Contents?: true

Size: 1.57 KB

Versions: 7

Compression:

Stored size: 1.57 KB

Contents

module ShopifyApp
  class WebhooksManager
    class CreationFailed < StandardError; end

    def self.queue(shop_name, token)
      ShopifyApp::WebhooksManagerJob.perform_later(shop_name: shop_name, token: token)
    end

    def initialize(shop_name, token)
      @shop_name, @token = shop_name, token
    end

    def recreate_webhooks!
      destroy_webhooks
      create_webhooks
    end

    def create_webhooks
      return unless required_webhooks.present?

      with_shopify_session do
        required_webhooks.each do |webhook|
          create_webhook(webhook) unless webhook_exists?(webhook[:topic])
        end
      end
    end

    def destroy_webhooks
      with_shopify_session do
        ShopifyAPI::Webhook.all.each do |webhook|
          ShopifyAPI::Webhook.delete(webhook.id) if is_required_webhook?(webhook)
        end
      end
      @current_webhooks = nil
    end

    private

    def required_webhooks
      ShopifyApp.configuration.webhooks
    end

    def is_required_webhook?(webhook)
      required_webhooks.map{ |w| w[:address] }.include? webhook.address
    end

    def with_shopify_session
      ShopifyAPI::Session.temp(@shop_name, @token) do
        yield
      end
    end

    def create_webhook(attributes)
      attributes.reverse_merge!(format: 'json')
      webhook = ShopifyAPI::Webhook.create(attributes)
      raise CreationFailed unless webhook.persisted?
      webhook
    end

    def webhook_exists?(topic)
      current_webhooks[topic]
    end

    def current_webhooks
      @current_webhooks ||= ShopifyAPI::Webhook.all.index_by(&:topic)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
shopify_app-7.0.2 lib/shopify_app/webhooks_manager.rb
shopify_app-7.0.1 lib/shopify_app/webhooks_manager.rb
shopify_app-7.0.0 lib/shopify_app/webhooks_manager.rb
shopify_app-6.4.2 lib/shopify_app/webhooks_manager.rb
shopify_app-6.4.1 lib/shopify_app/webhooks_manager.rb
shopify_app-6.4.0 lib/shopify_app/webhooks_manager.rb
shopify_app-6.3.0 lib/shopify_app/webhooks_manager.rb