Class: JetstreamBridge::Models::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/jetstream_bridge/models/event.rb

Overview

Structured event object provided to consumers

Examples:

Accessing event data in consumer

JetstreamBridge.subscribe do |event|
  puts event.type              # "user.created"
  puts event.payload.id        # 123
  puts event.resource_type     # "user"
  puts event.deliveries        # 1
  puts event..trace_id # "abc123"
end

Defined Under Namespace

Classes: Metadata, PayloadAccessor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(envelope, metadata: {}) ⇒ Event



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jetstream_bridge/models/event.rb', line 77

def initialize(envelope, metadata: {})
  envelope = envelope.transform_keys(&:to_s) if envelope.respond_to?(:transform_keys)

  @event_id = envelope['event_id']
  @type = envelope['event_type']
  @resource_type = envelope['resource_type']
  @resource_id = envelope['resource_id']
  @producer = envelope['producer']
  @schema_version = envelope['schema_version'] || 1
  @trace_id = envelope['trace_id']

  @occurred_at = parse_time(envelope['occurred_at'])
  @payload = PayloadAccessor.new(envelope['payload'] || {})
   = ()
  @raw_envelope = envelope

  freeze
end

Instance Attribute Details

#event_idObject (readonly)

Returns the value of attribute event_id.



71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 71

def event_id
  @event_id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 71

def 
  
end

#occurred_atObject (readonly)

Returns the value of attribute occurred_at.



71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 71

def occurred_at
  @occurred_at
end

#payloadObject (readonly)

Access payload with method-style syntax

Examples:

event.payload.user_id  # Same as event.payload["user_id"]
event.payload.to_h     # Get raw payload hash


101
102
103
# File 'lib/jetstream_bridge/models/event.rb', line 101

def payload
  @payload
end

#producerObject (readonly)

Returns the value of attribute producer.



71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 71

def producer
  @producer
end

#resource_idObject (readonly)

Returns the value of attribute resource_id.



71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 71

def resource_id
  @resource_id
end

#resource_typeObject (readonly)

Returns the value of attribute resource_type.



71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 71

def resource_type
  @resource_type
end

#schema_versionObject (readonly)

Returns the value of attribute schema_version.



71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 71

def schema_version
  @schema_version
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 71

def trace_id
  @trace_id
end

#typeObject (readonly)

Returns the value of attribute type.



71
72
73
# File 'lib/jetstream_bridge/models/event.rb', line 71

def type
  @type
end

Instance Method Details

#[](key) ⇒ Object

Support hash-like access for backwards compatibility



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jetstream_bridge/models/event.rb', line 163

def [](key)
  case key.to_s
  when 'event_id' then @event_id
  when 'event_type' then @type
  when 'resource_type' then @resource_type
  when 'resource_id' then @resource_id
  when 'producer' then @producer
  when 'occurred_at' then @occurred_at&.iso8601
  when 'trace_id' then @trace_id
  when 'schema_version' then @schema_version
  when 'payload' then @payload.to_h
  else
    @raw_envelope[key.to_s]
  end
end

#deliveriesInteger

Number of times this message has been delivered



133
134
135
# File 'lib/jetstream_bridge/models/event.rb', line 133

def deliveries
  .deliveries || 1
end

#inspectObject



158
159
160
# File 'lib/jetstream_bridge/models/event.rb', line 158

def inspect
  "#<#{self.class.name} id=#{@event_id} type=#{@type} deliveries=#{deliveries}>"
end

#sequenceInteger?

Message sequence number in the stream



154
155
156
# File 'lib/jetstream_bridge/models/event.rb', line 154

def sequence
  .sequence
end

#streamString?

Stream this message came from



147
148
149
# File 'lib/jetstream_bridge/models/event.rb', line 147

def stream
  .stream
end

#subjectString

Subject this message was received on



140
141
142
# File 'lib/jetstream_bridge/models/event.rb', line 140

def subject
  .subject
end

#to_envelopeHash

Get raw envelope hash



106
107
108
# File 'lib/jetstream_bridge/models/event.rb', line 106

def to_envelope
  @raw_envelope
end

#to_hHash Also known as: to_hash

Get hash representation



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/jetstream_bridge/models/event.rb', line 113

def to_h
  {
    event_id: @event_id,
    type: @type,
    resource_type: @resource_type,
    resource_id: @resource_id,
    producer: @producer,
    occurred_at: @occurred_at&.iso8601,
    trace_id: @trace_id,
    schema_version: @schema_version,
    payload: @payload.to_h,
    metadata: .to_h
  }
end