Method: ApiAi.parse_response

Defined in:
lib/api-ai.rb

.parse_response(response, raw = false) ⇒ Object

Parse response. You probably shouldn't be calling this directly. This takes responses from the server and parses them. It also checks for errors and raises exceptions with the appropriate messages.

Parameters:

  • response (Net::HTTPResponse)
  • raw (Boolean) (defaults to: false)

    if return raw data

Raises:



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/api-ai.rb', line 29

def self.parse_response(response, raw = false)
  if response.is_a?(Net::HTTPServerError)
    fail ApiAi::Error.new("api.ai Server Error: #{response} - #{response.body}", response)
  end
  return response.body if raw
  begin
    return JSON.parse(response.body)
  rescue JSON::ParserError
    raise ApiAi::Error.new("Unable to parse JSON response: #{response.body}", response)
  end
end