module MarbleApiClient::Responses

Module for getting response objects

Constants

BAD_REQUEST_RESPONSE_CODE
CREATE_ACTION
FORBIDDEN_RESPONSE_CODE
INDEX_ACTION
NOT_FOUND_RESPONSE_CODE
NOT_IMPLEMENTED_RESPONSE_CODE
RESPONSES
SERVER_ERROR_RESPONSE_CODE
SUCCESS_RESPONSE_CODE
UNAUTHORIZED_RESPONSE_CODE
UNPROCESSABLE_ENTITY_RESPONSE_CODE

Public Class Methods

parse_response(response, action) click to toggle source
# File lib/marble_api_client/responses.rb, line 80
def parse_response(response, action)
  raise ArgumentError, 'HTTPResponse required' unless response.is_a?(Net::HTTPResponse)

  find_object(response, action)[:class_constant].new(response)
end

Private Class Methods

create_searches(code) click to toggle source

Creates cascading searches using lambdas and the find ifnone argument. Shortcircuits at first found object and returns it.

# File lib/marble_api_client/responses.rb, line 95
def create_searches(code)
  none_found = lambda do
    raise ArgumentError, "Unexpecred HTTResponse: #{code}"
  end

  rounded_code_search = lambda do
    RESPONSES.find(none_found) { |r| r[:action].nil? && r[:code] == rounded_code(code) }
  end

  lambda do
    RESPONSES.find(rounded_code_search) { |r| r[:action].nil? && r[:code] == code }
  end
end
find_by_code_and_action(code, action, ifnone) click to toggle source
# File lib/marble_api_client/responses.rb, line 109
def find_by_code_and_action(code, action, ifnone)
  RESPONSES.find(ifnone) do |r|
    r[:code] == code && r[:action] == action
  end
end
find_object(response, action) click to toggle source
# File lib/marble_api_client/responses.rb, line 88
def find_object(response, action)
  code_search = create_searches(response.code)
  find_by_code_and_action(response.code, action, code_search)
end
rounded_code(code) click to toggle source
# File lib/marble_api_client/responses.rb, line 115
def rounded_code(code)
  code.to_i.floor(-2).to_s
end