Class: Ably::Rest::Middleware::ParseMessagePack

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/ably/rest/middleware/parse_message_pack.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



8
9
10
11
12
13
# File 'lib/ably/rest/middleware/parse_message_pack.rb', line 8

def on_complete(env)
  if env.response_headers['Content-Type'] == 'application/x-msgpack'
    env.body = parse(env.body) unless env.response_headers['Ably-Middleware-Parsed'] == true
    env.response_headers['Ably-Middleware-Parsed'] = true
  end
end

#parse(body) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/ably/rest/middleware/parse_message_pack.rb', line 15

def parse(body)
  if body.length > 0
    MessagePack.unpack(body)
  else
    body
  end
rescue MessagePack::MalformedFormatError => e
  raise Ably::Exceptions::InvalidResponseBody, "Expected MessagePack response: #{e.message}"
end