class Ribbon::Intercom::Packet

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

Public Class Methods

decode(encoded_packet) click to toggle source
# 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
new(params={}) click to toggle source
Calls superclass method
# File lib/ribbon/intercom/packet.rb, line 18
def initialize(params={})
  error = params.delete(:error)
  super(params)
  self.error = error if error
end

Public Instance Methods

encode() click to toggle source
# File lib/ribbon/intercom/packet.rb, line 48
def encode
  Base64.strict_encode64(Marshal.dump(to_h))
end
error() click to toggle source

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

# 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) click to toggle source

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.

# File lib/ribbon/intercom/packet.rb, line 29
def error=(err)
  self._encoded_error = Marshal.dump(err)
end
error?() click to toggle source

Whether the packet contains an error

# File lib/ribbon/intercom/packet.rb, line 44
def error?
  !!_encoded_error
end