lib/revo/loans_api/client.rb in revo-loans_api-0.0.9 vs lib/revo/loans_api/client.rb in revo-loans_api-0.0.10

- old
+ new

@@ -150,29 +150,31 @@ make_request(:post, "loan_requests/#{token}/card_loan", term_id: term_id) end private - attr_reader :connection, :base_url, :login, :password + API_CONTENT_TYPE = 'application/json'.freeze + attr_reader :base_url, :login, :password + def connection @connection ||= HTTP.persistent(base_url) end def loan_request_terms(&block) result = make_request(:get, "loan_requests/#{loan_request_token}", &block) return result if result.success? - Result.new(success?: false, response: { errors: { loan_request_terms: :cant_fetch } }) + Result.new(success?: false, response: { errors: { loan_request_terms: [:cant_fetch] } }) end def make_request(method, endpoint, params = {}, &block) headers = { Authorization: session_token }.compact response = connection.public_send(method, url_for(endpoint), json: params, headers: headers) handle_response(response, &block) - rescue HTTP::Error => exception - handle_error(exception) + rescue HTTP::Error => e + handle_error(e) end def handle_response(response) if response.status.success? if block_given? @@ -190,20 +192,28 @@ response = response_or_exception if response_or_exception.respond_to?(:status) if response if response.status.unauthorized? raise Revo::LoansApi::InvalidAccessTokenError else - Result.new(success?: false, response: parse(response)) + Result.new(success?: false, response: parse_errors(response)) end else raise Revo::LoansApi::UnexpectedResponseError, response_or_exception end end def parse(response) response.parse if response.body.present? rescue HTTP::Error response.to_s.presence + end + + def parse_errors(response) + if response.content_type.mime_type == API_CONTENT_TYPE + parse(response) + else + { errors: { base: [:unexpected_response] } } + end end def url_for(endpoint) [base_url, endpoint].join('/') end