Class: Croudia::Response::ParseJSON

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/croudia/response/parse_json.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/croudia/response/parse_json.rb', line 7

def on_complete(env)
  return unless env[:response_headers]['content-type'].to_s.include?('json')

  case env[:body]
  when /\A\s*\z/, nil
    env[:body] = nil
  else
    env[:body] = JSON.parse(env[:body])
  end
rescue JSON::ParserError => e
  env[:status] == 200 ? raise(e) : env[:body] = nil
end