Class: Ably::Models::MessageEncoders::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ably/models/message_encoders/base.rb

Overview

Base interface for an Ably Encoder

Direct Known Subclasses

Base64, Cipher, Json, Utf8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, options = {}) ⇒ Base

Returns a new instance of Base.



20
21
22
23
# File 'lib/ably/models/message_encoders/base.rb', line 20

def initialize(client, options = {})
  @client = client
  @options = options
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/ably/models/message_encoders/base.rb', line 18

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/ably/models/message_encoders/base.rb', line 18

def options
  @options
end

Instance Method Details

#add_encoding_to_message(encoding, message) ⇒ void

This method returns an undefined value.

Add encoding to the message Hash. Ensures that encoding delimeter is used where required i.e utf-8/cipher+aes-128-cbc/base64

Parameters:

  • message (Hash)

    the message as a Hash object received directly from Ably.

  • encoding (String)

    encoding to add to the current encoding



65
66
67
# File 'lib/ably/models/message_encoders/base.rb', line 65

def add_encoding_to_message(encoding, message)
  message[:encoding] = [message[:encoding], encoding].compact.join('/')
end

#current_encoding_part(message) ⇒ String?

Returns the right most encoding form a meessage encoding, and nil if none exists i.e. current_encoding_part(‘utf-8/cipher+aes-128-cbc/base64’) => ‘base64’

Returns:

  • (String, nil)


74
75
76
77
78
# File 'lib/ably/models/message_encoders/base.rb', line 74

def current_encoding_part(message)
  if message[:encoding]
    message[:encoding].split('/')[-1]
  end
end

#decode(message, channel_options) ⇒ void

This method returns an undefined value.

#decode is called once for every encoding step i.e. if message encoding arrives with ‘utf-8/cipher+aes-128-cbc/base64’

the decoder will call #decode once for each encoding part such as 'base64', then 'cipher+aes-128-cbc', and finally 'utf-8'

It is the responsibility of the #decode method to detect the current encoding part and modify the :data & :encoding properties of the message object.

Parameters:

  • message (Hash)

    the message as a Hash object received directly from Ably. The message contains properties :name, :data, :encoding, :timestamp, and optionally :id and :client_id. This #encode method should modify the message Hash if any decoding action is to be taken

  • channel_options (Hash)

    the options used to initialize the channel that this message was received on



53
54
55
# File 'lib/ably/models/message_encoders/base.rb', line 53

def decode(message, channel_options)
  raise "Not yet implemented"
end

#encode(message, channel_options) ⇒ void

This method returns an undefined value.

#encode is called once before a message is sent to Ably

It is the responsibility of the #encode method to detect the intended encoding and modify the :data & :encoding properties of the message object.

Parameters:

  • message (Hash)

    the message as a Hash object received directly from Ably. The message contains properties :name, :data, :encoding, :timestamp, and optionally :id and :client_id. This #encode method should modify the message Hash if any encoding action is to be taken

  • channel_options (Hash)

    the options used to initialize the channel that this message was received on



36
37
38
# File 'lib/ably/models/message_encoders/base.rb', line 36

def encode(message, channel_options)
  raise "Not yet implemented"
end

#is_empty?(message) ⇒ Boolean

True of the message data payload is empty

Parameters:

  • message (Hash)

    the message as a Hash object received directly from Ably.

Returns:

  • (Boolean)


101
102
103
# File 'lib/ably/models/message_encoders/base.rb', line 101

def is_empty?(message)
  message[:data].nil? || message[:data] == ''
end

#strip_current_encoding_part(message) ⇒ void

This method returns an undefined value.

Strip the current encoding part within the message Hash.

For example, calling this method on an :encoding value of ‘utf-8/cipher+aes-128-cbc/base64’ would update the attribute :encoding to ‘utf-8/cipher+aes-128-cbc’

Parameters:

  • message (Hash)

    the message as a Hash object received directly from Ably.



89
90
91
92
93
# File 'lib/ably/models/message_encoders/base.rb', line 89

def strip_current_encoding_part(message)
  raise "Cannot strip encoding when there is no encoding for this message" unless message[:encoding]
  message[:encoding] = message[:encoding].split('/')[0...-1].join('/')
  message[:encoding] = nil if message[:encoding].empty?
end