Class: Tango::Response::ParseJson

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

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



24
25
26
27
28
# File 'lib/tango/response/parse_json.rb', line 24

def on_complete(env)
  if respond_to?(:parse)
    env[:body] = parse(env[:body])
  end
end

#parse(body) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tango/response/parse_json.rb', line 10

def parse(body)
  case body
  when /\A^\s*$\z/, nil
    nil
  else
    json = MultiJson.load(body, :symbolize_keys => true)
    unless json.is_a?(Hash) && json[:response].is_a?(Hash) && json[:responseType] == 'SUCCESS'
      raise ::Tango::Error::ServerError.from_response(json)
    end

    json[:response]
  end
end