class AvataxClient::Request::Base

Base class for handling requests from Avatax.

Private Class Methods

check_debug(response:) click to toggle source

Provide a hook to allow user to debug a response. @param response [Typhoeus::Response] response from api call. @return [void]

# File lib/avatax_client/request/base.rb, line 20
def check_debug(response:)
  if AvataxClient.configuration.debug
    AvataxClient.configuration.debug_response.call(response)
  end
end
credentials() click to toggle source
# File lib/avatax_client/request/base.rb, line 46
def credentials
  avatax = AvataxClient.configuration
  "#{avatax.username}:#{avatax.password}"
end
handle_response(response:) click to toggle source

@param response [Typhoeus::Response] response from api call. @return [AvataxClient::Response, AvataxClient::Errors]

# File lib/avatax_client/request/base.rb, line 28
def handle_response(response:)
  check_debug(response: response)

  hash = JSON.parse(response.body)
  if response.success?
    to_s.gsub("Request", "Response").constantize.new(hash)
  elsif response.timed_out?
    raise AvataxClient::Errors::TimeoutError
  else
    msg = hash.dig("error", "code") || "Error"
    raise AvataxClient::Errors::ApiError.new(hash), msg
  end
end
headers() click to toggle source
# File lib/avatax_client/request/base.rb, line 42
def headers
  { "Content-Type" => "application/json", "Accept" => "application/json" }
end
url(path) click to toggle source
# File lib/avatax_client/request/base.rb, line 51
def url(path)
  config = AvataxClient.configuration
  endpoint = config.endpoint
  version = config.version

  URI.join(endpoint, ["api", version, path].join("/")).to_s
end