invoiced 0.2.0 → 0.3.2

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: 0f8a46e7387b20f707e7e5635d73f98e82c854c8
4
- data.tar.gz: ad3d8c0b95ac5161bd2b3685b53046983080c67f
3
+ metadata.gz: f8645f3555b1e4518b8f2e37f00500d2346d4a05
4
+ data.tar.gz: 29e3b4c7d5928436be985d562d56d31896ec3e98
5
5
  SHA512:
6
- metadata.gz: 1a9e191a39b17b162d6dff0e2b1b0377c2f4fe93070e07415cdd734384f5b98199bfd36311f7a78c41a51202714fd09e97c9be4851f37d7b002de0bd058b2e61
7
- data.tar.gz: 9ff6981193c43fe25e3550f737b19e782fba9e1dd18ee77c078091d0603e4785ff90bc161816de3525c3970a399426f4a9a44d8f99c26fed5664f1b96aabc4a8
6
+ metadata.gz: ec193889670371a0eede342cd061ccb15be1a850aa82d7ce377eced3b5885addd86dba1566677c47927586c60547c21a6fc4407ba7f48da7c8372f70125243b4
7
+ data.tar.gz: 1e78637e8ff80a7663d8f40c5802808e6520691355b7258b2fbae6fbeb01814f992391a39d25ed7b96594291939d71c256f42f97cf9ae6367288caf97ad15f21
@@ -6,7 +6,7 @@ module Invoiced
6
6
  include Invoiced::Operations::Delete
7
7
 
8
8
  def send_statement(opts={})
9
- response = @client.request(:post, "#{@endpoint}/emails", opts)
9
+ response = @client.request(:post, "#{self.endpoint()}/emails", opts)
10
10
 
11
11
  # build email objects
12
12
  email = Email.new(@client)
@@ -14,13 +14,13 @@ module Invoiced
14
14
  end
15
15
 
16
16
  def balance
17
- response = @client.request(:get, "#{@endpoint}/balance")
17
+ response = @client.request(:get, "#{self.endpoint()}/balance")
18
18
 
19
19
  response[:body]
20
20
  end
21
21
 
22
22
  def subscriptions(opts={})
23
- response = @client.request(:get, "#{@endpoint}/subscriptions", opts)
23
+ response = @client.request(:get, "#{self.endpoint()}/subscriptions", opts)
24
24
 
25
25
  # build objects
26
26
  subscription = Subscription.new(@client)
@@ -31,5 +31,18 @@ module Invoiced
31
31
 
32
32
  return subscriptions, metadata
33
33
  end
34
+
35
+ def line_items(opts={})
36
+ line = LineItem.new(@client)
37
+ line.set_endpoint_base(self.endpoint())
38
+ end
39
+
40
+ def invoice(opts={})
41
+ response = @client.request(:post, "#{self.endpoint()}/invoices", opts)
42
+
43
+ # build invoice object
44
+ invoice = Invoice.new(@client)
45
+ Util.convert_to_object(invoice, response[:body])
46
+ end
34
47
  end
35
48
  end
@@ -6,7 +6,7 @@ module Invoiced
6
6
  include Invoiced::Operations::Delete
7
7
 
8
8
  def send(opts={})
9
- response = @client.request(:post, "#{@endpoint}/emails", opts)
9
+ response = @client.request(:post, "#{self.endpoint()}/emails", opts)
10
10
 
11
11
  # build email objects
12
12
  email = Email.new(@client)
@@ -14,7 +14,7 @@ module Invoiced
14
14
  end
15
15
 
16
16
  def pay
17
- response = @client.request(:post, "#{@endpoint}/pay")
17
+ response = @client.request(:post, "#{self.endpoint()}/pay")
18
18
 
19
19
  # update the local values with the response
20
20
  refresh_from(response[:body].dup.merge({:id => self.id}))
@@ -0,0 +1,8 @@
1
+ module Invoiced
2
+ class LineItem < Object
3
+ include Invoiced::Operations::List
4
+ include Invoiced::Operations::Create
5
+ include Invoiced::Operations::Update
6
+ include Invoiced::Operations::Delete
7
+ end
8
+ end
@@ -9,6 +9,7 @@ module Invoiced
9
9
  def initialize(client, id=nil, values={})
10
10
  @client = client
11
11
  class_name = self.class.name.split('::').last
12
+ @endpoint_base = ''
12
13
  @endpoint = '/' + class_name.underscore.pluralize.downcase
13
14
 
14
15
  @id = id
@@ -21,14 +22,27 @@ module Invoiced
21
22
  end
22
23
  end
23
24
 
25
+ def set_endpoint_base(base)
26
+ @endpoint_base = base
27
+ self
28
+ end
29
+
30
+ def endpoint_base()
31
+ @endpoint_base
32
+ end
33
+
34
+ def endpoint()
35
+ @endpoint_base + @endpoint
36
+ end
37
+
24
38
  def retrieve(id, opts={})
25
39
  if !id
26
40
  raise ArgumentError.new("Missing ID.")
27
41
  end
28
42
 
29
- response = @client.request(:get, "#{@endpoint}/#{id}", opts)
43
+ response = @client.request(:get, "#{self.endpoint()}/#{id}", opts)
30
44
 
31
- self.class.new(@client, id, response[:body])
45
+ Util.convert_to_object(self, response[:body])
32
46
  end
33
47
 
34
48
  def load(opts={})
@@ -2,7 +2,7 @@ module Invoiced
2
2
  module Operations
3
3
  module Create
4
4
  def create(body={})
5
- response = @client.request(:post, @endpoint, body)
5
+ response = @client.request(:post, self.endpoint(), body)
6
6
 
7
7
  Util.convert_to_object(self, response[:body])
8
8
  end
@@ -2,7 +2,7 @@ module Invoiced
2
2
  module Operations
3
3
  module Delete
4
4
  def delete
5
- response = @client.request(:delete, @endpoint)
5
+ response = @client.request(:delete, self.endpoint())
6
6
 
7
7
  if response[:code] == 204
8
8
  refresh_from({:id => @id})
@@ -2,7 +2,7 @@ module Invoiced
2
2
  module Operations
3
3
  module List
4
4
  def list(opts={})
5
- response = @client.request(:get, @endpoint, opts)
5
+ response = @client.request(:get, self.endpoint(), opts)
6
6
 
7
7
  # build objects
8
8
  objects = Util.build_objects(self, response[:body])
@@ -12,7 +12,7 @@ module Invoiced
12
12
 
13
13
  # perform the update if there are any changes
14
14
  if update.length > 0
15
- response = @client.request(:patch, @endpoint, update)
15
+ response = @client.request(:patch, self.endpoint(), update)
16
16
 
17
17
  # update the local values with the response
18
18
  refresh_from(response[:body].dup.merge({:id => self.id}))
@@ -6,7 +6,7 @@ module Invoiced
6
6
  include Invoiced::Operations::Delete
7
7
 
8
8
  def send(opts={})
9
- response = @client.request(:post, "#{@endpoint}/emails", opts)
9
+ response = @client.request(:post, "#{self.endpoint()}/emails", opts)
10
10
 
11
11
  # build email objects
12
12
  email = Email.new(@client)
@@ -14,7 +14,7 @@ module Invoiced
14
14
  end
15
15
 
16
16
  def refund(opts={})
17
- response = @client.request(:post, "#{@endpoint}/refunds", opts)
17
+ response = @client.request(:post, "#{self.endpoint()}/refunds", opts)
18
18
 
19
19
  Util.convert_to_object(self, response[:body])
20
20
  end
data/lib/invoiced/util.rb CHANGED
@@ -16,8 +16,9 @@ module Invoiced
16
16
  }
17
17
  end
18
18
 
19
- def convert_to_object(_class, object)
20
- _class.class.new(_class.client, object[:id], object)
19
+ def convert_to_object(_class, values)
20
+ object = _class.class.new(_class.client, values[:id], values)
21
+ object.set_endpoint_base(_class.endpoint_base())
21
22
  end
22
23
 
23
24
  private
@@ -1,3 +1,3 @@
1
1
  module Invoiced
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.2'
3
3
  end
data/lib/invoiced.rb CHANGED
@@ -20,6 +20,7 @@ require 'invoiced/object'
20
20
  require 'invoiced/customer'
21
21
  require 'invoiced/email'
22
22
  require 'invoiced/invoice'
23
+ require 'invoiced/line_item'
23
24
  require 'invoiced/transaction'
24
25
  require 'invoiced/subscription'
25
26
 
@@ -2,6 +2,11 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Invoiced
4
4
  class CustomerTest < Test::Unit::TestCase
5
+ should "return the api endpoint" do
6
+ customer = Customer.new(@client, 123)
7
+ assert_equal('/customers/123', customer.endpoint())
8
+ end
9
+
5
10
  should "create a customer" do
6
11
  mockResponse = mock('RestClient::Response')
7
12
  mockResponse.stubs(:code).returns(201)
@@ -20,7 +25,7 @@ module Invoiced
20
25
  should "retrieve a customer" do
21
26
  mockResponse = mock('RestClient::Response')
22
27
  mockResponse.stubs(:code).returns(200)
23
- mockResponse.stubs(:body).returns('{"id":"123","name":"Pied Piper"}')
28
+ mockResponse.stubs(:body).returns('{"id":123,"name":"Pied Piper"}')
24
29
  mockResponse.stubs(:headers).returns({})
25
30
 
26
31
  RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
@@ -137,5 +142,74 @@ module Invoiced
137
142
  assert_instance_of(Invoiced::List, metadata)
138
143
  assert_equal(10, metadata.total_count)
139
144
  end
145
+
146
+ should "create a pending line item" do
147
+ mockResponse = mock('RestClient::Response')
148
+ mockResponse.stubs(:code).returns(201)
149
+ mockResponse.stubs(:body).returns('{"id":123,"unit_cost":500}')
150
+ mockResponse.stubs(:headers).returns({})
151
+
152
+ RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
153
+
154
+ customer = Customer.new(@client, 456)
155
+ line_item = customer.line_items.create({:unit_cost => 500})
156
+
157
+ assert_instance_of(Invoiced::LineItem, line_item)
158
+ assert_equal(123, line_item.id)
159
+ assert_equal(500, line_item.unit_cost)
160
+ assert_equal('/customers/456/line_items/123', line_item.endpoint())
161
+ end
162
+
163
+ should "list all of the customer's pending line item" do
164
+ mockResponse = mock('RestClient::Response')
165
+ mockResponse.stubs(:code).returns(200)
166
+ mockResponse.stubs(:body).returns('[{"id":123,"unit_cost":500}]')
167
+ mockResponse.stubs(:headers).returns(:x_total_count => 10, :link => '<https://api.invoiced.com/customers/123/line_items?per_page=25&page=1>; rel="self", <https://api.invoiced.com/customers/123/line_items?per_page=25&page=1>; rel="first", <https://api.invoiced.com/customers/123/line_items?per_page=25&page=1>; rel="last"')
168
+
169
+ RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
170
+
171
+ customer = Customer.new(@client, 456)
172
+ line_items, metadata = customer.line_items.list
173
+
174
+ assert_instance_of(Array, line_items)
175
+ assert_equal(1, line_items.length)
176
+ assert_equal(123, line_items[0].id)
177
+ assert_equal('/customers/456/line_items/123', line_items[0].endpoint())
178
+
179
+ assert_instance_of(Invoiced::List, metadata)
180
+ assert_equal(10, metadata.total_count)
181
+ end
182
+
183
+ should "retrieve a pending line item" do
184
+ mockResponse = mock('RestClient::Response')
185
+ mockResponse.stubs(:code).returns(200)
186
+ mockResponse.stubs(:body).returns('{"id":123,"unit_cost":500}')
187
+ mockResponse.stubs(:headers).returns({})
188
+
189
+ RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
190
+
191
+ customer = Customer.new(@client, 456)
192
+ line_item = customer.line_items.retrieve(123)
193
+
194
+ assert_instance_of(Invoiced::LineItem, line_item)
195
+ assert_equal(123, line_item.id)
196
+ assert_equal(500, line_item.unit_cost)
197
+ assert_equal('/customers/456/line_items/123', line_item.endpoint())
198
+ end
199
+
200
+ should "create an invoice" do
201
+ mockResponse = mock('RestClient::Response')
202
+ mockResponse.stubs(:code).returns(201)
203
+ mockResponse.stubs(:body).returns('{"id":4567,"total":100}')
204
+ mockResponse.stubs(:headers).returns({})
205
+
206
+ RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
207
+
208
+ customer = Customer.new(@client, 123)
209
+ invoice = customer.invoice
210
+
211
+ assert_instance_of(Invoiced::Invoice, invoice)
212
+ assert_equal(4567, invoice.id)
213
+ end
140
214
  end
141
215
  end
@@ -2,6 +2,11 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Invoiced
4
4
  class InvoiceTest < Test::Unit::TestCase
5
+ should "return the api endpoint" do
6
+ invoice = Invoice.new(@client, 123)
7
+ assert_equal('/invoices/123', invoice.endpoint())
8
+ end
9
+
5
10
  should "create an invoice" do
6
11
  mockResponse = mock('RestClient::Response')
7
12
  mockResponse.stubs(:code).returns(201)
@@ -0,0 +1,93 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Invoiced
4
+ class LineItemTest < Test::Unit::TestCase
5
+ should "return the api endpoint" do
6
+ line = LineItem.new(@client, 123)
7
+ assert_equal('/line_items/123', line.endpoint())
8
+ end
9
+
10
+ should "create a line item" do
11
+ mockResponse = mock('RestClient::Response')
12
+ mockResponse.stubs(:code).returns(201)
13
+ mockResponse.stubs(:body).returns('{"id":123,"unit_cost":500}')
14
+ mockResponse.stubs(:headers).returns({})
15
+
16
+ RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
17
+
18
+ line = LineItem.new(@client)
19
+ line_item = line.create({:unit_cost => 500})
20
+
21
+ assert_instance_of(Invoiced::LineItem, line_item)
22
+ assert_equal(123, line_item.id)
23
+ assert_equal(500, line_item.unit_cost)
24
+ end
25
+
26
+ should "retrieve a line item" do
27
+ mockResponse = mock('RestClient::Response')
28
+ mockResponse.stubs(:code).returns(200)
29
+ mockResponse.stubs(:body).returns('{"id":123,"unit_cost":500}')
30
+ mockResponse.stubs(:headers).returns({})
31
+
32
+ RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
33
+
34
+ line = LineItem.new(@client)
35
+ line_item = line.retrieve(123)
36
+
37
+ assert_instance_of(Invoiced::LineItem, line_item)
38
+ assert_equal(123, line_item.id)
39
+ assert_equal(500, line_item.unit_cost)
40
+ end
41
+
42
+ should "not update a line item when no params" do
43
+ line_item = LineItem.new(@client, 123)
44
+ assert_false(line_item.save)
45
+ end
46
+
47
+ should "update a line item" do
48
+ mockResponse = mock('RestClient::Response')
49
+ mockResponse.stubs(:code).returns(200)
50
+ mockResponse.stubs(:body).returns('{"id":123,"unit_cost":400}')
51
+ mockResponse.stubs(:headers).returns({})
52
+
53
+ RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
54
+
55
+ line_item = LineItem.new(@client, 123)
56
+ line_item.unit_cost = 400
57
+ assert_true(line_item.save)
58
+
59
+ assert_equal(400, line_item.unit_cost)
60
+ end
61
+
62
+ should "list all line items" do
63
+ mockResponse = mock('RestClient::Response')
64
+ mockResponse.stubs(:code).returns(200)
65
+ mockResponse.stubs(:body).returns('[{"id":123,"unit_cost":500}]')
66
+ mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '<https://api.invoiced.com/line_items?per_page=25&page=1>; rel="self", <https://api.invoiced.com/line_items?per_page=25&page=1>; rel="first", <https://api.invoiced.com/line_items?per_page=25&page=1>; rel="last"')
67
+
68
+ RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
69
+
70
+ line_item = LineItem.new(@client)
71
+ line_items, metadata = line_item.list
72
+
73
+ assert_instance_of(Array, line_items)
74
+ assert_equal(1, line_items.length)
75
+ assert_equal(123, line_items[0].id)
76
+
77
+ assert_instance_of(Invoiced::List, metadata)
78
+ assert_equal(15, metadata.total_count)
79
+ end
80
+
81
+ should "delete a line item" do
82
+ mockResponse = mock('RestClient::Response')
83
+ mockResponse.stubs(:code).returns(204)
84
+ mockResponse.stubs(:body).returns('')
85
+ mockResponse.stubs(:headers).returns({})
86
+
87
+ RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
88
+
89
+ line_item = LineItem.new(@client, 123)
90
+ assert_true(line_item.delete)
91
+ end
92
+ end
93
+ end
@@ -18,6 +18,15 @@ module Invoiced
18
18
  end
19
19
  end
20
20
 
21
+ should "return the api endpoint" do
22
+ object = Object.new(@client, 123)
23
+ assert_equal('/objects/123', object.endpoint())
24
+
25
+ object.set_endpoint_base('/blah')
26
+ assert_equal('/blah', object.endpoint_base())
27
+ assert_equal('/blah/objects/123', object.endpoint())
28
+ end
29
+
21
30
  should "throw exception when retrieving with no id" do
22
31
  assert_raise ArgumentError do
23
32
  client = Client.new "test"
@@ -2,6 +2,11 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Invoiced
4
4
  class SubscriptionTest < Test::Unit::TestCase
5
+ should "return the api endpoint" do
6
+ subscription = Subscription.new(@client, 123)
7
+ assert_equal('/subscriptions/123', subscription.endpoint())
8
+ end
9
+
5
10
  should "create a subscription" do
6
11
  mockResponse = mock('RestClient::Response')
7
12
  mockResponse.stubs(:code).returns(201)
@@ -2,6 +2,11 @@ require File.expand_path('../../test_helper', __FILE__)
2
2
 
3
3
  module Invoiced
4
4
  class TransactionTest < Test::Unit::TestCase
5
+ should "return the api endpoint" do
6
+ transaction = Transaction.new(@client, 123)
7
+ assert_equal('/transactions/123', transaction.endpoint())
8
+ end
9
+
5
10
  should "create a transaction" do
6
11
  mockResponse = mock('RestClient::Response')
7
12
  mockResponse.stubs(:code).returns(201)
data/test/test_helper.rb CHANGED
@@ -2,10 +2,10 @@ require 'simplecov'
2
2
  require 'simplecov-console'
3
3
  require 'coveralls'
4
4
 
5
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
6
6
  SimpleCov::Formatter::Console,
7
7
  Coveralls::SimpleCov::Formatter
8
- ]
8
+ ])
9
9
  SimpleCov.start do
10
10
  add_filter 'test/'
11
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: invoiced
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jared King
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-26 00:00:00.000000000 Z
11
+ date: 2016-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -130,6 +130,7 @@ files:
130
130
  - lib/invoiced/error/error_base.rb
131
131
  - lib/invoiced/error/invalid_request.rb
132
132
  - lib/invoiced/invoice.rb
133
+ - lib/invoiced/line_item.rb
133
134
  - lib/invoiced/list.rb
134
135
  - lib/invoiced/object.rb
135
136
  - lib/invoiced/operations/create.rb
@@ -140,12 +141,13 @@ files:
140
141
  - lib/invoiced/transaction.rb
141
142
  - lib/invoiced/util.rb
142
143
  - lib/invoiced/version.rb
143
- - test/invoiced/SubscriptionTest.rb
144
144
  - test/invoiced/customer_test.rb
145
145
  - test/invoiced/error_test.rb
146
146
  - test/invoiced/invoice_test.rb
147
147
  - test/invoiced/invoiced_test.rb
148
+ - test/invoiced/line_item_test.rb
148
149
  - test/invoiced/object_test.rb
150
+ - test/invoiced/subscription_test.rb
149
151
  - test/invoiced/transaction_test.rb
150
152
  - test/invoiced/util_test.rb
151
153
  - test/test_helper.rb
@@ -174,12 +176,13 @@ signing_key:
174
176
  specification_version: 4
175
177
  summary: Ruby client library for the Invoiced API
176
178
  test_files:
177
- - test/invoiced/SubscriptionTest.rb
178
179
  - test/invoiced/customer_test.rb
179
180
  - test/invoiced/error_test.rb
180
181
  - test/invoiced/invoice_test.rb
181
182
  - test/invoiced/invoiced_test.rb
183
+ - test/invoiced/line_item_test.rb
182
184
  - test/invoiced/object_test.rb
185
+ - test/invoiced/subscription_test.rb
183
186
  - test/invoiced/transaction_test.rb
184
187
  - test/invoiced/util_test.rb
185
188
  - test/test_helper.rb