Method: PCP::Message#initialize

Defined in:
lib/pcp/message.rb

#initialize(envelope_or_bytes = {}) ⇒ Object

Construct a new message or decode one

Parameters:

  • envelope_or_bytes (Hash<Symbol,Object>, Array<Integer>) (defaults to: {})

    When supplied a Hash it is taken as being a collection of envelope fields. When supplied an Array it is taken as being an array of byte values, a message in wire format.



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