taxjar-ruby 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1b7e3dd712773b9ccabcb86e905ecf021c59d9cc
4
- data.tar.gz: 514648f0f55fb724aef886ccab636f9c9e40f639
3
+ metadata.gz: bf9845756bfeec2ace638f0e6414d73e2aeba0b9
4
+ data.tar.gz: f1852b4526f2fe8fac2530324acee0ca41146a78
5
5
  SHA512:
6
- metadata.gz: 46aa5499f10eb2033f826c2995ee376fe129377b0c829eb8227c161acb54b7d76549f31b738d11df8a09ca8fff7de9badaea8789b92588d0bd6297cfbdc62aa7
7
- data.tar.gz: 767079085e61f59a7cf79bcb50f05994c5e7700d7b261012187ee1cfed209b2e3ef9429b0a01b5ea7712dc066c37313369ae39c9b109ea1e98afd85ae8658626
6
+ metadata.gz: b7dfb2ad328bc585763ca567f521a705fd799582ae32076051af68278dc618eae7593392a8b2c2af3c416685aa3c44878bb1660e55eb5b638b0152e908337df6
7
+ data.tar.gz: 073e8445230a589b9d67721df6e6fad3a14e92aba7c5bfe4e440de7fc7ec6c925044877a72aac0c0524e643262d2b5125f90e8207b33032f2d88bd5ec2f57201
@@ -1,4 +1,4 @@
1
- Copyright (c) 2016 TaxJar, http://www.taxjar.com
1
+ Copyright (c) 2018 TaxJar, https://www.taxjar.com
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -938,6 +938,48 @@ Set request timeout in seconds:
938
938
  client.tax_for_order({ timeout: 30 })
939
939
  ```
940
940
 
941
+ ## Sandbox Environment
942
+
943
+ You can easily configure the client to use the [TaxJar Sandbox](https://developers.taxjar.com/api/reference/#sandbox-environment):
944
+
945
+ ```ruby
946
+ require 'taxjar'
947
+ client = Taxjar::Client.new(api_key: 'YOUR_SANDBOX_API_TOKEN', api_url: 'https://api.sandbox.taxjar.com')
948
+ ```
949
+
950
+ For testing specific [error response codes](https://developers.taxjar.com/api/reference/#errors), pass the custom `X-TJ-Expected-Response` header:
951
+
952
+ ```ruby
953
+ client.set_api_config('headers', {
954
+ 'X-TJ-Expected-Response' => 422
955
+ })
956
+ ```
957
+
958
+ ## Error Handling
959
+
960
+ When invalid data is sent to TaxJar or we encounter an error, we’ll throw a `Taxjar::Error` with the HTTP status code and error message. To catch these exceptions, refer to the example below. [Click here](https://developers.taxjar.com/api/guides/ruby/#error-handling) for a list of common error response classes.
961
+
962
+ ```ruby
963
+ require 'taxjar'
964
+ client = Taxjar::Client.new(api_key: '9e0cd62a22f451701f29c3bde214')
965
+
966
+ begin
967
+ order = client.create_order({
968
+ :transaction_date => '2015/05/14',
969
+ :to_country => 'US',
970
+ :to_state => 'CA',
971
+ :to_zip => '90002',
972
+ :amount => 17.45,
973
+ :shipping => 1.5,
974
+ :sales_tax => 0.95
975
+ })
976
+ rescue Taxjar::Error => e
977
+ # <Taxjar::Error::NotAcceptable: transaction_id is missing>
978
+ puts e.class.name
979
+ puts e.message
980
+ end
981
+ ```
982
+
941
983
  ## Tests
942
984
 
943
985
  An RSpec test suite is available to ensure API functionality:
@@ -4,7 +4,8 @@ require 'http'
4
4
  module Taxjar
5
5
  module API
6
6
  class Request
7
- BASE_URL = 'https://api.taxjar.com'
7
+ DEFAULT_API_URL = 'https://api.taxjar.com'
8
+ SANDBOX_API_URL = 'https://api.sandbox.taxjar.com'
8
9
 
9
10
  attr_reader :client, :uri, :headers, :request_method, :path, :object_key, :options
10
11
 
@@ -16,8 +17,9 @@ module Taxjar
16
17
  @client = client
17
18
  @request_method = request_method
18
19
  @path = path
19
- @uri = Addressable::URI.parse(BASE_URL + path)
20
- set_request_headers
20
+ @base_url = client.api_url ? client.api_url : DEFAULT_API_URL
21
+ @uri = Addressable::URI.parse(@base_url + path)
22
+ set_request_headers(client.headers || {})
21
23
  @object_key = object_key
22
24
  @options = options
23
25
  set_http_timeout
@@ -33,10 +35,11 @@ module Taxjar
33
35
 
34
36
  private
35
37
 
36
- def set_request_headers
38
+ def set_request_headers(custom_headers = {})
37
39
  @headers = {}
38
40
  @headers[:user_agent] = client.user_agent
39
41
  @headers[:authorization] = "Bearer #{client.api_key}"
42
+ @headers.merge!(custom_headers)
40
43
  end
41
44
 
42
45
  def set_http_timeout
@@ -12,6 +12,8 @@ module Taxjar
12
12
  include Taxjar::API::Refund
13
13
 
14
14
  attr_accessor :api_key
15
+ attr_accessor :api_url
16
+ attr_accessor :headers
15
17
 
16
18
  def initialize(options = {})
17
19
  options.each do |key, value|
@@ -24,6 +26,14 @@ module Taxjar
24
26
  !!@api_key
25
27
  end
26
28
 
29
+ def set_api_config(key, value)
30
+ instance_variable_set("@#{key}", value)
31
+ end
32
+
33
+ def get_api_config(key)
34
+ instance_variable_get("@#{key}")
35
+ end
36
+
27
37
  def user_agent
28
38
  "TaxjarRubyGem/#{Taxjar::Version}"
29
39
  end
@@ -6,7 +6,7 @@ module Taxjar
6
6
  end
7
7
 
8
8
  def minor
9
- 0
9
+ 1
10
10
  end
11
11
 
12
12
  def patch
@@ -11,35 +11,35 @@ RSpec.configure do |config|
11
11
  end
12
12
 
13
13
  def a_get(path)
14
- a_request(:get, Taxjar::API::Request::BASE_URL + path)
14
+ a_request(:get, Taxjar::API::Request::DEFAULT_API_URL + path)
15
15
  end
16
16
 
17
17
  def a_post(path)
18
- a_request(:post, Taxjar::API::Request::BASE_URL + path)
18
+ a_request(:post, Taxjar::API::Request::DEFAULT_API_URL + path)
19
19
  end
20
20
 
21
21
  def a_delete(path)
22
- a_request(:delete, Taxjar::API::Request::BASE_URL + path)
22
+ a_request(:delete, Taxjar::API::Request::DEFAULT_API_URL + path)
23
23
  end
24
24
 
25
25
  def a_put(path)
26
- a_request(:put, Taxjar::API::Request::BASE_URL + path)
26
+ a_request(:put, Taxjar::API::Request::DEFAULT_API_URL + path)
27
27
  end
28
28
 
29
29
  def stub_get(path)
30
- stub_request(:get, Taxjar::API::Request::BASE_URL + path)
30
+ stub_request(:get, Taxjar::API::Request::DEFAULT_API_URL + path)
31
31
  end
32
32
 
33
33
  def stub_post(path)
34
- stub_request(:post, Taxjar::API::Request::BASE_URL + path)
34
+ stub_request(:post, Taxjar::API::Request::DEFAULT_API_URL + path)
35
35
  end
36
36
 
37
37
  def stub_put(path)
38
- stub_request(:put, Taxjar::API::Request::BASE_URL + path)
38
+ stub_request(:put, Taxjar::API::Request::DEFAULT_API_URL + path)
39
39
  end
40
40
 
41
41
  def stub_delete(path)
42
- stub_request(:delete, Taxjar::API::Request::BASE_URL + path)
42
+ stub_request(:delete, Taxjar::API::Request::DEFAULT_API_URL + path)
43
43
  end
44
44
 
45
45
  def fixture_path
@@ -1,9 +1,15 @@
1
1
  require 'helper'
2
2
 
3
3
  describe Taxjar::API::Request do
4
- describe "#BASE_URL" do
4
+ describe "#DEFAULT_API_URL" do
5
5
  it 'should have taxjar api url' do
6
- expect(Taxjar::API::Request::BASE_URL).to eq('https://api.taxjar.com')
6
+ expect(Taxjar::API::Request::DEFAULT_API_URL).to eq('https://api.taxjar.com')
7
+ end
8
+ end
9
+
10
+ describe "#SANDBOX_API_URL" do
11
+ it 'should have sandbox taxjar api url' do
12
+ expect(Taxjar::API::Request::SANDBOX_API_URL).to eq('https://api.sandbox.taxjar.com')
7
13
  end
8
14
  end
9
15
 
@@ -24,6 +30,14 @@ describe Taxjar::API::Request do
24
30
  expect(subject.uri.to_s).to eq('https://api.taxjar.com/api_path')
25
31
  end
26
32
 
33
+ it 'should return a sandbox uri' do
34
+ client = Taxjar::Client.new(api_key: 'AK', api_url: Taxjar::API::Request::SANDBOX_API_URL)
35
+ subject = Taxjar::API::Request.new(client, :get, '/api_path', 'object')
36
+ expect(subject).to respond_to(:uri)
37
+ expect(subject.uri).to be_instance_of(Addressable::URI)
38
+ expect(subject.uri.to_s).to eq('https://api.sandbox.taxjar.com/api_path')
39
+ end
40
+
27
41
  it 'should return headers' do
28
42
  expect(subject).to respond_to(:headers)
29
43
  expect(subject.headers).to be_instance_of(Hash)
@@ -31,6 +45,18 @@ describe Taxjar::API::Request do
31
45
  expect(subject.headers[:authorization]).to eq('Bearer AK')
32
46
  end
33
47
 
48
+ it 'should return custom headers' do
49
+ client = Taxjar::Client.new(api_key: 'AK', api_url: Taxjar::API::Request::SANDBOX_API_URL, headers: {
50
+ 'X-TJ-Expected-Response' => 422
51
+ })
52
+ subject = Taxjar::API::Request.new(client, :get, '/api_path', 'object')
53
+ expect(subject).to respond_to(:headers)
54
+ expect(subject.headers).to be_instance_of(Hash)
55
+ expect(subject.headers[:user_agent]).to match('TaxjarRubyGem')
56
+ expect(subject.headers[:authorization]).to eq('Bearer AK')
57
+ expect(subject.headers['X-TJ-Expected-Response']).to eq(422)
58
+ end
59
+
34
60
  it 'should return request method' do
35
61
  expect(subject).to respond_to(:request_method)
36
62
  expect(subject.request_method).to be_instance_of(Symbol)
@@ -102,7 +128,6 @@ describe Taxjar::API::Request do
102
128
  to_return(:status => 200, :body => '{"object": {"id": "3"}}',
103
129
  :headers => {content_type: 'application/json; charset=UTF-8'})
104
130
 
105
-
106
131
  expect(subject.perform).to eq({id: '3'})
107
132
  end
108
133
  end
@@ -140,6 +165,7 @@ describe Taxjar::API::Request do
140
165
  "detail": "error explanation",
141
166
  "status": "'+ status.to_s + '"}',
142
167
  :headers => {content_type: 'application/json; charset=UTF-8'})
168
+
143
169
  expect{subject.perform}.to raise_error(exception, 'error explanation')
144
170
  end
145
171
  end
@@ -13,6 +13,34 @@ describe Taxjar::Client do
13
13
  end
14
14
  end
15
15
 
16
+ describe "#set_api_config" do
17
+ it 'sets new api key' do
18
+ client = Taxjar::Client.new(api_key: 'AK')
19
+ client.set_api_config('api_key', 'ZZ')
20
+ expect(client.api_key).to eq('ZZ')
21
+ end
22
+
23
+ it 'sets new api url' do
24
+ client = Taxjar::Client.new(api_key: 'AK')
25
+ client.set_api_config('api_url', 'https://api.sandbox.taxjar.com')
26
+ expect(client.api_url).to eq('https://api.sandbox.taxjar.com')
27
+ end
28
+
29
+ it 'sets new custom headers' do
30
+ client = Taxjar::Client.new(api_key: 'AK')
31
+ client.set_api_config('headers', { 'X-TJ-Expected-Response' => 422 })
32
+ expect(client.headers).to eq({ 'X-TJ-Expected-Response' => 422 })
33
+ end
34
+ end
35
+
36
+ describe "#get_api_config" do
37
+ it 'gets a config value' do
38
+ client = Taxjar::Client.new(api_key: 'AK')
39
+ client.set_api_config('api_key', 'ZZ')
40
+ expect(client.get_api_config('api_key')).to eq('ZZ')
41
+ end
42
+ end
43
+
16
44
  describe '#user_agent' do
17
45
  it 'returns string with version' do
18
46
  client = Taxjar::Client.new(api_key: 'AK')
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.required_ruby_version = '>= 2.0'
22
22
 
23
23
  spec.add_dependency 'addressable', '~> 2.3'
24
- spec.add_dependency 'http', '~> 2.2.2'
24
+ spec.add_dependency 'http', '~> 2.2'
25
25
  spec.add_dependency 'memoizable', '~> 0.4.0'
26
26
  spec.add_dependency 'model_attribute', '~> 3.2'
27
27
  spec.add_development_dependency "bundler", "~> 1.7"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: taxjar-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TaxJar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-30 00:00:00.000000000 Z
11
+ date: 2018-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 2.2.2
33
+ version: '2.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 2.2.2
40
+ version: '2.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: memoizable
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -176,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
176
  version: '0'
177
177
  requirements: []
178
178
  rubyforge_project:
179
- rubygems_version: 2.6.8
179
+ rubygems_version: 2.6.13
180
180
  signing_key:
181
181
  specification_version: 4
182
182
  summary: Ruby wrapper for Taxjar API