Sha256: f74ea4dc4741b6dbb688e0606c4da486e4e068dd80f3e022ce589eb914556833

Contents?: true

Size: 1.48 KB

Versions: 47

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true
module ShopifyApp
  class WebhooksManager
    class CreationFailed < StandardError; end

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

    attr_reader :required_webhooks

    def initialize(webhooks)
      @required_webhooks = webhooks
    end

    def recreate_webhooks!
      destroy_webhooks
      create_webhooks
    end

    def create_webhooks
      return unless required_webhooks.present?

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

    def destroy_webhooks
      ShopifyAPI::Webhook.all.to_a.each do |webhook|
        ShopifyAPI::Webhook.delete(webhook.id) if required_webhook?(webhook)
      end

      @current_webhooks = nil
    end

    private

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

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

    def webhook_exists?(topic)
      current_webhooks[topic]
    end

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

Version data entries

47 entries across 47 versions & 2 rubygems

Version Path
ruby_shopify_app-1.3.3 lib/ruby_shopify_app/managers/webhooks_manager.rb
ruby_shopify_app-1.3.2 lib/ruby_shopify_app/managers/webhooks_manager.rb
ruby_shopify_app-1.3.1 lib/ruby_shopify_app/managers/webhooks_manager.rb
ruby_shopify_app-1.3.0 lib/ruby_shopify_app/managers/webhooks_manager.rb
ruby_shopify_app-1.2.0 lib/ruby_shopify_app/managers/webhooks_manager.rb
ruby_shopify_app-1.1.0 lib/ruby_shopify_app/managers/webhooks_manager.rb
ruby_shopify_app-1.0.0 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-18.1.3 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-18.1.2 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-18.1.1 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-18.1.0 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-18.0.4 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-18.0.3 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-18.0.2 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-18.0.1 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-18.0.0 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-17.2.1 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-17.2.0 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-17.1.1 lib/shopify_app/managers/webhooks_manager.rb
shopify_app-17.1.0 lib/shopify_app/managers/webhooks_manager.rb