Class: Lucid::Shopify::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.



6
7
8
# File 'lib/lucid/shopify/webhook_handler_list.rb', line 6

def initialize
  @handlers = {}
end

Instance Method Details

#[](topic) ⇒ Object

Parameters:

  • topic (String)


27
28
29
# File 'lib/lucid/shopify/webhook_handler_list.rb', line 27

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

#delegate(webhook) ⇒ Object

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

Parameters:



34
35
36
# File 'lib/lucid/shopify/webhook_handler_list.rb', line 34

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 Lucid::Shopify::Webhook argument.

Parameters:

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

Raises:

  • (ArgumentError)


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

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

  handler = block if block

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

  nil
end