class Deimos::Message

Basically a struct to hold the message as it's processed.

Attributes

encoded_key[RW]
encoded_payload[RW]
key[RW]
partition_key[RW]
payload[RW]
producer_name[RW]
topic[RW]

Public Class Methods

new(payload, producer, topic: nil, key: nil, partition_key: nil) click to toggle source

@param payload [Hash] @param producer [Class]

# 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

Public Instance Methods

==(other) click to toggle source

@param other [Message] @return [Boolean]

# File lib/deimos/message.rb, line 70
def ==(other)
  self.to_h == other.to_h
end
add_fields(fields) click to toggle source

Add message_id and timestamp default values if they are in the schema and don't already have values. @param fields [Array<String>] existing name fields in the schema.

# 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) click to toggle source

@param encoder [Deimos::SchemaBackends::Base]

# File lib/deimos/message.rb, line 34
def coerce_fields(encoder)
  return if payload.nil?

  @payload = encoder.coerce(@payload)
end
encoded_hash() click to toggle source

@return [Hash]

# 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() click to toggle source

@return [Hash]

# 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?() click to toggle source

@return [Boolean] True if this message is a tombstone

# File lib/deimos/message.rb, line 75
def tombstone?
  payload.nil?
end