Class: Banter::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/banter/message.rb

Constant Summary collapse

@@message_version =
"1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



16
17
# File 'lib/banter/message.rb', line 16

def initialize
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



12
13
14
# File 'lib/banter/message.rb', line 12

def context
  @context
end

#dead_letterObject (readonly)

Returns the value of attribute dead_letter.



13
14
15
# File 'lib/banter/message.rb', line 13

def dead_letter
  @dead_letter
end

#envelopeObject (readonly)

Returns the value of attribute envelope.



14
15
16
# File 'lib/banter/message.rb', line 14

def envelope
  @envelope
end

#payloadObject (readonly)

Returns the value of attribute payload.



11
12
13
# File 'lib/banter/message.rb', line 11

def payload
  @payload
end

#pubObject (readonly)

Returns the value of attribute pub.



9
10
11
# File 'lib/banter/message.rb', line 9

def pub
  @pub
end

#tsObject (readonly)

Returns the value of attribute ts.



8
9
10
# File 'lib/banter/message.rb', line 8

def ts
  @ts
end

#versionObject (readonly)

Returns the value of attribute version.



10
11
12
# File 'lib/banter/message.rb', line 10

def version
  @version
end

Instance Method Details

#from_hash(contents) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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?Boolean

Returns:

  • (Boolean)


69
70
71
# 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) ⇒ Object

Context object should be passed into the call.



20
21
22
23
24
25
26
27
28
# 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_hashObject



57
58
59
60
61
62
63
64
65
66
67
# 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