module Ably::Modules::Encodeable

Provides methods to allow this model’s ‘data` property to be encoded and decoded based on the `encoding` property.

This module expects the following:

Public Class Methods

included(base) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/encodeable.rb, line 13
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

decode(encoders, channel_options, &error_block) click to toggle source

Decode a message using the channel options and registered encoders for the client @param encoders [Array<Ably::Models::MessageEncoders::Base>] List of encoders to apply to the message @param channel_options [Hash] Channel options, currently reserved for Encryption options @return [void] @api private

# File lib/submodules/ably-ruby/lib/ably/modules/encodeable.rb, line 65
def decode(encoders, channel_options, &error_block)
  apply_encoders :decode, encoders, channel_options, &error_block
end
encode(encoders, channel_options, &error_block) click to toggle source

Encode a message using the channel options and register encoders for the client @param encoders [Array<Ably::Models::MessageEncoders::Base>] List of encoders to apply to the message @param channel_options [Hash] Channel options, currently reserved for Encryption options @return [void] @api private

# File lib/submodules/ably-ruby/lib/ably/modules/encodeable.rb, line 56
def encode(encoders, channel_options, &error_block)
  apply_encoders :encode, encoders, channel_options, &error_block
end
original_encoding() click to toggle source

The original encoding of this message when it was received as a raw message from the Ably service @return [String,nil] @api private

# File lib/submodules/ably-ruby/lib/ably/modules/encodeable.rb, line 72
def original_encoding
  raw_hash_object['encoding']
end

Private Instance Methods

apply_encoders(method, encoders, channel_options) { |cipher_error, "Encoder error #{code} trying to #{method} message: #{message}"| ... } click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/encodeable.rb, line 87
def apply_encoders(method, encoders, channel_options, &error_callback)
  max_encoding_length = 512
  message_attributes = attributes.dup

  begin
    if message_attributes[:encoding].to_s.length > max_encoding_length
      raise Ably::Exceptions::EncoderError("Encoding error, encoding value is too long: '#{message_attributes[:encoding]}'", nil, 92100)
    end

    previous_encoding = message_attributes[:encoding]
    encoders.each do |encoder|
      encoder.public_send method, message_attributes, channel_options
    end
  end until previous_encoding == message_attributes[:encoding]

  set_attributes_object message_attributes
rescue Ably::Exceptions::CipherError => cipher_error
  if block_given?
    yield cipher_error, "Encoder error #{cipher_error.code} trying to #{method} message: #{cipher_error.message}"
  else
    raise cipher_error
  end
end
decode_binary_data_before_to_json(message) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/modules/encodeable.rb, line 77
def decode_binary_data_before_to_json(message)
  data_key = message[:data] ? :data : 'data'
  encoding_key = message[:encoding] ? :encoding : 'encoding'

  if message[data_key].kind_of?(String) && message[data_key].encoding == ::Encoding::ASCII_8BIT
    message[data_key] = ::Base64.encode64(message[data_key])
    message[encoding_key] = [message[encoding_key], 'base64'].compact.join('/')
  end
end