Class: Deimos::Message
- Inherits:
-
Object
- Object
- Deimos::Message
- Defined in:
- lib/deimos/message.rb
Overview
Basically a struct to hold the message as it's processed.
Instance Attribute Summary collapse
-
#encoded_key ⇒ Object
Returns the value of attribute encoded_key.
-
#encoded_payload ⇒ Object
Returns the value of attribute encoded_payload.
-
#key ⇒ Object
Returns the value of attribute key.
-
#partition_key ⇒ Object
Returns the value of attribute partition_key.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#producer_name ⇒ Object
Returns the value of attribute producer_name.
-
#topic ⇒ Object
Returns the value of attribute topic.
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#add_fields(fields) ⇒ Object
Add message_id and timestamp default values if they are in the schema and don't already have values.
- #coerce_fields(encoder) ⇒ Object
- #encoded_hash ⇒ Hash
-
#initialize(payload, producer, topic: nil, key: nil, partition_key: nil) ⇒ Message
constructor
A new instance of Message.
- #to_h ⇒ Hash
-
#tombstone? ⇒ Boolean
True if this message is a tombstone.
Constructor Details
#initialize(payload, producer, topic: nil, key: nil, partition_key: nil) ⇒ Message
Returns a new instance of Message.
11 12 13 14 15 16 17 |
# File 'lib/deimos/message.rb', line 11 def initialize(payload, producer, topic: nil, key: nil, partition_key: nil) @payload = payload&.with_indifferent_access @producer_name = producer&.name @topic = topic @key = key @partition_key = partition_key end |
Instance Attribute Details
#encoded_key ⇒ Object
Returns the value of attribute encoded_key.
6 7 8 |
# File 'lib/deimos/message.rb', line 6 def encoded_key @encoded_key end |
#encoded_payload ⇒ Object
Returns the value of attribute encoded_payload.
6 7 8 |
# File 'lib/deimos/message.rb', line 6 def encoded_payload @encoded_payload end |
#key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/deimos/message.rb', line 6 def key @key end |
#partition_key ⇒ Object
Returns the value of attribute partition_key.
6 7 8 |
# File 'lib/deimos/message.rb', line 6 def partition_key @partition_key end |
#payload ⇒ Object
Returns the value of attribute payload.
6 7 8 |
# File 'lib/deimos/message.rb', line 6 def payload @payload end |
#producer_name ⇒ Object
Returns the value of attribute producer_name.
6 7 8 |
# File 'lib/deimos/message.rb', line 6 def producer_name @producer_name end |
#topic ⇒ Object
Returns the value of attribute topic.
6 7 8 |
# File 'lib/deimos/message.rb', line 6 def topic @topic end |
Instance Method Details
#==(other) ⇒ Boolean
70 71 72 |
# File 'lib/deimos/message.rb', line 70 def ==(other) self.to_h == other.to_h end |
#add_fields(fields) ⇒ Object
Add message_id and timestamp default values if they are in the schema and don't already have values.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/deimos/message.rb', line 22 def add_fields(fields) return if @payload.except(:payload_key, :partition_key).blank? if fields.include?('message_id') @payload['message_id'] ||= SecureRandom.uuid end if fields.include?('timestamp') @payload['timestamp'] ||= Time.now.in_time_zone.to_s end end |
#coerce_fields(encoder) ⇒ Object
34 35 36 37 38 |
# File 'lib/deimos/message.rb', line 34 def coerce_fields(encoder) return if payload.nil? @payload = encoder.coerce(@payload) end |
#encoded_hash ⇒ Hash
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/deimos/message.rb', line 41 def encoded_hash { topic: @topic, key: @encoded_key, partition_key: @partition_key || @encoded_key, payload: @encoded_payload, metadata: { decoded_payload: @payload, producer_name: @producer_name } } end |
#to_h ⇒ Hash
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/deimos/message.rb', line 55 def to_h { topic: @topic, key: @key, partition_key: @partition_key || @key, payload: @payload, metadata: { decoded_payload: @payload, producer_name: @producer_name } } end |
#tombstone? ⇒ Boolean
Returns True if this message is a tombstone.
75 76 77 |
# File 'lib/deimos/message.rb', line 75 def tombstone? payload.nil? end |