Module: Rerout::Rails::Events

Defined in:
lib/rerout/rails/events.rb

Overview

Maps Rerout webhook event names onto ‘ActiveSupport::Notifications` topic names and dispatches verified deliveries.

Every verified webhook is instrumented under CATCH_ALL (‘rerout.webhook`) so a single subscriber can see everything. Events with a recognised `event` field are also instrumented under a dedicated, more specific topic so subscribers can listen narrowly.

Subscribe the Rails way:

ActiveSupport::Notifications.subscribe('rerout.link.clicked') do |event|
  code = event.payload[:body]['code']
  Rails.logger.info("link #{code} clicked")
end

The instrumentation payload carries:

  • ‘:event` — the event-type string (e.g. `“link.clicked”`), or `“”`.

  • ‘:body` — the full parsed JSON body as a Hash.

  • ‘:request` — the `ActionDispatch::Request` that delivered the webhook.

Constant Summary collapse

CATCH_ALL =

Topic every verified webhook is instrumented under.

'rerout.webhook'
TOPICS =

Known ‘event` strings mapped onto their dedicated notification topic. Events absent from this map still fire CATCH_ALL.

{
  'link.created' => 'rerout.link.created',
  'link.updated' => 'rerout.link.updated',
  'link.deleted' => 'rerout.link.deleted',
  'link.clicked' => 'rerout.link.clicked',
  'qr.scanned' => 'rerout.qr.scanned'
}.freeze

Class Method Summary collapse

Class Method Details

.all_topicsArray<String>

Returns every notification topic this gem can emit.

Returns:

  • (Array<String>)

    every notification topic this gem can emit.



60
61
62
# File 'lib/rerout/rails/events.rb', line 60

def all_topics
  [CATCH_ALL, *TOPICS.values]
end

.dispatch(event:, body:, request: nil) ⇒ void

This method returns an undefined value.

Instrument a verified webhook delivery.

Fires CATCH_ALL unconditionally, then the event-specific topic when ‘event` is one of TOPICS.

Parameters:

  • event (String)

    the event-type string; may be empty.

  • body (Hash)

    the parsed JSON body.

  • request (ActionDispatch::Request, nil) (defaults to: nil)

    the delivering request.



50
51
52
53
54
55
56
57
# File 'lib/rerout/rails/events.rb', line 50

def dispatch(event:, body:, request: nil)
  payload = { event: event, body: body, request: request }

  ActiveSupport::Notifications.instrument(CATCH_ALL, payload)

  specific = TOPICS[event]
  ActiveSupport::Notifications.instrument(specific, payload) if specific
end