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 ⇒ String
- #encoded_payload ⇒ String
- #headers ⇒ Hash
- #key ⇒ Hash, ...
- #partition_key ⇒ Integer
- #payload ⇒ Hash
- #topic ⇒ String
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#add_fields(fields) ⇒ void
Add message_id and timestamp default values if they are in the schema and don’t already have values.
- #coerce_fields(encoder) ⇒ void
- #encoded_hash ⇒ Hash
-
#initialize(payload, topic: nil, key: nil, headers: 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, topic: nil, key: nil, headers: nil, partition_key: nil) ⇒ Message
Returns a new instance of Message.
25 26 27 28 29 30 31 32 |
# File 'lib/deimos/message.rb', line 25 def initialize(payload, topic: nil, key: nil, headers: nil, partition_key: nil) @payload = payload @payload = @payload.with_indifferent_access if @payload.is_a?(Hash) @topic = topic @key = key @headers = headers&.with_indifferent_access @partition_key = partition_key end |
Instance Attribute Details
#encoded_key ⇒ String
15 16 17 |
# File 'lib/deimos/message.rb', line 15 def encoded_key @encoded_key end |
#encoded_payload ⇒ String
17 18 19 |
# File 'lib/deimos/message.rb', line 17 def encoded_payload @encoded_payload end |
#headers ⇒ Hash
11 12 13 |
# File 'lib/deimos/message.rb', line 11 def headers @headers end |
#key ⇒ Hash, ...
9 10 11 |
# File 'lib/deimos/message.rb', line 9 def key @key end |
#partition_key ⇒ Integer
13 14 15 |
# File 'lib/deimos/message.rb', line 13 def partition_key @partition_key end |
#payload ⇒ Hash
7 8 9 |
# File 'lib/deimos/message.rb', line 7 def payload @payload end |
#topic ⇒ String
19 20 21 |
# File 'lib/deimos/message.rb', line 19 def topic @topic end |
Instance Method Details
#==(other) ⇒ Boolean
93 94 95 |
# File 'lib/deimos/message.rb', line 93 def ==(other) self.to_h == other.to_h end |
#add_fields(fields) ⇒ void
This method returns an undefined value.
Add message_id and timestamp default values if they are in the schema and don’t already have values.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/deimos/message.rb', line 38 def add_fields(fields) return if @payload.to_h.with_indifferent_access.except(:payload_key, :partition_key).blank? if @payload.respond_to?(:message_id) if fields.include?('message_id') && @payload..blank? @payload. = SecureRandom.uuid end elsif fields.include?('message_id') @payload['message_id'] ||= SecureRandom.uuid end if @payload.respond_to?(:timestamp) if fields.include?('timestamp') && @payload..blank? @payload. = Time.now end elsif fields.include?('timestamp') @payload['timestamp'] ||= Time.now.in_time_zone.to_s end end |
#coerce_fields(encoder) ⇒ void
This method returns an undefined value.
60 61 62 63 64 |
# File 'lib/deimos/message.rb', line 60 def coerce_fields(encoder) return if payload.nil? @payload = encoder.coerce(@payload) end |
#encoded_hash ⇒ Hash
67 68 69 70 71 72 73 74 75 |
# File 'lib/deimos/message.rb', line 67 def encoded_hash { topic: @topic, key: @encoded_key, headers: @headers, partition_key: @partition_key || @encoded_key, payload: @encoded_payload }.delete_if { |k, v| k == :headers && v.nil? } end |
#to_h ⇒ Hash
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/deimos/message.rb', line 78 def to_h { topic: @topic, key: @key, headers: @headers, partition_key: @partition_key || @key, payload: @payload, metadata: { decoded_payload: @payload } }.delete_if { |k, v| k == :headers && v.nil? } end |
#tombstone? ⇒ Boolean
Returns True if this message is a tombstone.
98 99 100 |
# File 'lib/deimos/message.rb', line 98 def tombstone? payload.nil? end |