Class: PCP::Message
- Inherits:
-
Object
- Object
- PCP::Message
- Defined in:
- lib/pcp/message.rb
Overview
Represent a message that can be sent via PCP::Client
Instance Attribute Summary collapse
-
#envelope ⇒ Hash<Symbol,Object>
readonly
Read access to the @envelope property.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get an envelope field.
-
#[]=(key, value) ⇒ Object
Set an envelope field.
-
#data ⇒ Object
Get the content of the data chunk.
-
#data=(value) ⇒ Object
Sets the content for the data chunk.
-
#debug ⇒ Object
Get the content of the debug chunk.
-
#debug=(value) ⇒ Object
Sets the content for the debug chunk.
-
#encode ⇒ Array<Integer>
Encodes the message as an array of byte values.
-
#expires(seconds) ⇒ Object
Set the expiry of the message.
-
#initialize(envelope_or_bytes = {}) ⇒ Object
constructor
Construct a new message or decode one.
-
#validate ⇒ Object
Validate the data in message against schema.
Constructor Details
#initialize(envelope_or_bytes = {}) ⇒ Object
Construct a new message or decode one
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pcp/message.rb', line 24 def initialize(envelope_or_bytes = {}) @chunks = ['', ''] case envelope_or_bytes when Array # it's bytes decode(envelope_or_bytes) when Hash # it's an envelope default_envelope = {:id => SecureRandom.uuid} @envelope = default_envelope.merge(envelope_or_bytes) else raise ArgumentError, "Unhandled type" end end |
Instance Attribute Details
#envelope ⇒ Hash<Symbol,Object> (readonly)
Read access to the @envelope property
13 14 15 |
# File 'lib/pcp/message.rb', line 13 def envelope @envelope end |
Instance Method Details
#[](key) ⇒ Object
Get an envelope field
65 66 67 |
# File 'lib/pcp/message.rb', line 65 def [](key) @envelope[key] end |
#[]=(key, value) ⇒ Object
Set an envelope field
56 57 58 |
# File 'lib/pcp/message.rb', line 56 def []=(key, value) @envelope[key] = value end |
#data ⇒ Object
Get the content of the data chunk
73 74 75 |
# File 'lib/pcp/message.rb', line 73 def data @chunks[0] end |
#data=(value) ⇒ Object
Sets the content for the data chunk
82 83 84 |
# File 'lib/pcp/message.rb', line 82 def data=(value) @chunks[0] = value end |
#debug ⇒ Object
Get the content of the debug chunk
90 91 92 |
# File 'lib/pcp/message.rb', line 90 def debug @chunks[1] end |
#debug=(value) ⇒ Object
Sets the content for the debug chunk
99 100 101 |
# File 'lib/pcp/message.rb', line 99 def debug=(value) @chunks[1] = value end |
#encode ⇒ Array<Integer>
Encodes the message as an array of byte values
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/pcp/message.rb', line 107 def encode chunks = [] @chunks.each_index do |i| chunks << frame_chunk(i + 2, @chunks[i]) end validate [1, frame_chunk(1, envelope.to_json), chunks].flatten end |
#expires(seconds) ⇒ Object
Set the expiry of the message
45 46 47 48 |
# File 'lib/pcp/message.rb', line 45 def expires(seconds) @envelope[:expires] = (Time.now + seconds).utc.iso8601 self end |