Module: AvaTax::Request

Included in:
API
Defined in:
lib/avatax/request.rb

Instance Method Summary collapse

Instance Method Details

#delete(path, options = {}, apiversion = "", headers = Hash.new) ⇒ Object



21
22
23
# File 'lib/avatax/request.rb', line 21

def delete(path, options={}, apiversion="", headers=Hash.new)
  request(:delete, path, nil, options, apiversion, headers)
end

#get(path, options = {}, apiversion = "", headers = Hash.new) ⇒ Object



9
10
11
# File 'lib/avatax/request.rb', line 9

def get(path, options={}, apiversion="", headers=Hash.new)
  request(:get, path, nil, options, apiversion, headers)
end

#post(path, model, options = {}, apiversion = "", headers = Hash.new) ⇒ Object



13
14
15
# File 'lib/avatax/request.rb', line 13

def post(path, model, options={}, apiversion="", headers=Hash.new)
  request(:post, path, model, options, apiversion, headers)
end

#put(path, model, options = {}, apiversion = "", headers = Hash.new) ⇒ Object



17
18
19
# File 'lib/avatax/request.rb', line 17

def put(path, model, options={}, apiversion="", headers=Hash.new)
  request(:put, path, model, options, apiversion, headers)
end

#request(method, path, model, options = {}, apiversion = "", headers = Hash.new) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/avatax/request.rb', line 25

def request(method, path, model, options={}, apiversion="", headers=Hash.new )
  response = connection.send(method) do |request|
    request.headers['X-Avalara-Client'] = request.headers['X-Avalara-Client'].gsub("API_VERSION", apiversion)
    request.headers=request.headers.merge(headers)  unless headers.empty?
    case method
    when :get, :delete
      request.url("#{encode_path(path)}?#{URI.encode_www_form(options)}")
    when :post, :put
      request.url("#{encode_path(path)}?#{URI.encode_www_form(options)}")
      request.headers['Content-Type'] = 'application/json'
      request.body = model.to_json unless model.empty?
    end
  end

  if faraday_response
    response
  else
    response.body
  end
end