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

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

Instance Method Summary collapse

Instance Method Details

#base64_body(body) ⇒ Object



35
36
37
38
39
# File 'lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_message_pack.rb', line 35

def base64_body(body)
  Base64.encode64(body)
rescue => err
  "[#{err.message}! Could not base64 encode body: '#{body}']"
end

#on_complete(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/submodules/ably-ruby/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
rescue Ably::Exceptions::InvalidResponseBody => e
  debug_info = {
    method: env.method,
    url: env.url,
    base64_body: base64_body(env.body),
    response_headers: env.response_headers
  }
  raise Ably::Exceptions::InvalidResponseBody, "#{e.message}\nRequest env: #{debug_info}"
end

#parse(body) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/submodules/ably-ruby/lib/ably/rest/middleware/parse_message_pack.rb', line 23

def parse(body)
  if body.length > 0
    MessagePack.unpack(body)
  else
    body
  end
rescue MessagePack::UnknownExtTypeError => e
  raise Ably::Exceptions::InvalidResponseBody, "MessagePack::UnknownExtTypeError body could not be decoded: #{e.message}. Got Base64:\n#{base64_body(body)}"
rescue MessagePack::MalformedFormatError => e
  raise Ably::Exceptions::InvalidResponseBody, "MessagePack::MalformedFormatError body could not be decoded: #{e.message}. Got Base64:\n#{base64_body(body)}"
end