Class: Mammoth::WebhookSink

Inherits:
Object
  • Object
show all
Defined in:
lib/mammoth/webhook_sink.rb

Overview

Delivers normalized Mammoth events to a webhook endpoint.

Constant Summary collapse

SUCCESS_RANGE =

HTTP status range treated as successful webhook delivery.

200..299
SIGNING_ALGORITHM =
"hmac_sha256"
SIGNATURE_PREFIX =
"sha256="

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, url:, timeout_seconds: 5, headers: {}, signing: nil) ⇒ WebhookSink

Returns a new instance of WebhookSink.

Parameters:

  • name (String)

    destination name

  • url (String)

    webhook endpoint URL

  • timeout_seconds (Integer) (defaults to: 5)

    HTTP open/read timeout in seconds

  • headers (Hash) (defaults to: {})

    static HTTP headers applied to every request

  • signing (Hash, nil) (defaults to: nil)

    HMAC signing configuration



26
27
28
29
30
31
32
# File 'lib/mammoth/webhook_sink.rb', line 26

def initialize(name:, url:, timeout_seconds: 5, headers: {}, signing: nil)
  @name = name
  @url = URI(url)
  @timeout_seconds = timeout_seconds
  @headers = headers.transform_keys(&:to_s)
  @signing = signing
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



19
20
21
# File 'lib/mammoth/webhook_sink.rb', line 19

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/mammoth/webhook_sink.rb', line 19

def name
  @name
end

#signingObject (readonly)

Returns the value of attribute signing.



19
20
21
# File 'lib/mammoth/webhook_sink.rb', line 19

def signing
  @signing
end

#timeout_secondsObject (readonly)

Returns the value of attribute timeout_seconds.



19
20
21
# File 'lib/mammoth/webhook_sink.rb', line 19

def timeout_seconds
  @timeout_seconds
end

#urlObject (readonly)

Returns the value of attribute url.



19
20
21
# File 'lib/mammoth/webhook_sink.rb', line 19

def url
  @url
end

Class Method Details

.from_config(config) ⇒ Mammoth::WebhookSink

Build a sink from Mammoth configuration.

Parameters:

Returns:



39
40
41
42
43
44
45
46
47
# File 'lib/mammoth/webhook_sink.rb', line 39

def from_config(config)
  new(
    name: config.dig("webhook", "name"),
    url: config.dig("webhook", "url"),
    timeout_seconds: config.dig("webhook", "timeout_seconds"),
    headers: configured_headers(config),
    signing: configured_signing(config)
  )
end

Instance Method Details

#deliver(event) ⇒ Hash

Deliver an event to the webhook endpoint.

Parameters:

  • event (Hash, #to_h)

    normalized event

Returns:

  • (Hash)

    delivery result

Raises:



89
90
91
# File 'lib/mammoth/webhook_sink.rb', line 89

def deliver(event)
  deliver_payload(EventSerializer.call(event))
end

#deliver_transaction(envelope) ⇒ Hash

Deliver a transaction envelope to the webhook endpoint.

Parameters:

  • envelope (#events, #transaction_id)

    CDC transaction envelope

Returns:

  • (Hash)

    delivery result

Raises:



98
99
100
# File 'lib/mammoth/webhook_sink.rb', line 98

def deliver_transaction(envelope)
  deliver_payload(TransactionEnvelopeSerializer.call(envelope))
end