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

Inherits:
Object
  • Object
show all
Defined in:
lib/submodules/ably-ruby/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/submodules/ably-ruby/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/submodules/ably-ruby/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/submodules/ably-ruby/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



62
63
64
# File 'lib/submodules/ably-ruby/lib/ably/models/message_encoders/base.rb', line 62

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)


70
71
72
73
74
# File 'lib/submodules/ably-ruby/lib/ably/models/message_encoders/base.rb', line 70

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



51
52
53
# File 'lib/submodules/ably-ruby/lib/ably/models/message_encoders/base.rb', line 51

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



35
36
37
# File 'lib/submodules/ably-ruby/lib/ably/models/message_encoders/base.rb', line 35

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)


95
96
97
# File 'lib/submodules/ably-ruby/lib/ably/models/message_encoders/base.rb', line 95

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.



84
85
86
87
88
# File 'lib/submodules/ably-ruby/lib/ably/models/message_encoders/base.rb', line 84

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