Class: JetstreamBridge::Models::EventEnvelope
- Inherits:
-
Object
- Object
- JetstreamBridge::Models::EventEnvelope
- Defined in:
- lib/jetstream_bridge/models/event_envelope.rb
Overview
Value object representing an event envelope
Constant Summary collapse
- SCHEMA_VERSION =
1
Instance Attribute Summary collapse
-
#event_id ⇒ Object
readonly
Returns the value of attribute event_id.
-
#event_type ⇒ Object
readonly
Returns the value of attribute event_type.
-
#occurred_at ⇒ Object
readonly
Returns the value of attribute occurred_at.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#producer ⇒ Object
readonly
Returns the value of attribute producer.
-
#resource_id ⇒ Object
readonly
Returns the value of attribute resource_id.
-
#resource_type ⇒ Object
readonly
Returns the value of attribute resource_type.
-
#schema_version ⇒ Object
readonly
Returns the value of attribute schema_version.
-
#trace_id ⇒ Object
readonly
Returns the value of attribute trace_id.
Class Method Summary collapse
-
.from_h(hash) ⇒ Object
Create from hash.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(resource_type:, event_type:, payload:, event_id: nil, occurred_at: nil, trace_id: nil, producer: nil, resource_id: nil) ⇒ EventEnvelope
constructor
A new instance of EventEnvelope.
-
#to_h ⇒ Object
Convert to hash for serialization.
Constructor Details
#initialize(resource_type:, event_type:, payload:, event_id: nil, occurred_at: nil, trace_id: nil, producer: nil, resource_id: nil) ⇒ EventEnvelope
Returns a new instance of EventEnvelope.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 15 def initialize( resource_type:, event_type:, payload:, event_id: nil, occurred_at: nil, trace_id: nil, producer: nil, resource_id: nil ) @event_id = event_id || SecureRandom.uuid @schema_version = SCHEMA_VERSION @event_type = event_type.to_s @producer = producer || JetstreamBridge.config.app_name @resource_type = resource_type.to_s @resource_id = resource_id || extract_resource_id(payload) @occurred_at = parse_occurred_at(occurred_at) @trace_id = trace_id || SecureRandom.hex(8) @payload = deep_freeze(payload) validate! freeze end |
Instance Attribute Details
#event_id ⇒ Object (readonly)
Returns the value of attribute event_id.
12 13 14 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12 def event_id @event_id end |
#event_type ⇒ Object (readonly)
Returns the value of attribute event_type.
12 13 14 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12 def event_type @event_type end |
#occurred_at ⇒ Object (readonly)
Returns the value of attribute occurred_at.
12 13 14 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12 def occurred_at @occurred_at end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
12 13 14 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12 def payload @payload end |
#producer ⇒ Object (readonly)
Returns the value of attribute producer.
12 13 14 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12 def producer @producer end |
#resource_id ⇒ Object (readonly)
Returns the value of attribute resource_id.
12 13 14 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12 def resource_id @resource_id end |
#resource_type ⇒ Object (readonly)
Returns the value of attribute resource_type.
12 13 14 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12 def resource_type @resource_type end |
#schema_version ⇒ Object (readonly)
Returns the value of attribute schema_version.
12 13 14 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12 def schema_version @schema_version end |
#trace_id ⇒ Object (readonly)
Returns the value of attribute trace_id.
12 13 14 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12 def trace_id @trace_id end |
Class Method Details
.from_h(hash) ⇒ Object
Create from hash
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 59 def self.from_h(hash) new( event_id: hash['event_id'] || hash[:event_id], event_type: hash['event_type'] || hash[:event_type], producer: hash['producer'] || hash[:producer], resource_type: hash['resource_type'] || hash[:resource_type], resource_id: hash['resource_id'] || hash[:resource_id], occurred_at: parse_time(hash['occurred_at'] || hash[:occurred_at]), trace_id: hash['trace_id'] || hash[:trace_id], payload: hash['payload'] || hash[:payload] || {} ) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
72 73 74 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 72 def ==(other) other.is_a?(EventEnvelope) && event_id == other.event_id end |
#hash ⇒ Object
78 79 80 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 78 def hash event_id.hash end |
#to_h ⇒ Object
Convert to hash for serialization
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 40 def to_h hash = { event_id: @event_id, schema_version: @schema_version, event_type: @event_type, producer: @producer, resource_type: @resource_type, occurred_at: format_time(@occurred_at), payload: @payload } # Only include optional fields if they have values hash[:resource_id] = @resource_id if @resource_id && !@resource_id.to_s.empty? hash[:trace_id] = @trace_id if @trace_id && !@trace_id.to_s.empty? hash end |