module XingApi::ResponseHandler

Constants

OAUTH_ERROR_RESPONSES

Public Instance Methods

handle(response) click to toggle source
# File lib/xing_api/response_handler.rb, line 16
def handle(response)
  return Response.new('{}', response.to_hash) if response.code.to_i == 204

  unless (200..299).cover?(response.code.to_i)
    raise_failed_response!(response)
  end

  Response.new(
    response.body.to_s,
    response.to_hash
  )
end

Private Instance Methods

failed_response_for(status_code, error_name) click to toggle source
# File lib/xing_api/response_handler.rb, line 39
def failed_response_for(status_code, error_name)
  case status_code
  when 401
    unauthorized_response_for(error_name)
  when 403
    forbidden_response_for(error_name)
  when (500..504)
    XingApi::ServerError
  end || XingApi::Error
end
forbidden_response_for(error_name) click to toggle source
# File lib/xing_api/response_handler.rb, line 59
def forbidden_response_for(error_name)
  case error_name
  when 'RATE_LIMIT_EXCEEDED'
    XingApi::RateLimitExceededError
  when 'ACCESS_DENIED'
    XingApi::AccessDeniedError
  when 'INVALID_PARAMETERS'
    XingApi::InvalidParameterError
  end
end
parse_json(response) click to toggle source
# File lib/xing_api/response_handler.rb, line 70
def parse_json(response)
  JSON.parse(response.body.to_s, symbolize_names: true)
rescue JSON::ParserError
  { message: response.body.to_s }
end
raise_failed_response!(response) click to toggle source
# File lib/xing_api/response_handler.rb, line 31
def raise_failed_response!(response)
  status_code = response.code.to_i
  body = parse_json(response)
  error_class = failed_response_for(status_code, body[:error_name])

  raise error_class.new(status_code, body[:error_name], body[:message], body[:errors])
end
unauthorized_response_for(error_name) click to toggle source
# File lib/xing_api/response_handler.rb, line 50
def unauthorized_response_for(error_name)
  case error_name
  when 'INVALID_OAUTH_TOKEN'
    XingApi::InvalidOauthTokenError
  when *OAUTH_ERROR_RESPONSES
    XingApi::OauthError
  end
end