Class: HonestPubsub::Message
- Inherits:
-
Object
- Object
- HonestPubsub::Message
- Defined in:
- lib/honest_pubsub/message.rb
Constant Summary collapse
- @@message_version =
"1"
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#pub ⇒ Object
readonly
Returns the value of attribute pub.
-
#ts ⇒ Object
readonly
Returns the value of attribute ts.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize ⇒ Message
constructor
A new instance of Message.
- #parse(envelope) ⇒ Object
-
#serialize(context, routing_key, payload) ⇒ Object
Context object should be passed into the call.
- #to_hash ⇒ Object
Constructor Details
#initialize ⇒ Message
Returns a new instance of Message.
14 15 |
# File 'lib/honest_pubsub/message.rb', line 14 def initialize end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
12 13 14 |
# File 'lib/honest_pubsub/message.rb', line 12 def context @context end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
11 12 13 |
# File 'lib/honest_pubsub/message.rb', line 11 def payload @payload end |
#pub ⇒ Object (readonly)
Returns the value of attribute pub.
9 10 11 |
# File 'lib/honest_pubsub/message.rb', line 9 def pub @pub end |
#ts ⇒ Object (readonly)
Returns the value of attribute ts.
8 9 10 |
# File 'lib/honest_pubsub/message.rb', line 8 def ts @ts end |
#version ⇒ Object (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_hash ⇒ Object
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 |