class Banter::Message

Attributes

context[R]
dead_letter[R]
envelope[R]
payload[R]
pub[R]
ts[R]
version[R]

Public Class Methods

new() click to toggle source
# File lib/banter/message.rb, line 16
def initialize
end

Public Instance Methods

from_hash(contents) click to toggle source
# File lib/banter/message.rb, line 45
def from_hash(contents)
  @ts = contents.ts
  @pub = contents.pub
  # Version used for messaging can get updated if we need to add extra header information, and need
  # to move each section of the library separately.
  @version = contents.v
  @payload = contents.payload
  @dead_letter = contents.dead_letter
  @context = ::Banter::Context.from_json(contents[:context])
  self
end
parse(envelope) click to toggle source
# File lib/banter/message.rb, line 30
def parse(envelope)
  contents = Hashie::Mash.new(::JSON.parse(envelope))
  from_hash(contents)
  to_hash
rescue => e
  ::Hashie::Mash.new({
    ts: Time.now.to_f,
    pub: "unknown",
    v: "unknown",
    payload: envelope,
    context: { "format" => "invalid" }.as_json
  })

end
retry_completed?() click to toggle source
# File lib/banter/message.rb, line 69
def retry_completed?
  @dead_letter.present? && @dead_letter.completed.present? && @dead_letter.completed > 0
end
serialize(context, routing_key, payload) click to toggle source

Context object should be passed into the call.

# File lib/banter/message.rb, line 20
def serialize(context, routing_key, payload)
  @ts = Time.now.to_f
  @pub = "#{routing_key}:#{Socket.gethostname()}:#{Process::pid}"
  @version = @@message_version
  @payload = payload
  @context = context
  @dead_letter = nil
  to_hash
end
to_hash() click to toggle source
# File lib/banter/message.rb, line 57
def to_hash
  ::Hashie::Mash.new({
    ts: @ts,
    pub: @pub,
    v: @@message_version,
    payload: @payload,
    dead_letter: @dead_letter,
    context: @context.as_json
  })

end