Class: Axn::Webhooks::Inbound::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/axn/webhooks/inbound/router.rb

Overview

Resolves a parsed webhook event to the handler to invoke. Pure logic (no Axn) — a missing constant or an unmatched key with no otherwise: raises, and the Dispatch Axn turns that raise into a reported exception + formatted result.

Instance Method Summary collapse

Constructor Details

#initialize(to:, on: nil, otherwise: nil, via: nil) ⇒ Router

rubocop:disable Naming/MethodParameterName



11
12
13
14
15
16
17
18
19
# File 'lib/axn/webhooks/inbound/router.rb', line 11

def initialize(to:, on: nil, otherwise: nil, via: nil)
  # rubocop:enable Naming/MethodParameterName
  raise Axn::Webhooks::Error, "dispatch needs a `to:` target" if to.nil?

  @to = to
  @on = on
  @otherwise = otherwise
  @via = via
end

Instance Method Details

#resolve(event) ⇒ Object

→ [handler_class, kwargs, route_async] for a matched handler, or :ack.



22
23
24
25
26
27
# File 'lib/axn/webhooks/inbound/router.rb', line 22

def resolve(event)
  return handler_for(@to, event) if @on.nil?

  key = @on.call(event)
  @to.is_a?(Hash) ? resolve_mapped(key, event) : resolve_by_convention(key, event)
end