Sha256: f7b7861f1a498aaf0c02814429cd926c014f67f6f63ebd0640c6d9d2071d0f4c

Contents?: true

Size: 1.44 KB

Versions: 9

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(shopify_domain)
    # Get the full list of expected webhook topics.
    expected_topics = [:'app/uninstalled', :'shop/update'] + 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)
        webhook.delete
      end
    end
  end

  protected

    # Return a list of additional webhook topics to listen for. This method
    # can be overridden in the application to provide a list of app-specific
    # webhooks that should be created during synchronisation.
    def topics
      []
    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

9 entries across 9 versions & 1 rubygems

Version Path
disco_app-0.6.6 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.6.7 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.6.8 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.6.9 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.7.0 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.7.1 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.7.2 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.8.0 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb
disco_app-0.8.1 app/jobs/disco_app/concerns/synchronise_webhooks_job.rb