Class: PCP::Message

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

Overview

Represent a message that can be sent via PCP::Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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

Instance Attribute Details

#envelopeHash<Symbol,Object> (readonly)

Read access to the @envelope property

Returns:

  • (Hash<Symbol,Object>)


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

def envelope
  @envelope
end

Instance Method Details

#[](key) ⇒ Object

Get an envelope field

Parameters:

  • key (Symbol)

Returns:

  • value associated with that key



65
66
67
# File 'lib/pcp/message.rb', line 65

def [](key)
  @envelope[key]
end

#[]=(key, value) ⇒ Object

Set an envelope field

Parameters:

  • key (Symbol)
  • value

Returns:

  • value



56
57
58
# File 'lib/pcp/message.rb', line 56

def []=(key, value)
  @envelope[key] = value
end

#dataObject

Get the content of the data chunk

Returns:

  • current 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

Parameters:

  • value

Returns:

  • value



82
83
84
# File 'lib/pcp/message.rb', line 82

def data=(value)
  @chunks[0] = value
end

#debugObject

Get the content of the debug chunk

Returns:

  • current 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

Parameters:

  • value

Returns:

  • value



99
100
101
# File 'lib/pcp/message.rb', line 99

def debug=(value)
  @chunks[1] = value
end

#encodeArray<Integer>

Encodes the message as an array of byte values

Returns:

  • (Array<Integer>)


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

Parameters:

  • seconds (Numeric)

Returns:

  • the object itself



45
46
47
48
# File 'lib/pcp/message.rb', line 45

def expires(seconds)
  @envelope[:expires] = (Time.now + seconds).utc.iso8601
  self
end

#validateObject

Validate the data in message against schema

Returns:

  • ignore



123
124
125
# File 'lib/pcp/message.rb', line 123

def validate
  RSchema.validate!(PCP::Protocol::Envelope, envelope)
end