Class: Ably::Models::MessageEncoders::Base
- Inherits:
-
Object
- Object
- Ably::Models::MessageEncoders::Base
- Defined in:
- lib/ably/models/message_encoders/base.rb
Overview
Base interface for an Ably Encoder
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#add_encoding_to_message(encoding, message) ⇒ void
Add encoding to the message Hash.
-
#current_encoding_part(message) ⇒ String?
Returns the right most encoding form a meessage encoding, and nil if none exists i.e.
-
#decode(message, channel_options) ⇒ void
#decode is called once for every encoding step i.e.
-
#encode(message, channel_options) ⇒ void
#encode is called once before a message is sent to Ably.
-
#initialize(client, options = {}) ⇒ Base
constructor
A new instance of Base.
-
#is_empty?(message) ⇒ Boolean
True of the message data payload is empty.
-
#strip_current_encoding_part(message) ⇒ void
Strip the current encoding part within the message Hash.
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, = {}) @client = client @options = end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
18 19 20 |
# File 'lib/ably/models/message_encoders/base.rb', line 18 def client @client end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
18 19 20 |
# File 'lib/ably/models/message_encoders/base.rb', line 18 def @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
65 66 67 |
# File 'lib/ably/models/message_encoders/base.rb', line 65 def (encoding, ) [:encoding] = [[: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’
74 75 76 77 78 |
# File 'lib/ably/models/message_encoders/base.rb', line 74 def current_encoding_part() if [:encoding] [: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.
53 54 55 |
# File 'lib/ably/models/message_encoders/base.rb', line 53 def decode(, ) 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.
36 37 38 |
# File 'lib/ably/models/message_encoders/base.rb', line 36 def encode(, ) raise "Not yet implemented" end |
#is_empty?(message) ⇒ Boolean
True of the message data payload is empty
101 102 103 |
# File 'lib/ably/models/message_encoders/base.rb', line 101 def is_empty?() [:data].nil? || [: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’
89 90 91 92 93 |
# File 'lib/ably/models/message_encoders/base.rb', line 89 def strip_current_encoding_part() raise "Cannot strip encoding when there is no encoding for this message" unless [:encoding] [:encoding] = [:encoding].split('/')[0...-1].join('/') [:encoding] = nil if [:encoding].empty? end |