Sha256: 1ed556582043d5b88c675c032b80da2de90d9dd2d416b676f686ba13001a51b0

Contents?: true

Size: 910 Bytes

Versions: 4

Compression:

Stored size: 910 Bytes

Contents

# 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.
  # @return [Array] the code and the body parsed.
  def self.response(response)
    raise_exception(response.code)
    begin
      object = JSON.parse(response.body)
    rescue JSON::ParserError, TypeError
      object = {}
    end
    object
  end

  protected

  def self.raise_exception(code)
    case code.to_i
      when 401 then raise ApiClient::Exceptions::Unauthorized
      when 403 then raise ApiClient::Exceptions::Forbidden
      when 404 then raise ApiClient::Exceptions::NotFound
      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

4 entries across 4 versions & 1 rubygems

Version Path
api-client-1.8.1 lib/api-client/parser.rb
api-client-1.8.0 lib/api-client/parser.rb
api-client-1.7.1 lib/api-client/parser.rb
api-client-1.7.0 lib/api-client/parser.rb