Sha256: 6f727e1cdbb4e1bccb72b01628bff1820268d4079f868c2111e2a5ed957c2826

Contents?: true

Size: 913 Bytes

Versions: 82

Compression:

Stored size: 913 Bytes

Contents

require 'json'
require 'ably/models/message_encoders/base'

module Ably::Models::MessageEncoders
  # JSON Encoder and Decoder
  # Uses encoding identifier 'json' and encodes all objects that are not strings or byte arrays
  #
  class Json < Base
    ENCODING_ID = 'json'

    def encode(message, channel_options)
      if needs_json_encoding?(message)
        message[:data] = ::JSON.dump(message[:data])
        add_encoding_to_message ENCODING_ID, message
      end
    end

    def decode(message, channel_options)
      if is_json_encoded?(message)
        message[:data] = ::JSON.parse(message[:data])
        strip_current_encoding_part message
      end
    end

    private
    def needs_json_encoding?(message)
      !message[:data].kind_of?(String) && !message[:data].nil?
    end

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

Version data entries

82 entries across 82 versions & 2 rubygems

Version Path
ably-rest-1.1.5 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-1.1.5 lib/ably/models/message_encoders/json.rb
ably-rest-1.1.4 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-1.1.4 lib/ably/models/message_encoders/json.rb
ably-rest-1.1.4.rc lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-1.1.4.rc lib/ably/models/message_encoders/json.rb
ably-rest-1.1.3 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-1.1.3 lib/ably/models/message_encoders/json.rb
ably-rest-1.1.2 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-1.1.2 lib/ably/models/message_encoders/json.rb
ably-rest-1.1.2.rc1 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-1.1.1 lib/ably/models/message_encoders/json.rb
ably-rest-1.1.0 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-1.1.0 lib/ably/models/message_encoders/json.rb
ably-1.0.7 lib/ably/models/message_encoders/json.rb
ably-rest-1.0.6 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-1.0.6 lib/ably/models/message_encoders/json.rb
ably-rest-1.0.5 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-1.0.5 lib/ably/models/message_encoders/json.rb
ably-1.0.4 lib/ably/models/message_encoders/json.rb