Class: Taxjar::API::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/taxjar/api/request.rb

Constant Summary collapse

DEFAULT_API_URL =
'https://api.taxjar.com'
SANDBOX_API_URL =
'https://api.sandbox.taxjar.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, request_method, path, object_key, options = {}) ⇒ Request

Returns a new instance of Request.

Parameters:

  • client (Taxjar::Client)
  • request_method (String, Symbol)
  • path (String)
  • object_key (String)


16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/taxjar/api/request.rb', line 10

def client
  @client
end

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/taxjar/api/request.rb', line 10

def headers
  @headers
end

#object_keyObject (readonly)

Returns the value of attribute object_key.



10
11
12
# File 'lib/taxjar/api/request.rb', line 10

def object_key
  @object_key
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/taxjar/api/request.rb', line 10

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/taxjar/api/request.rb', line 10

def path
  @path
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



10
11
12
# File 'lib/taxjar/api/request.rb', line 10

def request_method
  @request_method
end

#uriObject (readonly)

Returns the value of attribute uri.



10
11
12
# File 'lib/taxjar/api/request.rb', line 10

def uri
  @uri
end

Instance Method Details

#performObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# 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