module ApiClient::Parser

ApiClient::Parser provides a method to parse the request response.

Public Class Methods

response(response, url) click to toggle source

Parse the JSON response.

@param [HTTP] response HTTP object for the request. @param [String] url The url of the requisition. @return [Hash] the body parsed.

# File lib/api-client/parser.rb, line 10
def self.response(response, url)
  raise_exception(response, url)
  begin
    object = ::JSON.parse(response.body)
  rescue ::JSON::ParserError, TypeError
    object = {}
  end
  object
end

Protected Class Methods

raise_exception(response, url) click to toggle source
# File lib/api-client/parser.rb, line 22
def self.raise_exception(response, url)
  case response.code.to_i
    when 401 then raise ApiClient::Exceptions::Unauthorized
    when 403 then raise ApiClient::Exceptions::Forbidden
    when 404 then raise ApiClient::Exceptions::NotFound.new(url)
    when 500 then raise ApiClient::Exceptions::InternalServerError
    when 502 then raise ApiClient::Exceptions::BadGateway
    when 503 then raise ApiClient::Exceptions::ServiceUnavailable
    else return
  end
end