Sha256: 9f3a8464c485cc8b8f6f590df86e072b7b4ab4fd9fa1a88eddcf1538f3d56519

Contents?: true

Size: 1.44 KB

Versions: 17

Compression:

Stored size: 1.44 KB

Contents

module DiscoApp::Concerns::SynchroniseWebhooksJob
  extend ActiveSupport::Concern

  # Ensure the webhooks registered with our shop are the same as those listed
  # in our application configuration.
  def perform(shop)
    # Get the full list of expected webhook topics.
    expected_topics = [:'app/uninstalled', :'shop/update'] + (DiscoApp.configuration.webhook_topics || [])

    # Registered any webhooks that haven't been registered yet.
    (expected_topics - current_topics).each do |topic|
      ShopifyAPI::Webhook.create(
        topic: topic,
        address: webhooks_url,
        format: 'json'
      )
    end

    # Remove any extraneous topics.
    current_webhooks.each do |webhook|
      unless expected_topics.include?(webhook.topic.to_sym)
        ShopifyAPI::Webhook.delete(webhook.id)
      end
    end

    # Ensure webhook addresses are current.
    current_webhooks.each do |webhook|
      unless webhook.address == webhooks_url
        webhook.address = webhooks_url
        webhook.save
      end
    end
  end

  private

    # Return a list of currently registered topics.
    def current_topics
      current_webhooks.map(&:topic).map(&:to_sym)
    end

    # Return a list of current registered webhooks.
    def current_webhooks
      @current_webhooks ||= ShopifyAPI::Webhook.find(:all)
    end

    # Return the absolute URL to the webhooks endpoint.
    def webhooks_url
      DiscoApp::Engine.routes.url_helpers.webhooks_url
    end

end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
disco_app-0.10.0 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.10.1 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.10.2 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.10.3 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.10.4 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.10.5 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.11.0 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.11.1 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.12.0 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.12.1 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.12.5 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.12.6 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.12.7 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.13.0 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.13.1 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.13.2 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.12.7.pre.puma.pre.3 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb