Class: LucidShopify::WebhookHandlerList

Inherits:
Object
  • Object
show all
Defined in:
lib/lucid_shopify/webhook_handler_list.rb

Instance Method Summary collapse

Constructor Details

#initializeWebhookHandlerList

Returns a new instance of WebhookHandlerList.



5
6
7
# File 'lib/lucid_shopify/webhook_handler_list.rb', line 5

def initialize
  @handlers = {}
end

Instance Method Details

#[](topic) ⇒ Object

Parameters:

  • topic (String)


30
31
32
# File 'lib/lucid_shopify/webhook_handler_list.rb', line 30

def [](topic)
  @handlers[topic] || []
end

#delegate(webhook) ⇒ Object

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

Parameters:



39
40
41
# File 'lib/lucid_shopify/webhook_handler_list.rb', line 39

def delegate(webhook)
  self[webhook.topic].each { |handler| handler.(webhook) }
end

#register(topic, handler = nil, &block) ⇒ Object

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

Parameters:

  • topic (String)
  • handler (#call) (defaults to: nil)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
# File 'lib/lucid_shopify/webhook_handler_list.rb', line 16

def register(topic, handler = nil, &block)
  raise ArgumentError unless nil ^ handler ^ block

  handler = block if block

  @handlers[topic] ||= []
  @handlers[topic] << handler

  nil
end