Class: HonestPubsub::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/honest_pubsub/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.



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

def initialize
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#payloadObject (readonly)

Returns the value of attribute payload.



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

def payload
  @payload
end

#pubObject (readonly)

Returns the value of attribute pub.



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

def pub
  @pub
end

#tsObject (readonly)

Returns the value of attribute ts.



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

def ts
  @ts
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#parse(envelope) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/honest_pubsub/message.rb', line 27

def parse(envelope)
  contents = Hashie::Mash.new(::JSON.parse(envelope))
  @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]
  @context = ::HonestPubsub::Context.from_json(contents[:context])
  to_hash
end

#serialize(context, routing_key, payload) ⇒ Object

Context object should be passed into the call.



18
19
20
21
22
23
24
25
# File 'lib/honest_pubsub/message.rb', line 18

def serialize(context, routing_key, payload)
  @ts = Time.now.to_i
  @pub = "#{routing_key}:#{Socket.gethostname()}:#{Process::pid}"
  @version = @@message_version
  @payload = payload
  @context = context
  to_hash
end

#to_hashObject



39
40
41
42
43
44
45
46
47
# File 'lib/honest_pubsub/message.rb', line 39

def to_hash
  ::Hashie::Mash.new({
    ts: @ts,
    pub: @pub,
    v: @@message_version,
    payload: @payload,
    context: @context.as_json
  })
end