Class: Mammoth::WebhookSink
- Inherits:
-
Object
- Object
- Mammoth::WebhookSink
- Defined in:
- lib/mammoth/webhook_sink.rb
Overview
Delivers normalized Mammoth events to a webhook endpoint.
Constant Summary collapse
- SUCCESS_RANGE =
200..299
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#timeout_seconds ⇒ Object
readonly
Returns the value of attribute timeout_seconds.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
-
.from_config(config) ⇒ Mammoth::WebhookSink
Build a sink from Mammoth configuration.
Instance Method Summary collapse
-
#deliver(event) ⇒ Hash
Deliver an event to the webhook endpoint.
-
#initialize(name:, url:, timeout_seconds: 5) ⇒ WebhookSink
constructor
A new instance of WebhookSink.
Constructor Details
#initialize(name:, url:, timeout_seconds: 5) ⇒ WebhookSink
Returns a new instance of WebhookSink.
17 18 19 20 21 |
# File 'lib/mammoth/webhook_sink.rb', line 17 def initialize(name:, url:, timeout_seconds: 5) @name = name @url = URI(url) @timeout_seconds = timeout_seconds end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/mammoth/webhook_sink.rb', line 12 def name @name end |
#timeout_seconds ⇒ Object (readonly)
Returns the value of attribute timeout_seconds.
12 13 14 |
# File 'lib/mammoth/webhook_sink.rb', line 12 def timeout_seconds @timeout_seconds end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
12 13 14 |
# File 'lib/mammoth/webhook_sink.rb', line 12 def url @url end |
Class Method Details
.from_config(config) ⇒ Mammoth::WebhookSink
Build a sink from Mammoth configuration.
27 28 29 30 31 32 33 |
# File 'lib/mammoth/webhook_sink.rb', line 27 def self.from_config(config) new( name: config.dig("webhook", "name"), url: config.dig("webhook", "url"), timeout_seconds: config.dig("webhook", "timeout_seconds") ) end |
Instance Method Details
#deliver(event) ⇒ Hash
Deliver an event to the webhook endpoint.
40 41 42 43 44 45 46 47 48 |
# File 'lib/mammoth/webhook_sink.rb', line 40 def deliver(event) payload = EventSerializer.call(event) response = perform_request(payload) return delivery_result(payload, response) if SUCCESS_RANGE.cover?(response.code.to_i) raise DeliveryError, "webhook #{name} returned HTTP #{response.code}" rescue Timeout::Error, SystemCallError, SocketError, JSON::GeneratorError => e raise DeliveryError, "webhook #{name} delivery failed: #{e.}" end |