Class: RelayGrid::Event
- Inherits:
-
Object
- Object
- RelayGrid::Event
- Defined in:
- lib/relaygrid/event.rb
Overview
A verified delivery-status webhook, as returned by RelayGrid::Webhooks.construct_event.
case event.type
when "delivery.delivered" then mark_delivered(event.delivery.id)
when "delivery.failed" then alert(event.delivery.)
end
Two properties of the transport shape how you should handle these:
- At-least-once. The same
idcan arrive twice (a receiver that times out after processing still gets retried). Key your side-effects offid. - Unordered.
delivery.deliveredcan arrive beforedelivery.sent. Branch onevent.delivery.status, which is the state at emission time, rather than on the order events show up in.
Instance Attribute Summary collapse
-
#created_at ⇒ Time?
readonly
When the event was emitted.
-
#data ⇒ Hash
readonly
The raw
dataobject. -
#delivery ⇒ RelayGrid::Delivery?
readonly
The delivery this event is about, in exactly the shape
client.deliveries.getreturns -- the server serializes both from one definition, so webhook-driven and polling code can share their handlers. -
#id ⇒ String
readonly
"evt_..." -- stable across retries, so it is what you deduplicate on.
-
#raw ⇒ Hash
readonly
The whole envelope as received.
-
#type ⇒ String
readonly
E.g.
Instance Method Summary collapse
- #delivery_event? ⇒ Boolean
-
#initialize(payload) ⇒ Event
constructor
A new instance of Event.
- #inspect ⇒ Object
-
#ping? ⇒ Boolean
True for the test event the dashboard's "send test event" button emits.
- #to_h ⇒ Object
Constructor Details
#initialize(payload) ⇒ Event
Returns a new instance of Event.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/relaygrid/event.rb', line 34 def initialize(payload) @raw = payload @id = payload["id"] @type = payload["type"] @created_at = parse_time(payload["created_at"]) @data = payload["data"] || {} # Built up front, not memoized: the instance is frozen below, matching # Delivery -- an Event handed to another thread never changes underneath it. @delivery = Delivery.new(@data["delivery"]) if @data["delivery"].is_a?(Hash) freeze end |
Instance Attribute Details
#created_at ⇒ Time? (readonly)
Returns when the event was emitted.
28 29 30 |
# File 'lib/relaygrid/event.rb', line 28 def created_at @created_at end |
#data ⇒ Hash (readonly)
Returns the raw data object.
30 31 32 |
# File 'lib/relaygrid/event.rb', line 30 def data @data end |
#delivery ⇒ RelayGrid::Delivery? (readonly)
The delivery this event is about, in exactly the shape
client.deliveries.get returns -- the server serializes both from one
definition, so webhook-driven and polling code can share their handlers.
52 53 54 |
# File 'lib/relaygrid/event.rb', line 52 def delivery @delivery end |
#id ⇒ String (readonly)
Returns "evt_..." -- stable across retries, so it is what you deduplicate on.
24 25 26 |
# File 'lib/relaygrid/event.rb', line 24 def id @id end |
#raw ⇒ Hash (readonly)
Returns the whole envelope as received.
32 33 34 |
# File 'lib/relaygrid/event.rb', line 32 def raw @raw end |
#type ⇒ String (readonly)
Returns e.g. "delivery.delivered".
26 27 28 |
# File 'lib/relaygrid/event.rb', line 26 def type @type end |
Instance Method Details
#delivery_event? ⇒ Boolean
59 60 61 |
# File 'lib/relaygrid/event.rb', line 59 def delivery_event? type.to_s.start_with?("delivery.") end |
#inspect ⇒ Object
67 68 69 |
# File 'lib/relaygrid/event.rb', line 67 def inspect "#<RelayGrid::Event id: #{id.inspect}, type: #{type.inspect}>" end |
#ping? ⇒ Boolean
True for the test event the dashboard's "send test event" button emits.
55 56 57 |
# File 'lib/relaygrid/event.rb', line 55 def ping? type == "ping" end |
#to_h ⇒ Object
63 64 65 |
# File 'lib/relaygrid/event.rb', line 63 def to_h raw end |