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-0.8.8 lib/ably/models/message_encoders/json.rb
ably-0.8.7 lib/ably/models/message_encoders/json.rb
ably-0.8.6 lib/ably/models/message_encoders/json.rb
ably-rest-0.8.6 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-rest-0.8.5 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-0.8.5 lib/ably/models/message_encoders/json.rb
ably-0.8.4 lib/ably/models/message_encoders/json.rb
ably-rest-0.8.3 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-0.8.3 lib/ably/models/message_encoders/json.rb
ably-rest-0.8.2 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-0.8.2 lib/ably/models/message_encoders/json.rb
ably-0.8.1 lib/ably/models/message_encoders/json.rb
ably-rest-0.8.1 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-0.8.0 lib/ably/models/message_encoders/json.rb
ably-0.7.6 lib/ably/models/message_encoders/json.rb
ably-rest-0.7.5 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-0.7.5 lib/ably/models/message_encoders/json.rb
ably-0.7.4 lib/ably/models/message_encoders/json.rb
ably-rest-0.7.3 lib/submodules/ably-ruby/lib/ably/models/message_encoders/json.rb
ably-0.7.2 lib/ably/models/message_encoders/json.rb