Class: Chain::Middleware::HashieMashResponse

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/chain/middleware/hashie_mash_response.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chain/middleware/hashie_mash_response.rb', line 4

def on_complete(env)
  case env[:status]
  when 200
    body    = env[:body]
    json    = JSON.parse(body)
    headers = env[:response_headers]

    env[:body] = Hashie::Mash.new(json).tap do |item|
      item._headers = Hashie::Mash.new(headers)
      item._status  = env[:status]
    end
  else
    raise Chain::Middleware::RequestError, env[:status]
  end

rescue JSON::ParserError => ex
  raise Chain::Middleware::ParseError, "Unable to parse JSON response: #{ex.message}"

rescue NoMethodError => ex
  # This captures parsing errors from Hashie::Mash. Unfortunately, HM does not raise
  # their own errors.
  raise Chain::Middleware::ParseError, "Unable to parse JSON as object: #{ex.message}"
end