class Minter::Api::Result

Attributes

body[R]
headers[R]
status[R]

Public Class Methods

new(response, **options) click to toggle source
# File lib/minter/api/result.rb, line 8
def initialize(response, **options)
  handle_error_response(response) if !response.status.success? && !options[:suppress_errors]

  @headers = response.headers
  @body = JSON.parse(response.body)
  @status = response.status
end

Public Instance Methods

to_h() click to toggle source
# File lib/minter/api/result.rb, line 16
def to_h
  {
    headers: @headers.to_h,
    body: @body,
    status: @status.code
  }
end
to_s() click to toggle source
# File lib/minter/api/result.rb, line 24
def to_s
  to_h.slice(:body, :status).to_s
end

Private Instance Methods

handle_error_response(response) click to toggle source
# File lib/minter/api/result.rb, line 30
def handle_error_response(response)
  response_body = JSON.parse(response.body)

  case response.status.code
  when 400
    raise BadRequestError, response_body
  when 401
    raise UnauthorizedError, response_body
  when 403
    raise ForbiddenError, response_body
  when 404
    raise NotFoundError, response.uri.to_s
  when 412
    raise PreconditionFailedError, response_body["error"]
  when 429
    raise RateLimitError, response_body
  when 500
    raise InternalServerError, response_body
  else
    raise UnknownError, response_body
  end
end