class Taxjar::API::Request

Constants

DEFAULT_API_URL
SANDBOX_API_URL

Attributes

client[R]
headers[R]
object_key[R]
options[R]
path[R]
request_method[R]
uri[R]

Public Class Methods

new(client, request_method, path, object_key, options = {}) click to toggle source

@param client [Taxjar::Client] @param request_method [String, Symbol] @param path [String] @param object_key [String]

# File lib/taxjar/api/request.rb, line 16
def initialize(client, request_method, path, object_key, options = {})
  @client = client
  @request_method = request_method
  @path = path
  @base_url = client.api_url ? client.api_url : DEFAULT_API_URL
  @uri = Addressable::URI.parse(@base_url + path)
  set_request_headers(client.headers || {})
  @object_key = object_key
  @options = options
  set_http_timeout
end

Public Instance Methods

perform() click to toggle source
# File lib/taxjar/api/request.rb, line 28
def perform
  options_key = [:get, :delete].include?(@request_method) ? :params : :json
  response = build_http_client.request(request_method, uri.to_s, options_key => @options)
  response_body =
    begin
      symbolize_keys!(response.parse(:json))
    rescue JSON::ParserError
      nil
    end
  fail_or_return_response_body(response, response_body)
rescue HTTP::Error => e
  raise Taxjar::Error, e
end

Private Instance Methods

build_http_client() click to toggle source
# File lib/taxjar/api/request.rb, line 44
def build_http_client
  http_client = HTTP.timeout(@http_timeout).headers(headers)
  http_client = http_client.via(*client.http_proxy) if client.http_proxy
  http_client
end
fail_or_return_response_body(response, body) click to toggle source
# File lib/taxjar/api/request.rb, line 77
def fail_or_return_response_body(response, body)
  if body.nil?
    fail(Taxjar::Error.for_json_parse_error(response.code))
  elsif response.status.success?
    body[object_key.to_sym]
  elsif !(klass = Taxjar::Error::ERRORS[response.code]).nil?
    fail(klass.from_response(body))
  else
    fail(Taxjar::Error.from_response_code(response.code))
  end
end
set_http_timeout() click to toggle source
# File lib/taxjar/api/request.rb, line 57
def set_http_timeout
  @http_timeout = {}
  @http_timeout[:write] = @options[:timeout]
  @http_timeout[:connect] = @options[:timeout]
  @http_timeout[:read] = @options[:timeout]
end
set_request_headers(custom_headers = {}) click to toggle source
# File lib/taxjar/api/request.rb, line 50
def set_request_headers(custom_headers = {})
  @headers = {}
  @headers[:user_agent] = client.user_agent
  @headers[:authorization] = "Bearer #{client.api_key}"
  @headers.merge!(custom_headers)
end
symbolize_keys!(object) click to toggle source
# File lib/taxjar/api/request.rb, line 64
def symbolize_keys!(object)
  if object.is_a?(Array)
    object.each_with_index do |val, index|
      object[index] = symbolize_keys!(val)
    end
  elsif object.is_a?(Hash)
    object.keys.each do |key|
      object[key.to_sym] = symbolize_keys!(object.delete(key))
    end
  end
  object
end