Module: DiscoApp::Concerns::SynchroniseWebhooksJob

Extended by:
ActiveSupport::Concern
Included in:
SynchroniseWebhooksJob
Defined in:
app/jobs/disco_app/concerns/synchronise_webhooks_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(shop) ⇒ Object

Ensure the webhooks registered with our shop are the same as those listed in our application configuration.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/jobs/disco_app/concerns/synchronise_webhooks_job.rb', line 6

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