Module: OpenLoam::Webhooks

Defined in:
lib/open_loam/webhooks.rb

Overview

Domain events, delivered over HTTP to whoever asked for them.

One global subscriber listens to every OpenLoam event and, in the tenant the event was published in, enqueues a delivery per matching active endpoint. Matching happens at dispatch time rather than at subscribe time because endpoints are data: a tenant can add one from the admin without a reboot.

Class Method Summary collapse

Class Method Details

.dispatch(event_name, payload) ⇒ Object

Runs inline in whatever published the event, so it stays cheap: one query for the tenant's active endpoints, then the HTTP call is a job.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/open_loam/webhooks.rb', line 17

def self.dispatch(event_name, payload)
  tenant = Tenant.find_by(id: payload[:tenant_id])
  return if tenant.nil?

  # Only JSON-safe primitives cross into the job — event payloads are ids
  # and scalars by convention, never records.
  deliverable = payload.transform_keys(&:to_s)

  OpenLoam.as_tenant(tenant) do
    WebhookEndpoint.active.each do |endpoint|
      next unless endpoint.matches?(event_name)

      WebhookDeliveryJob.perform_later(tenant.id, endpoint.id, event_name, deliverable)
    end
  end
end

.subscribe! ⇒ Object

Wired once from OpenLoam::Engine. Idempotent: subscribing twice would deliver every event twice.



11
12
13
# File 'lib/open_loam/webhooks.rb', line 11

def self.subscribe!
  @subscription ||= Events.subscribe_all { |event_name, payload| dispatch(event_name, payload) }
end