Class: ShopifyClient::WebhookList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/shopify-client/webhook_list.rb

Instance Method Summary collapse

Constructor Details

#initializeWebhookList

Returns a new instance of WebhookList.



5
6
7
# File 'lib/shopify-client/webhook_list.rb', line 5

def initialize
  @webhooks = {}
end

Instance Method Details

#[](topic) ⇒ Object

Parameters:

  • topic (String)


36
37
38
39
40
41
# File 'lib/shopify-client/webhook_list.rb', line 36

def [](topic)
  @webhooks[topic] ||= {
    handlers: [],
    fields: [],
  }
end

#delegate(webhook) ⇒ Object

Call each of the handlers registered for the given topic in turn.

Parameters:



29
30
31
32
33
# File 'lib/shopify-client/webhook_list.rb', line 29

def delegate(webhook)
  self[webhook.topic][:handlers].each do |handler|
    handler.(webhook)
  end
end

#each {|Hash| ... } ⇒ Object

Yields:

  • (Hash)


46
47
48
# File 'lib/shopify-client/webhook_list.rb', line 46

def each(&block)
  @webhooks.each(&block)
end

#register(topic, handler = nil, fields: [], &block) ⇒ Object

Register a handler for a webhook topic. The callable handler should receive a single ShopifyClient::Webhook argument.

Parameters:

  • topic (String)
  • handler (#call) (defaults to: nil)
  • fields (Array<String>) (defaults to: [])

    e.g. %w[id tags]

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
# File 'lib/shopify-client/webhook_list.rb', line 15

def register(topic, handler = nil, fields: [], &block)
  raise ArgumentError unless nil ^ handler ^ block

  handler = block if block

  self[topic][:handlers] << handler
  self[topic][:fields] |= fields # merge fields with previous fields

  nil
end