Sha256: 4421a3189622b1d83041f817b09c959a7bb0a33f95387cd48e1928db81c24cc2

Contents?: true

Size: 1 KB

Versions: 35

Compression:

Stored size: 1 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?
      !client.protocol_binary?
    end
  end
end

Version data entries

35 entries across 35 versions & 2 rubygems

Version Path
ably-rest-0.8.15 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-0.8.15 lib/ably/models/message_encoders/base64.rb
ably-rest-0.9.1 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-rest-0.9.0 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-rest-0.8.14 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-0.8.14 lib/ably/models/message_encoders/base64.rb
ably-rest-0.8.13 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-0.8.13 lib/ably/models/message_encoders/base64.rb
ably-0.8.12 lib/ably/models/message_encoders/base64.rb
ably-0.8.11 lib/ably/models/message_encoders/base64.rb
ably-0.8.10 lib/ably/models/message_encoders/base64.rb
ably-rest-0.8.9 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-0.8.9 lib/ably/models/message_encoders/base64.rb
ably-0.8.8 lib/ably/models/message_encoders/base64.rb
ably-0.8.7 lib/ably/models/message_encoders/base64.rb
ably-0.8.6 lib/ably/models/message_encoders/base64.rb
ably-rest-0.8.6 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-rest-0.8.5 lib/submodules/ably-ruby/lib/ably/models/message_encoders/base64.rb
ably-0.8.5 lib/ably/models/message_encoders/base64.rb
ably-0.8.4 lib/ably/models/message_encoders/base64.rb