Sha256: 2942c7de64a921e93e4344767bcb70ab59f310a66783d3c54110afd5c00e370f

Contents?: true

Size: 999 Bytes

Versions: 15

Compression:

Stored size: 999 Bytes

Contents

require 'json'

# ApiClient::Parser provides a method to parse the request response.
module ApiClient::Parser
  # 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.
  def self.response(response, url)
    raise_exception(response, url)
    begin
      object = ::JSON.parse(response.body)
    rescue ::JSON::ParserError, TypeError
      object = {}
    end
    object
  end

  protected

  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
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
api-client-3.1.0 lib/api-client/parser.rb
api-client-3.0.0 lib/api-client/parser.rb
api-client-2.7.0 lib/api-client/parser.rb
api-client-2.6.0 lib/api-client/parser.rb
api-client-2.5.0 lib/api-client/parser.rb
api-client-2.5.0.rc1 lib/api-client/parser.rb
api-client-2.4.0 lib/api-client/parser.rb
api-client-2.3.0 lib/api-client/parser.rb
api-client-2.2.0 lib/api-client/parser.rb
api-client-2.1.0 lib/api-client/parser.rb
api-client-2.0.3 lib/api-client/parser.rb
api-client-2.0.2 lib/api-client/parser.rb
api-client-2.0.1 lib/api-client/parser.rb
api-client-2.0.0 lib/api-client/parser.rb
api-client-2.0.0.rc2 lib/api-client/parser.rb