class Ably::Rest::Middleware::Encoder

Encode the body of the message according to the mime type

Constants

CONTENT_TYPE

Public Instance Methods

call(env) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/rest/middleware/encoder.rb, line 11
def call(env)
  encode env if env.body
  @app.call env
end

Private Instance Methods

encode(env) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/rest/middleware/encoder.rb, line 17
def encode(env)
  env.body = case request_type(env)
  when 'application/x-msgpack'
    to_msgpack(env.body)
  when 'application/json', '', nil
    env.request_headers[CONTENT_TYPE] = 'application/json'
    to_json(env.body)
  else
    env.body
  end
end
request_type(env) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/rest/middleware/encoder.rb, line 41
def request_type(env)
  type = env.request_headers[CONTENT_TYPE].to_s
  type = type.split(';', 2).first if type.index(';')
  type
end
to_json(body) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/rest/middleware/encoder.rb, line 33
def to_json(body)
  if body.kind_of?(String)
    body
  else
    body.to_json
  end
end
to_msgpack(body) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/rest/middleware/encoder.rb, line 29
def to_msgpack(body)
  body.to_msgpack
end