Class: Ribbon::Intercom::Packet

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/ribbon/intercom/packet.rb,
lib/ribbon/intercom/packet/method_queue.rb

Overview

Represents a collection of data to be passed between the service and client.

Defined Under Namespace

Classes: MethodQueue

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Packet

Class Methods



18
19
20
21
22
# File 'lib/ribbon/intercom/packet.rb', line 18

def initialize(params={})
  error = params.delete(:error)
  super(params)
  self.error = error if error
end

Class Method Details

.decode(encoded_packet) ⇒ Object



11
12
13
14
15
# File 'lib/ribbon/intercom/packet.rb', line 11

def decode(encoded_packet)
  hash = Marshal.load(Base64.strict_decode64(encoded_packet))
  raise Errors::InvalidEncodingError, hash.inspect unless hash.is_a?(Hash)
  new(hash)
end

Instance Method Details

#encodeObject



48
49
50
# File 'lib/ribbon/intercom/packet.rb', line 48

def encode
  Base64.strict_encode64(Marshal.dump(to_h))
end

#errorObject

Decode the error, which may not exist on the client. If the error class doesn’t exist on the client, raise a ServerError.



36
37
38
39
40
# File 'lib/ribbon/intercom/packet.rb', line 36

def error
  error? && Marshal.load(_encoded_error)
rescue
  Errors::ServerError.new('unknown server error')
end

#error=(err) ⇒ Object

Encode (marshal) the error before saving it. This allows the error to be decoded on the client when requested, rather than decoded at the same time the packet is decoded, which could cause problems if the error class doesn’t exist on the client.



29
30
31
# File 'lib/ribbon/intercom/packet.rb', line 29

def error=(err)
  self._encoded_error = Marshal.dump(err)
end

#error?Boolean

Whether the packet contains an error

Returns:

  • (Boolean)


44
45
46
# File 'lib/ribbon/intercom/packet.rb', line 44

def error?
  !!_encoded_error
end