Class: Hooksmith::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/hooksmith/dispatcher.rb

Overview

Dispatcher routes incoming webhook payloads to the appropriate processor.

Uses string keys internally to prevent Symbol DoS attacks when processing untrusted webhook input from external sources.

Examples:

Dispatch a webhook event:

Hooksmith::Dispatcher.new(provider: :stripe, event: :charge_succeeded, payload: payload).run!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:, event:, payload:) ⇒ Dispatcher

Initializes a new Dispatcher.

Parameters:

  • provider (Symbol, String)

    the provider (e.g., :stripe)

  • event (Symbol, String)

    the event (e.g., :charge_succeeded)

  • payload (Hash)

    the webhook payload data.



25
26
27
28
29
# File 'lib/hooksmith/dispatcher.rb', line 25

def initialize(provider:, event:, payload:)
  @provider = provider.to_s
  @event    = event.to_s
  @payload  = payload
end

Instance Attribute Details

#eventString (readonly)

Returns the event name.

Returns:

  • (String)

    the event name



16
17
18
# File 'lib/hooksmith/dispatcher.rb', line 16

def event
  @event
end

#payloadHash (readonly)

Returns the webhook payload.

Returns:

  • (Hash)

    the webhook payload



18
19
20
# File 'lib/hooksmith/dispatcher.rb', line 18

def payload
  @payload
end

#providerString (readonly)

Returns the provider name.

Returns:

  • (String)

    the provider name



14
15
16
# File 'lib/hooksmith/dispatcher.rb', line 14

def provider
  @provider
end

Instance Method Details

#run!Object?

Runs the dispatcher.

Instantiates each processor registered for the given provider and event, then selects the ones that can handle the payload using the can_handle? method.

  • If no processors qualify, logs a warning (or raises NoProcessorError in strict mode).
  • If more than one qualifies, raises MultipleProcessorsError.
  • Otherwise, processes the event with the single matching processor.

Returns:

  • (Object, nil)

    the result of the processor, or nil if no processor matched.

Raises:



43
44
45
46
47
# File 'lib/hooksmith/dispatcher.rb', line 43

def run!
  Hooksmith::Instrumentation.instrument('dispatch', provider: @provider, event: @event) do
    dispatch_webhook
  end
end