Class: ShopifyClient::WebhookList
- Inherits:
-
Object
- Object
- ShopifyClient::WebhookList
- Includes:
- Enumerable
- Defined in:
- lib/shopify-client/webhook_list.rb
Instance Method Summary collapse
- #[](topic) ⇒ Object
-
#delegate(webhook) ⇒ Object
Call each of the handlers registered for the given topic in turn.
- #each {|Hash| ... } ⇒ Object
-
#initialize ⇒ WebhookList
constructor
A new instance of WebhookList.
-
#register(topic, handler = nil, fields: [], &block) ⇒ Object
Register a handler for a webhook topic.
Constructor Details
#initialize ⇒ WebhookList
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
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.
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
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.
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 |