Sha256: f6e5c85f10d5faedae415b79858b648f16e9eb1ab2e4d04c8c50e092d6a3854e

Contents?: true

Size: 1.01 KB

Versions: 47

Compression:

Stored size: 1.01 KB

Contents

require 'base64'
require 'ably/models/message_encoders/base'

module Ably::Models::MessageEncoders
  # Base64 binary Encoder and Decoder
  # Uses encoding identifier 'base64'
  #
  class Base64 < Base
    ENCODING_ID = 'base64'

    def encode(message, channel_options)
      return if is_empty?(message)

      if is_binary?(message) && transport_protocol_text?
        message[:data] = ::Base64.encode64(message[:data])
        add_encoding_to_message ENCODING_ID, message
      end
    end

    def decode(message, channel_options)
      if is_base64_encoded?(message)
        message[:data] = ::Base64.decode64(message[:data])
        strip_current_encoding_part message
      end
    end

    private
    def is_binary?(message)
      message[:data].kind_of?(String) && message[:data].encoding == Encoding::ASCII_8BIT
    end

    def is_base64_encoded?(message)
      current_encoding_part(message).to_s.match(/^#{ENCODING_ID}$/i)
    end

    def transport_protocol_text?
      !options[:binary_protocol]
    end
  end
end

Version data entries

47 entries across 47 versions & 2 rubygems

Version Path
ably-rest-1.2.7 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.2.7 lib/ably/models/message_encoders/base64.rb
ably-rest-1.2.6 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.2.6 lib/ably/models/message_encoders/base64.rb
ably-rest-1.2.4 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.2.4 lib/ably/models/message_encoders/base64.rb
ably-rest-1.2.3 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.2.3 lib/ably/models/message_encoders/base64.rb
ably-rest-1.2.2 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.2.2 lib/ably/models/message_encoders/base64.rb
ably-rest-1.2.1 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.2.1 lib/ably/models/message_encoders/base64.rb
ably-rest-1.2.0 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.2.0 lib/ably/models/message_encoders/base64.rb
ably-rest-1.1.8 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.1.8 lib/ably/models/message_encoders/base64.rb
ably-rest-1.1.7 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.1.7 lib/ably/models/message_encoders/base64.rb
ably-rest-1.1.6 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-1.1.6 lib/ably/models/message_encoders/base64.rb