Class: Castle::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/castle/response.rb

Overview

parses api response

Constant Summary collapse

RESPONSE_ERRORS =
{
  400 => Castle::BadRequestError,
  401 => Castle::UnauthorizedError,
  403 => Castle::ForbiddenError,
  404 => Castle::NotFoundError,
  419 => Castle::UserUnauthorizedError,
  422 => Castle::InvalidParametersError
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



15
16
17
18
# File 'lib/castle/response.rb', line 15

def initialize(response)
  @response = response
  verify_response_code
end

Instance Method Details

#parseObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/castle/response.rb', line 20

def parse
  response_body = @response.body

  return {} if response_body.nil? || response_body.empty?

  begin
    JSON.parse(response_body, symbolize_names: true)
  rescue JSON::ParserError
    raise Castle::ApiError, 'Invalid response from Castle API'
  end
end