lib/credit_gateway/client.rb in credit_gateway-0.1.1 vs lib/credit_gateway/client.rb in credit_gateway-0.3.0

- old
+ new

@@ -28,11 +28,12 @@ def connection @connection ||= Faraday.new(url: api_url) do |conn| conn.request :credit_gateway_auth, CreditGateway.configuration.client_id, - CreditGateway.configuration.client_secret + CreditGateway.configuration.client_secret, + CreditGateway.configuration.country conn.response :multi_json, symbolize_keys: true if CreditGateway.configuration.debug conn.response :logger, nil, { headers: true, bodies: CreditGateway.configuration.verbose } end conn.adapter Faraday.default_adapter @@ -46,20 +47,24 @@ def handle_response(response) return if response.success? case response.status when 400 - if response.body.dig(:Errors, 0, :ErrorCode) == 'Company.NotFound' - raise CompanyNotFoundError, response.body.dig(:Errors, 0, :ErrorMessage) - else - raise InvalidRequestError, response.body - end + handle_company_not_found(response.body, InvalidRequestError) when 401 raise UnauthorizedError, response.body when 404 - raise NotFoundError, response.body + handle_company_not_found(response.body, NotFoundError) + when 409 + raise ConflictError, response.body else raise GenericError, "#{response.status}: #{response.body}" end + end + + def handle_company_not_found(body, alternative_exception) + raise CompanyNotFoundError, body.dig(0, :message) if body.dig(0, :code) == 'company_not_found' + + raise alternative_exception, body end end end