taxjar-ruby 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,23 +15,5 @@ module Taxjar
15
15
  perform_post_with_object("/v2/taxes", 'tax', options, Taxjar::Tax)
16
16
  end
17
17
 
18
- def create_order(options = {})
19
- perform_post_with_object("/v2/transactions/orders", 'order', options, Taxjar::Order)
20
- end
21
-
22
- def update_order(options = {})
23
- id = options.fetch(:transaction_id)
24
- perform_put_with_object("/v2/transactions/orders/#{id}", 'order', options, Taxjar::Order)
25
- end
26
-
27
- def create_refund(options = {})
28
- perform_post_with_object("/v2/transactions/refunds", 'refund', options, Taxjar::Refund)
29
- end
30
-
31
- def update_refund(options = {})
32
- id = options.fetch(:transaction_id)
33
- perform_put_with_object("/v2/transactions/refunds/#{id}", 'refund', options, Taxjar::Refund)
34
- end
35
-
36
18
  end
37
19
  end
@@ -0,0 +1,29 @@
1
+ require 'taxjar/api/utils'
2
+ module Taxjar
3
+ module API
4
+ module Order
5
+ include Taxjar::API::Utils
6
+
7
+ def list_orders(options = {})
8
+ perform_get_with_array("/v2/transactions/orders", 'orders', options)
9
+ end
10
+
11
+ def show_order(id, options = {})
12
+ perform_get_with_object("/v2/transactions/orders/#{id}", 'order', options, Taxjar::Order)
13
+ end
14
+
15
+ def create_order(options = {})
16
+ perform_post_with_object("/v2/transactions/orders", 'order', options, Taxjar::Order)
17
+ end
18
+
19
+ def update_order(options = {})
20
+ id = options.fetch(:transaction_id)
21
+ perform_put_with_object("/v2/transactions/orders/#{id}", 'order', options, Taxjar::Order)
22
+ end
23
+
24
+ def delete_order(id, options={})
25
+ perform_delete_with_object("/v2/transactions/orders/#{id}", 'order', options, Taxjar::Order)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require 'taxjar/api/utils'
2
+ module Taxjar
3
+ module API
4
+ module Refund
5
+ include Taxjar::API::Utils
6
+
7
+ def list_refunds(options = {})
8
+ perform_get_with_array("/v2/transactions/refunds", 'refunds', options)
9
+ end
10
+
11
+ def show_refund(id, options = {})
12
+ perform_get_with_object("/v2/transactions/refunds/#{id}", 'refund', options, Taxjar::Refund)
13
+ end
14
+
15
+ def create_refund(options = {})
16
+ perform_post_with_object("/v2/transactions/refunds", 'refund', options, Taxjar::Refund)
17
+ end
18
+
19
+ def update_refund(options = {})
20
+ id = options.fetch(:transaction_id)
21
+ perform_put_with_object("/v2/transactions/refunds/#{id}", 'refund', options, Taxjar::Refund)
22
+ end
23
+
24
+ def delete_refund(id, options={})
25
+ perform_delete_with_object("/v2/transactions/refunds/#{id}", 'refund', options, Taxjar::Refund)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -16,6 +16,10 @@ module Taxjar
16
16
  perform_request_with_objects(:get, path, object_key, options, klass)
17
17
  end
18
18
 
19
+ def perform_get_with_array(path, object_key, options)
20
+ perform_request_with_array(:get, path, object_key, options)
21
+ end
22
+
19
23
  def perform_post_with_object(path, object_key, options, klass)
20
24
  perform_request_with_object(:post, path, object_key, options, klass)
21
25
  end
@@ -24,6 +28,10 @@ module Taxjar
24
28
  perform_request_with_object(:put, path, object_key, options, klass)
25
29
  end
26
30
 
31
+ def perform_delete_with_object(path, object_key, options, klass)
32
+ perform_request_with_object(:delete, path, object_key, options, klass)
33
+ end
34
+
27
35
  def perform_request_with_object(request_method, path, object_key, options, klass)
28
36
  response = perform_request(request_method, path, object_key, options)
29
37
  klass.new(response)
@@ -35,6 +43,10 @@ module Taxjar
35
43
  klass.new(element)
36
44
  end
37
45
  end
46
+
47
+ def perform_request_with_array(request_method, path, object_key, options)
48
+ perform_request(request_method, path, object_key, options) || []
49
+ end
38
50
  end
39
51
  end
40
52
  end
data/lib/taxjar/base.rb CHANGED
@@ -2,6 +2,27 @@ require 'addressable/uri'
2
2
  require 'forwardable'
3
3
  require 'memoizable'
4
4
 
5
+ if !Array.new.respond_to?(:to_h)
6
+ module Enumerable
7
+ def to_h(*arg)
8
+ h = {}
9
+ each_with_index(*arg) do |elem, i|
10
+ unless elem.respond_to?(:to_ary)
11
+ raise TypeError, "wrong element type #{elem.class} at #{i} (expected array)"
12
+ end
13
+
14
+ ary = elem.to_ary
15
+ if ary.size != 2
16
+ raise ArgumentError, "wrong array length at #{i} (expected 2, was #{ary.size})"
17
+ end
18
+
19
+ h[ary[0]] = ary[1]
20
+ end
21
+ h
22
+ end
23
+ end
24
+ end
25
+
5
26
  module Taxjar
6
27
  class Base
7
28
  extend Forwardable
data/lib/taxjar/client.rb CHANGED
@@ -1,10 +1,15 @@
1
1
  require 'taxjar/api/api'
2
+ require 'taxjar/api/order'
3
+ require 'taxjar/api/refund'
2
4
  require 'taxjar/error'
3
5
  require 'taxjar/api/request'
4
6
  require 'taxjar/api/utils'
5
7
  module Taxjar
6
8
  class Client
7
9
  include Taxjar::API
10
+ include Taxjar::API::Order
11
+ include Taxjar::API::Refund
12
+
8
13
  attr_accessor :api_key
9
14
 
10
15
  def initialize(options = {})
@@ -7,11 +7,11 @@ module Taxjar
7
7
  end
8
8
 
9
9
  def minor
10
- 0
10
+ 1
11
11
  end
12
12
 
13
13
  def patch
14
- 1
14
+ 0
15
15
  end
16
16
 
17
17
  def pre
@@ -0,0 +1,6 @@
1
+ {
2
+ "orders": [
3
+ "123",
4
+ "456"
5
+ ]
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "refunds": [
3
+ "321",
4
+ "654"
5
+ ]
6
+ }
data/spec/helper.rb CHANGED
@@ -19,6 +19,10 @@ def a_post(path)
19
19
  a_request(:post, Taxjar::API::Request::BASE_URL + path)
20
20
  end
21
21
 
22
+ def a_delete(path)
23
+ a_request(:delete, Taxjar::API::Request::BASE_URL + path)
24
+ end
25
+
22
26
  def a_put(path)
23
27
  a_request(:put, Taxjar::API::Request::BASE_URL + path)
24
28
  end
@@ -35,6 +39,10 @@ def stub_put(path)
35
39
  stub_request(:put, Taxjar::API::Request::BASE_URL + path)
36
40
  end
37
41
 
42
+ def stub_delete(path)
43
+ stub_request(:delete, Taxjar::API::Request::BASE_URL + path)
44
+ end
45
+
38
46
  def fixture_path
39
47
  File.expand_path('../fixtures', __FILE__)
40
48
  end
@@ -74,134 +74,4 @@ describe Taxjar::API do
74
74
  expect(tax.tax_source).to eq('destination')
75
75
  end
76
76
  end
77
-
78
- describe "#create_order" do
79
- before do
80
- stub_post("/v2/transactions/orders").to_return(body: fixture('order.json'),
81
- headers: {content_type: 'application/json; charset=utf-8'})
82
-
83
- @order = {:transaction_id => '123',
84
- :transaction_date => '2015/05/14',
85
- :to_country => 'US',
86
- :to_zip => '90002',
87
- :to_city => 'Los Angeles',
88
- :to_street => '123 Palm Grove Ln',
89
- :amount => 17.45,
90
- :shipping => 1.5,
91
- :sales_tax => 0.95,
92
- :line_items => [{:quantity => 1,
93
- :product_identifier => '12-34243-9',
94
- :descriptiion => 'Fuzzy Widget',
95
- :unit_price => 15.0,
96
- :sales_tax => 0.95}]
97
- }
98
- end
99
-
100
- it 'requests the right resource' do
101
- @client.create_order(@order)
102
- expect(a_post("/v2/transactions/orders")).to have_been_made
103
- end
104
-
105
- it 'returns the created order' do
106
- order = @client.create_order(@order)
107
- expect(order).to be_a Taxjar::Order
108
- expect(order.transaction_id).to eq(123)
109
- end
110
- end
111
-
112
- describe "#update_order" do
113
- before do
114
- @order_id = 123
115
- stub_put("/v2/transactions/orders/#{@order_id}").to_return(body: fixture('order.json'),
116
- headers: {content_type: 'application/json; charset=utf-8'})
117
-
118
- @order = {:transaction_id => '123',
119
- :amount => 17.95,
120
- :shipping => 2.0,
121
- :line_items => [{:quantity => 1,
122
- :product_identifier => '12-34243-0',
123
- :descriptiion => 'Heavy Widget',
124
- :unit_price => 15.0,
125
- :discount => 0.0,
126
- :sales_tax => 0.95}]
127
- }
128
- end
129
-
130
- it 'requests the right resource' do
131
- @client.update_order(@order)
132
- expect(a_put("/v2/transactions/orders/#{@order_id}")).to have_been_made
133
- end
134
-
135
- it 'returns the updated order' do
136
- order = @client.update_order(@order)
137
- expect(order).to be_a Taxjar::Order
138
- expect(order.transaction_id).to eq(123)
139
- end
140
- end
141
-
142
- describe "#create_refund" do
143
- before do
144
- stub_post("/v2/transactions/refunds").to_return(body: fixture('refund.json'),
145
- headers: {content_type: 'application/json; charset=utf-8'})
146
-
147
- @refund = {:transaction_id => '321',
148
- :transaction_date => '2015/05/14',
149
- :transaction_reference_id => '123',
150
- :to_country => 'US',
151
- :to_zip => '90002',
152
- :to_state => 'CA',
153
- :to_city => 'Los Angeles',
154
- :to_street => '123 Palm Grove Ln',
155
- :amount => 17.45,
156
- :shipping => 1.5,
157
- :sales_tax => 0.95,
158
- :line_items => [{:quantity => 1,
159
- :product_identifier => '12-34243-9',
160
- :descriptiion => 'Fuzzy Widget',
161
- :unit_price => 15.0,
162
- :sales_tax => 0.95}]
163
- }
164
- end
165
-
166
- it 'requests the right resource' do
167
- @client.create_refund(@refund)
168
- expect(a_post("/v2/transactions/refunds")).to have_been_made
169
- end
170
-
171
- it 'returns the created order' do
172
- refund = @client.create_refund(@refund)
173
- expect(refund).to be_a Taxjar::Refund
174
- expect(refund.transaction_id).to eq(321)
175
- end
176
- end
177
-
178
- describe "#update_refund" do
179
- before do
180
- @refund_id = 321
181
- stub_put("/v2/transactions/refunds/#{@refund_id}").to_return(body: fixture('refund.json'),
182
- headers: {content_type: 'application/json; charset=utf-8'})
183
-
184
- @refund = {:transaction_id => '321',
185
- :amount => 17.95,
186
- :shipping => 2.0,
187
- :sales_tax => 0.95,
188
- :line_items => [{:quantity => 1,
189
- :product_identifier => '12-34243-9',
190
- :descriptiion => 'Heavy Widget',
191
- :unit_price => 15.0,
192
- :sales_tax => 0.95}]
193
- }
194
- end
195
-
196
- it 'requests the right resource' do
197
- @client.update_refund(@refund)
198
- expect(a_put("/v2/transactions/refunds/#{@refund_id}")).to have_been_made
199
- end
200
-
201
- it 'returns the updated refund' do
202
- refund = @client.update_refund(@refund)
203
- expect(refund).to be_a Taxjar::Refund
204
- expect(refund.transaction_id).to eq(321)
205
- end
206
- end
207
77
  end
@@ -0,0 +1,156 @@
1
+ require 'helper'
2
+
3
+ describe Taxjar::API::Order do
4
+
5
+ before do
6
+ @client = Taxjar::Client.new(api_key: 'AK')
7
+ end
8
+
9
+ describe "#list_orders" do
10
+ context "without parameters" do
11
+ before do
12
+ stub_get('/v2/transactions/orders').to_return(body: fixture('orders.json'),
13
+ headers: {content_type: 'application/json; charset=utf-8'})
14
+
15
+ end
16
+
17
+ it 'requests the right resource' do
18
+ @client.list_orders
19
+ expect(a_get('/v2/transactions/orders')).to have_been_made
20
+ end
21
+
22
+ it 'returns the requested orders' do
23
+ orders = @client.list_orders
24
+ expect(orders).to be_an Array
25
+ expect(orders.first).to be_a String
26
+ expect(orders.first).to eq('123')
27
+ end
28
+ end
29
+
30
+ context "with parameters" do
31
+ before do
32
+ stub_get('/v2/transactions/orders?from_transaction_date=2015/05/01&to_transaction_date=2015/05/31').
33
+ to_return(body: fixture('orders.json'),
34
+ headers: {content_type: 'application/json; charset=utf-8'})
35
+
36
+ end
37
+
38
+ it 'requests the right resource' do
39
+ @client.list_orders(from_transaction_date: '2015/05/01',
40
+ to_transaction_date: '2015/05/31')
41
+ expect(a_get('/v2/transactions/orders?from_transaction_date=2015/05/01&to_transaction_date=2015/05/31')).to have_been_made
42
+ end
43
+
44
+ it 'returns the requested orders' do
45
+ orders = @client.list_orders(from_transaction_date: '2015/05/01',
46
+ to_transaction_date: '2015/05/31')
47
+ expect(orders).to be_an Array
48
+ expect(orders.first).to be_a String
49
+ expect(orders.first).to eq('123')
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "#show_order" do
55
+ before do
56
+ stub_get('/v2/transactions/orders/123').
57
+ to_return(body: fixture('order.json'),
58
+ headers: {content_type: 'application/json; charset=utf-8'})
59
+ end
60
+
61
+ it 'requests the right resource' do
62
+ @client.show_order('123')
63
+ expect(a_get('/v2/transactions/orders/123')).to have_been_made
64
+ end
65
+
66
+ it 'returns the requested order' do
67
+ order = @client.show_order('123')
68
+ expect(order).to be_an Taxjar::Order
69
+ expect(order.transaction_id).to eq(123)
70
+ end
71
+ end
72
+
73
+ describe "#create_order" do
74
+ before do
75
+ stub_post("/v2/transactions/orders").to_return(body: fixture('order.json'),
76
+ headers: {content_type: 'application/json; charset=utf-8'})
77
+
78
+ @order = {:transaction_id => '123',
79
+ :transaction_date => '2015/05/14',
80
+ :to_country => 'US',
81
+ :to_zip => '90002',
82
+ :to_city => 'Los Angeles',
83
+ :to_street => '123 Palm Grove Ln',
84
+ :amount => 17.45,
85
+ :shipping => 1.5,
86
+ :sales_tax => 0.95,
87
+ :line_items => [{:quantity => 1,
88
+ :product_identifier => '12-34243-9',
89
+ :descriptiion => 'Fuzzy Widget',
90
+ :unit_price => 15.0,
91
+ :sales_tax => 0.95}]
92
+ }
93
+ end
94
+
95
+ it 'requests the right resource' do
96
+ @client.create_order(@order)
97
+ expect(a_post("/v2/transactions/orders")).to have_been_made
98
+ end
99
+
100
+ it 'returns the created order' do
101
+ order = @client.create_order(@order)
102
+ expect(order).to be_a Taxjar::Order
103
+ expect(order.transaction_id).to eq(123)
104
+ end
105
+ end
106
+
107
+ describe "#update_order" do
108
+ before do
109
+ @order_id = 123
110
+ stub_put("/v2/transactions/orders/#{@order_id}").to_return(body: fixture('order.json'),
111
+ headers: {content_type: 'application/json; charset=utf-8'})
112
+
113
+ @order = {:transaction_id => '123',
114
+ :amount => 17.95,
115
+ :shipping => 2.0,
116
+ :line_items => [{:quantity => 1,
117
+ :product_identifier => '12-34243-0',
118
+ :descriptiion => 'Heavy Widget',
119
+ :unit_price => 15.0,
120
+ :discount => 0.0,
121
+ :sales_tax => 0.95}]
122
+ }
123
+ end
124
+
125
+ it 'requests the right resource' do
126
+ @client.update_order(@order)
127
+ expect(a_put("/v2/transactions/orders/#{@order_id}")).to have_been_made
128
+ end
129
+
130
+ it 'returns the updated order' do
131
+ order = @client.update_order(@order)
132
+ expect(order).to be_a Taxjar::Order
133
+ expect(order.transaction_id).to eq(123)
134
+ end
135
+ end
136
+
137
+ describe "#delete_order" do
138
+ before do
139
+ stub_delete('/v2/transactions/orders/123').
140
+ to_return(body: fixture('order.json'),
141
+ headers: {content_type: 'application/json; charset=utf-8'})
142
+ end
143
+
144
+ it 'requests the right resource' do
145
+ @client.delete_order('123')
146
+ expect(a_delete('/v2/transactions/orders/123')).to have_been_made
147
+ end
148
+
149
+ it 'returns the deleted order' do
150
+ order = @client.delete_order('123')
151
+ expect(order).to be_an Taxjar::Order
152
+ expect(order.transaction_id).to eq(123)
153
+ end
154
+ end
155
+
156
+ end