Class: JetstreamBridge::Models::EventEnvelope

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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_idObject (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_typeObject (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_atObject (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

#payloadObject (readonly)

Returns the value of attribute payload.



12
13
14
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 12

def payload
  @payload
end

#producerObject (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_idObject (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_typeObject (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_versionObject (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_idObject (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

#hashObject



78
79
80
# File 'lib/jetstream_bridge/models/event_envelope.rb', line 78

def hash
  event_id.hash
end

#to_hObject

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