Sha256: 382ade3509039f514922a66ab32be3bca3fdf05f8c72e0e0a442ebfcc40632d2

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

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, remote_object)
    begin
      object = JSON.parse(response.body)
      object = object[remote_object] if object.key?(remote_object)
      object = object[remote_object.pluralize] if object.key?(remote_object.pluralize)
    rescue JSON::ParserError
      object = {}
    end
    raise_exception(response.code)
    object
  end

  protected

  def self.raise_exception(code)
    case code
      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.5.4 lib/api-client/parser.rb
api-client-1.5.3 lib/api-client/parser.rb
api-client-1.5.2 lib/api-client/parser.rb
api-client-1.5.1 lib/api-client/parser.rb