Class: Ably::Rest::Middleware::ParseJson

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

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



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

def on_complete(env)
  if env.response_headers['Content-Type'] == 'application/json'
    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_json.rb', line 15

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