invoiced 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/Gemfile +4 -1
- data/README.md +3 -0
- data/lib/invoiced/error/api_connection_error.rb +5 -0
- data/lib/invoiced/invoice.rb +1 -1
- data/lib/invoiced/object.rb +21 -5
- data/lib/invoiced/operations/delete.rb +3 -1
- data/lib/invoiced/operations/update.rb +3 -2
- data/lib/invoiced/util.rb +1 -7
- data/lib/invoiced/version.rb +1 -1
- data/lib/invoiced.rb +17 -33
- data/test/invoiced/customer_test.rb +113 -15
- data/test/invoiced/error_test.rb +10 -0
- data/test/invoiced/invoice_test.rb +90 -12
- data/test/invoiced/invoiced_test.rb +146 -3
- data/test/invoiced/object_test.rb +88 -0
- data/test/invoiced/transaction_test.rb +91 -12
- data/test/invoiced/util_test.rb +11 -3
- data/test/test_helper.rb +12 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d309d5779157c8855a9715ee6fcab533e115c882
|
4
|
+
data.tar.gz: a68bb49eb9f049e6c8fccdcc45e9c4b1f606e345
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbb3a5fd43352b3926ab8250b55d85330a702d2a28cd9063c8dfe79e8db12f3ee08cd57752bb01c9e05e1c05371cda8aedfb6afb97144148cce7c3c39e6f0954
|
7
|
+
data.tar.gz: 79f215a059b3918b226a150ef3c88cd4b0c278b93842d1116aee67960f1f64e282976e4292be0ae4af2b556f768170797d69b6b20c51124d1c1f03bc7d8bca75
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -3,4 +3,7 @@ gemspec
|
|
3
3
|
|
4
4
|
gem 'json', '~> 1.8.3'
|
5
5
|
gem 'rest-client', '~> 1.8.0'
|
6
|
-
gem 'activesupport', '~> 4.2.3'
|
6
|
+
gem 'activesupport', '~> 4.2.3'
|
7
|
+
gem 'coveralls', :require => false, :group => :test
|
8
|
+
gem 'simplecov', :require => false, :group => :test
|
9
|
+
gem 'simplecov-console', :require => false, :group => :test
|
data/README.md
CHANGED
@@ -3,6 +3,9 @@ invoiced-ruby
|
|
3
3
|
|
4
4
|
This repository contains the Ruby client library for the [Invoiced](https://invoiced.com) API.
|
5
5
|
|
6
|
+
[](https://travis-ci.org/Invoiced/invoiced-ruby)
|
7
|
+
[](https://coveralls.io/github/Invoiced/invoiced-ruby?branch=master)
|
8
|
+
|
6
9
|
## Installing
|
7
10
|
|
8
11
|
The Invoiced gem can be installed liked this:
|
data/lib/invoiced/invoice.rb
CHANGED
@@ -17,7 +17,7 @@ module Invoiced
|
|
17
17
|
response = @client.request(:post, "#{@endpoint}/pay")
|
18
18
|
|
19
19
|
# update the local values with the response
|
20
|
-
|
20
|
+
refresh_from(response[:body].dup.merge({:id => self.id}))
|
21
21
|
|
22
22
|
return response[:code] == 200
|
23
23
|
end
|
data/lib/invoiced/object.rb
CHANGED
@@ -16,8 +16,8 @@ module Invoiced
|
|
16
16
|
|
17
17
|
if !id.nil?
|
18
18
|
@endpoint += "/#{id}"
|
19
|
-
@values = values.dup.merge({:id => id})
|
20
19
|
@unsaved = Set.new
|
20
|
+
refresh_from(values.dup.merge({:id => id}))
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -43,6 +43,26 @@ module Invoiced
|
|
43
43
|
"#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@values)
|
44
44
|
end
|
45
45
|
|
46
|
+
def refresh_from(values)
|
47
|
+
removed = Set.new(@values.keys - values.keys)
|
48
|
+
added = Set.new(values.keys - @values.keys)
|
49
|
+
|
50
|
+
instance_eval do
|
51
|
+
remove_accessors(removed)
|
52
|
+
add_accessors(added)
|
53
|
+
end
|
54
|
+
removed.each do |k|
|
55
|
+
@values.delete(k)
|
56
|
+
@unsaved.delete(k)
|
57
|
+
end
|
58
|
+
values.each do |k, v|
|
59
|
+
@values[k] = v
|
60
|
+
@unsaved.delete(k)
|
61
|
+
end
|
62
|
+
|
63
|
+
return self
|
64
|
+
end
|
65
|
+
|
46
66
|
def [](k)
|
47
67
|
@values[k.to_sym]
|
48
68
|
end
|
@@ -63,10 +83,6 @@ module Invoiced
|
|
63
83
|
JSON.generate(@values)
|
64
84
|
end
|
65
85
|
|
66
|
-
def as_json(*a)
|
67
|
-
@values.as_json(*a)
|
68
|
-
end
|
69
|
-
|
70
86
|
def to_hash
|
71
87
|
@values.inject({}) do |acc, (key, value)|
|
72
88
|
acc[key] = value.respond_to?(:to_hash) ? value.to_hash : value
|
@@ -15,11 +15,12 @@ module Invoiced
|
|
15
15
|
response = @client.request(:patch, @endpoint, update)
|
16
16
|
|
17
17
|
# update the local values with the response
|
18
|
-
|
19
|
-
@unsaved = Set.new
|
18
|
+
refresh_from(response[:body].dup.merge({:id => self.id}))
|
20
19
|
|
21
20
|
return response[:code] == 200
|
22
21
|
end
|
22
|
+
|
23
|
+
false
|
23
24
|
end
|
24
25
|
end
|
25
26
|
end
|
data/lib/invoiced/util.rb
CHANGED
@@ -23,13 +23,7 @@ module Invoiced
|
|
23
23
|
private
|
24
24
|
|
25
25
|
def url_encode(params)
|
26
|
-
|
27
|
-
params.map {
|
28
|
-
|k,v| "#{k}=#{uri_encode(v)}"
|
29
|
-
}.join('&')
|
30
|
-
else
|
31
|
-
URI.escape(params.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
32
|
-
end
|
26
|
+
URI.escape(params.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
33
27
|
end
|
34
28
|
|
35
29
|
def flatten_params(params, parent_key=nil)
|
data/lib/invoiced/version.rb
CHANGED
data/lib/invoiced.rb
CHANGED
@@ -6,6 +6,7 @@ require 'active_support/inflector'
|
|
6
6
|
require 'invoiced/version'
|
7
7
|
require 'invoiced/util'
|
8
8
|
require 'invoiced/error/error_base'
|
9
|
+
require 'invoiced/error/api_connection_error'
|
9
10
|
require 'invoiced/error/api_error'
|
10
11
|
require 'invoiced/error/authentication_error'
|
11
12
|
require 'invoiced/error/invalid_request'
|
@@ -28,29 +29,15 @@ module Invoiced
|
|
28
29
|
ApiBase = 'https://api.invoiced.com'
|
29
30
|
|
30
31
|
attr_reader :api_key
|
32
|
+
attr_reader :Customer, :Invoice, :Transaction, :Plan, :Subscription
|
31
33
|
|
32
34
|
def initialize(api_key)
|
33
35
|
@api_key = api_key
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
def Invoice
|
41
|
-
Invoiced::Invoice.new(self)
|
42
|
-
end
|
43
|
-
|
44
|
-
def Transaction
|
45
|
-
Invoiced::Transaction.new(self)
|
46
|
-
end
|
47
|
-
|
48
|
-
def Plan
|
49
|
-
Invoiced::Plan.new(self)
|
50
|
-
end
|
51
|
-
|
52
|
-
def Subscription
|
53
|
-
Invoiced::Subscription.new(self)
|
36
|
+
@Customer = Invoiced::Customer.new(self)
|
37
|
+
@Invoice = Invoiced::Invoice.new(self)
|
38
|
+
@Transaction = Invoiced::Transaction.new(self)
|
39
|
+
@Plan = Invoiced::Plan.new(self)
|
40
|
+
@Subscription = Invoiced::Subscription.new(self)
|
54
41
|
end
|
55
42
|
|
56
43
|
def request(method, endpoint, params={})
|
@@ -69,17 +56,14 @@ module Invoiced
|
|
69
56
|
|
70
57
|
begin
|
71
58
|
response = RestClient::Request.execute(
|
72
|
-
method
|
73
|
-
url
|
74
|
-
headers
|
59
|
+
:method => method,
|
60
|
+
:url => url,
|
61
|
+
:headers => {
|
75
62
|
:authorization => Util.auth_header(@api_key),
|
76
63
|
:content_type => "application/json",
|
77
|
-
:user_agent => "Invoiced Ruby/#{Invoiced::VERSION}"
|
78
|
-
# pass in query parameters here due
|
79
|
-
# to an eccentricity in the rest-client gem
|
80
|
-
# :params => params.merge({:envelope => '0'})
|
64
|
+
:user_agent => "Invoiced Ruby/#{Invoiced::VERSION}"
|
81
65
|
},
|
82
|
-
payload
|
66
|
+
:payload => payload
|
83
67
|
)
|
84
68
|
rescue RestClient::Exception => e
|
85
69
|
if e.response
|
@@ -126,23 +110,23 @@ module Invoiced
|
|
126
110
|
end
|
127
111
|
|
128
112
|
def rescue_rest_client_error(error)
|
129
|
-
raise
|
113
|
+
raise ApiConnectionError.new("There was an error connecting to Invoiced.")
|
130
114
|
end
|
131
115
|
|
132
116
|
def authentication_error(error, response)
|
133
|
-
|
117
|
+
AuthenticationError.new(error["message"], response.code, error)
|
134
118
|
end
|
135
119
|
|
136
120
|
def invalid_request_error(error, response)
|
137
|
-
|
121
|
+
InvalidRequestError.new(error["message"], response.code, error)
|
138
122
|
end
|
139
123
|
|
140
124
|
def api_error(error, response)
|
141
|
-
|
125
|
+
ApiError.new(error["message"], response.code, error)
|
142
126
|
end
|
143
127
|
|
144
128
|
def general_api_error(code, body)
|
145
|
-
|
129
|
+
ApiError.new("API Error #{code} - #{body}", code)
|
146
130
|
end
|
147
131
|
end
|
148
132
|
end
|
@@ -3,40 +3,138 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
3
3
|
module Invoiced
|
4
4
|
class CustomerTest < Test::Unit::TestCase
|
5
5
|
should "create a customer" do
|
6
|
-
|
7
|
-
|
6
|
+
mockResponse = mock('RestClient::Response')
|
7
|
+
mockResponse.stubs(:code).returns(201)
|
8
|
+
mockResponse.stubs(:body).returns('{"id":123,"name":"Pied Piper"}')
|
9
|
+
mockResponse.stubs(:headers).returns({})
|
10
|
+
|
11
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
8
12
|
|
9
|
-
|
10
|
-
|
11
|
-
|
13
|
+
customer = @client.Customer.create({:name => "Pied Piper"})
|
14
|
+
|
15
|
+
assert_instance_of(Invoiced::Customer, customer)
|
16
|
+
assert_equal(123, customer.id)
|
17
|
+
assert_equal('Pied Piper', customer.name)
|
18
|
+
end
|
12
19
|
|
13
20
|
should "retrieve a customer" do
|
14
|
-
|
21
|
+
mockResponse = mock('RestClient::Response')
|
22
|
+
mockResponse.stubs(:code).returns(200)
|
23
|
+
mockResponse.stubs(:body).returns('{"id":"123","name":"Pied Piper"}')
|
24
|
+
mockResponse.stubs(:headers).returns({})
|
25
|
+
|
26
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
27
|
+
|
28
|
+
customer = @client.Customer.retrieve(123)
|
29
|
+
|
30
|
+
assert_instance_of(Invoiced::Customer, customer)
|
31
|
+
assert_equal(123, customer.id)
|
32
|
+
assert_equal('Pied Piper', customer.name)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "not update a customer when no params" do
|
36
|
+
customer = Customer.new(@client, 123)
|
37
|
+
assert_false(customer.save)
|
15
38
|
end
|
16
39
|
|
17
40
|
should "update a customer" do
|
18
|
-
|
19
|
-
|
41
|
+
mockResponse = mock('RestClient::Response')
|
42
|
+
mockResponse.stubs(:code).returns(200)
|
43
|
+
mockResponse.stubs(:body).returns('{"id":123,"name":"Pied Piper","notes":"Terrible customer"}')
|
44
|
+
mockResponse.stubs(:headers).returns({})
|
45
|
+
|
46
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
47
|
+
|
48
|
+
customer = Customer.new(@client, 123)
|
49
|
+
customer.notes = 'Terrible customer'
|
20
50
|
assert_true(customer.save)
|
51
|
+
|
52
|
+
assert_equal("Terrible customer", customer.notes)
|
53
|
+
end
|
54
|
+
|
55
|
+
should "list all customers" do
|
56
|
+
mockResponse = mock('RestClient::Response')
|
57
|
+
mockResponse.stubs(:code).returns(200)
|
58
|
+
mockResponse.stubs(:body).returns('[{"id":123,"name":"Pied Piper"}]')
|
59
|
+
mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '<https://api.invoiced.com/customers?per_page=25&page=1>; rel="self", <https://api.invoiced.com/customers?per_page=25&page=1>; rel="first", <https://api.invoiced.com/customers?per_page=25&page=1>; rel="last"')
|
60
|
+
|
61
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
62
|
+
|
63
|
+
customers, metadata = @client.Customer.list
|
64
|
+
|
65
|
+
assert_instance_of(Array, customers)
|
66
|
+
assert_equal(1, customers.length)
|
67
|
+
assert_equal(123, customers[0].id)
|
68
|
+
|
69
|
+
assert_instance_of(Invoiced::List, metadata)
|
70
|
+
assert_equal(15, metadata.total_count)
|
71
|
+
end
|
72
|
+
|
73
|
+
should "delete a customer" do
|
74
|
+
mockResponse = mock('RestClient::Response')
|
75
|
+
mockResponse.stubs(:code).returns(204)
|
76
|
+
mockResponse.stubs(:body).returns('')
|
77
|
+
mockResponse.stubs(:headers).returns({})
|
78
|
+
|
79
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
80
|
+
|
81
|
+
customer = Customer.new(@client, 123)
|
82
|
+
assert_true(customer.delete)
|
21
83
|
end
|
22
84
|
|
23
85
|
should "send an account statement" do
|
24
|
-
|
86
|
+
mockResponse = mock('RestClient::Response')
|
87
|
+
mockResponse.stubs(:code).returns(201)
|
88
|
+
mockResponse.stubs(:body).returns('[{"id":4567,"email":"test@example.com"}]')
|
89
|
+
mockResponse.stubs(:headers).returns({})
|
90
|
+
|
91
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
92
|
+
|
93
|
+
customer = Customer.new(@client, 123)
|
25
94
|
emails = customer.send_statement
|
95
|
+
|
96
|
+
assert_instance_of(Array, emails)
|
97
|
+
assert_equal(1, emails.length)
|
98
|
+
assert_instance_of(Invoiced::Email, emails[0])
|
99
|
+
assert_equal(4567, emails[0].id)
|
26
100
|
end
|
27
101
|
|
28
102
|
should "retrieve a customer's balance" do
|
29
|
-
|
103
|
+
mockResponse = mock('RestClient::Response')
|
104
|
+
mockResponse.stubs(:code).returns(200)
|
105
|
+
mockResponse.stubs(:body).returns('{"total_outstanding":1000,"available_credits":0,"past_due":true}')
|
106
|
+
mockResponse.stubs(:headers).returns(:x_total_count => 10, :link => '<https://api.invoiced.com/customers/123/subscriptions?per_page=25&page=1>; rel="self", <https://api.invoiced.com/customers/123/subscriptions?per_page=25&page=1>; rel="first", <https://api.invoiced.com/customers/123/subscriptions?per_page=25&page=1>; rel="last"')
|
107
|
+
|
108
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
109
|
+
|
110
|
+
customer = Customer.new(@client, 123)
|
30
111
|
balance = customer.balance
|
112
|
+
|
113
|
+
expected = {
|
114
|
+
:past_due => true,
|
115
|
+
:available_credits => 0,
|
116
|
+
:total_outstanding => 1000
|
117
|
+
}
|
118
|
+
|
119
|
+
assert_equal(expected, balance)
|
31
120
|
end
|
32
121
|
|
33
122
|
should "list all of the customer's subscriptions" do
|
34
|
-
|
35
|
-
|
123
|
+
mockResponse = mock('RestClient::Response')
|
124
|
+
mockResponse.stubs(:code).returns(200)
|
125
|
+
mockResponse.stubs(:body).returns('[{"id":123,"plan":456}]')
|
126
|
+
mockResponse.stubs(:headers).returns(:x_total_count => 10, :link => '<https://api.invoiced.com/customers/123/subscriptions?per_page=25&page=1>; rel="self", <https://api.invoiced.com/customers/123/subscriptions?per_page=25&page=1>; rel="first", <https://api.invoiced.com/customers/123/subscriptions?per_page=25&page=1>; rel="last"')
|
36
127
|
|
37
|
-
|
38
|
-
|
39
|
-
|
128
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
129
|
+
|
130
|
+
subscriptions, metadata = @client.Customer.subscriptions
|
131
|
+
|
132
|
+
assert_instance_of(Array, subscriptions)
|
133
|
+
assert_equal(1, subscriptions.length)
|
134
|
+
assert_equal(123, subscriptions[0].id)
|
135
|
+
|
136
|
+
assert_instance_of(Invoiced::List, metadata)
|
137
|
+
assert_equal(10, metadata.total_count)
|
40
138
|
end
|
41
139
|
end
|
42
140
|
end
|
@@ -3,36 +3,114 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
3
3
|
module Invoiced
|
4
4
|
class InvoiceTest < Test::Unit::TestCase
|
5
5
|
should "create an invoice" do
|
6
|
-
|
7
|
-
|
6
|
+
mockResponse = mock('RestClient::Response')
|
7
|
+
mockResponse.stubs(:code).returns(201)
|
8
|
+
mockResponse.stubs(:body).returns('{"id":123,"number":"INV-0001"}')
|
9
|
+
mockResponse.stubs(:headers).returns({})
|
10
|
+
|
11
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
8
12
|
|
9
|
-
|
10
|
-
|
11
|
-
|
13
|
+
invoice = @client.Invoice.create({:number => "INV-0001"})
|
14
|
+
|
15
|
+
assert_instance_of(Invoiced::Invoice, invoice)
|
16
|
+
assert_equal(123, invoice.id)
|
17
|
+
assert_equal('INV-0001', invoice.number)
|
18
|
+
end
|
12
19
|
|
13
20
|
should "retrieve an invoice" do
|
14
|
-
|
21
|
+
mockResponse = mock('RestClient::Response')
|
22
|
+
mockResponse.stubs(:code).returns(200)
|
23
|
+
mockResponse.stubs(:body).returns('{"id":123,"number":"INV-0001"}')
|
24
|
+
mockResponse.stubs(:headers).returns({})
|
25
|
+
|
26
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
27
|
+
|
28
|
+
invoice = @client.Invoice.retrieve(123)
|
29
|
+
|
30
|
+
assert_instance_of(Invoiced::Invoice, invoice)
|
31
|
+
assert_equal(123, invoice.id)
|
32
|
+
assert_equal('INV-0001', invoice.number)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "not update an invoice when no params" do
|
36
|
+
invoice = Invoice.new(@client, 123)
|
37
|
+
assert_false(invoice.save)
|
15
38
|
end
|
16
39
|
|
17
40
|
should "update an invoice" do
|
18
|
-
|
19
|
-
|
41
|
+
mockResponse = mock('RestClient::Response')
|
42
|
+
mockResponse.stubs(:code).returns(200)
|
43
|
+
mockResponse.stubs(:body).returns('{"id":123,"closed":true}')
|
44
|
+
mockResponse.stubs(:headers).returns({})
|
45
|
+
|
46
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
47
|
+
|
48
|
+
invoice = Invoice.new(@client, 123)
|
49
|
+
invoice.closed = true
|
20
50
|
assert_true(invoice.save)
|
51
|
+
|
52
|
+
assert_true(invoice.closed)
|
53
|
+
end
|
54
|
+
|
55
|
+
should "list all invoices" do
|
56
|
+
mockResponse = mock('RestClient::Response')
|
57
|
+
mockResponse.stubs(:code).returns(200)
|
58
|
+
mockResponse.stubs(:body).returns('[{"id":123,"number":"INV-0001"}]')
|
59
|
+
mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '<https://api.invoiced.com/invoices?per_page=25&page=1>; rel="self", <https://api.invoiced.com/invoices?per_page=25&page=1>; rel="first", <https://api.invoiced.com/invoices?per_page=25&page=1>; rel="last"')
|
60
|
+
|
61
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
62
|
+
|
63
|
+
invoices, metadata = @client.Invoice.list
|
64
|
+
|
65
|
+
assert_instance_of(Array, invoices)
|
66
|
+
assert_equal(1, invoices.length)
|
67
|
+
assert_equal(123, invoices[0].id)
|
68
|
+
|
69
|
+
assert_instance_of(Invoiced::List, metadata)
|
70
|
+
assert_equal(15, metadata.total_count)
|
71
|
+
end
|
72
|
+
|
73
|
+
should "delete an invoice" do
|
74
|
+
mockResponse = mock('RestClient::Response')
|
75
|
+
mockResponse.stubs(:code).returns(204)
|
76
|
+
mockResponse.stubs(:body).returns('')
|
77
|
+
mockResponse.stubs(:headers).returns({})
|
78
|
+
|
79
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
80
|
+
|
81
|
+
invoice = Invoice.new(@client, 123)
|
82
|
+
assert_true(invoice.delete)
|
21
83
|
end
|
22
84
|
|
23
85
|
should "send an invoice" do
|
86
|
+
mockResponse = mock('RestClient::Response')
|
87
|
+
mockResponse.stubs(:code).returns(201)
|
88
|
+
mockResponse.stubs(:body).returns('[{"id":4567,"email":"test@example.com"}]')
|
89
|
+
mockResponse.stubs(:headers).returns({})
|
90
|
+
|
91
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
92
|
+
|
24
93
|
invoice = Invoice.new(@client, 1234)
|
25
94
|
emails = invoice.send
|
95
|
+
|
96
|
+
assert_instance_of(Array, emails)
|
97
|
+
assert_equal(1, emails.length)
|
98
|
+
assert_instance_of(Invoiced::Email, emails[0])
|
99
|
+
assert_equal(4567, emails[0].id)
|
26
100
|
end
|
27
101
|
|
28
102
|
should "pay an invoice" do
|
103
|
+
mockResponse = mock('RestClient::Response')
|
104
|
+
mockResponse.stubs(:code).returns(200)
|
105
|
+
mockResponse.stubs(:body).returns('{"paid":true}')
|
106
|
+
mockResponse.stubs(:headers).returns({})
|
107
|
+
|
108
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
109
|
+
|
29
110
|
invoice = Invoice.new(@client, 1234)
|
30
111
|
assert_true(invoice.pay)
|
31
|
-
end
|
32
112
|
|
33
|
-
|
34
|
-
invoie = Invoice.new(@client, 1234)
|
35
|
-
assert_true(invoice.delete)
|
113
|
+
assert_true(invoice.paid)
|
36
114
|
end
|
37
115
|
end
|
38
116
|
end
|
@@ -10,15 +10,158 @@ module Invoiced
|
|
10
10
|
assert_equal('api_key', client.api_key)
|
11
11
|
end
|
12
12
|
|
13
|
-
should "perform a request" do
|
14
|
-
|
13
|
+
should "perform a get request" do
|
14
|
+
mockResponse = mock('RestClient::Response')
|
15
|
+
mockResponse.stubs(:code).returns(200)
|
16
|
+
mockResponse.stubs(:body).returns('{"test":true}')
|
17
|
+
mockResponse.stubs(:headers).returns(:Header => "test")
|
18
|
+
|
19
|
+
# not used
|
20
|
+
expectedParameters = {
|
21
|
+
:method => "GET",
|
22
|
+
:url => "https://api.invoiced.com/invoices?envelope=0&test=property&filter[levels]=work",
|
23
|
+
:headers => {
|
24
|
+
:authorization => "Basic dGVzdDo=",
|
25
|
+
:content_type => "application/json",
|
26
|
+
:user_agent => "Invoiced Ruby/#{Invoiced::VERSION}"
|
27
|
+
},
|
28
|
+
:payload => nil
|
29
|
+
}
|
30
|
+
|
31
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
32
|
+
|
33
|
+
client = Invoiced::Client.new('test')
|
15
34
|
params = {
|
16
35
|
"test" => "property",
|
17
36
|
"filter" => {
|
18
37
|
"levels" => "work"
|
19
38
|
}
|
20
39
|
}
|
21
|
-
client.request("GET", "/invoices", params)
|
40
|
+
response = client.request("GET", "/invoices", params)
|
41
|
+
|
42
|
+
expectedResponse = {
|
43
|
+
:code => 200,
|
44
|
+
:headers => {
|
45
|
+
:Header => "test"
|
46
|
+
},
|
47
|
+
:body => {
|
48
|
+
:test => true
|
49
|
+
}
|
50
|
+
}
|
51
|
+
assert_equal(expectedResponse, response)
|
22
52
|
end
|
53
|
+
|
54
|
+
should "perform a post request" do
|
55
|
+
mockResponse = mock('RestClient::Response')
|
56
|
+
mockResponse.stubs(:code).returns(204)
|
57
|
+
mockResponse.stubs(:headers).returns(:Header => "test")
|
58
|
+
|
59
|
+
# not used
|
60
|
+
expectedParameters = {
|
61
|
+
:method => "POST",
|
62
|
+
:url => "https://api.invoiced.com/invoices?envelope=0&test=property&filter[levels]=work",
|
63
|
+
:headers => {
|
64
|
+
:authorization => "Basic dGVzdDo=",
|
65
|
+
:content_type => "application/json",
|
66
|
+
:user_agent => "Invoiced Ruby/#{Invoiced::VERSION}"
|
67
|
+
},
|
68
|
+
:payload => '{"test":"property"}'
|
69
|
+
}
|
70
|
+
|
71
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
72
|
+
|
73
|
+
client = Invoiced::Client.new('test')
|
74
|
+
params = {
|
75
|
+
"test" => "property"
|
76
|
+
}
|
77
|
+
response = client.request("POST", "/invoices", params)
|
78
|
+
|
79
|
+
expectedResponse = {
|
80
|
+
:code => 204,
|
81
|
+
:headers => {
|
82
|
+
:Header => "test"
|
83
|
+
},
|
84
|
+
:body => nil
|
85
|
+
}
|
86
|
+
assert_equal(expectedResponse, response)
|
87
|
+
end
|
88
|
+
|
89
|
+
should "handle a request exception" do
|
90
|
+
RestClient::Request.any_instance.expects(:execute).raises(RestClient::Exception.new)
|
91
|
+
|
92
|
+
client = Invoiced::Client.new('test')
|
93
|
+
|
94
|
+
assert_raise Invoiced::ApiConnectionError do
|
95
|
+
client.request("POST", "/invoices")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
should "handle an invalid request error" do
|
100
|
+
mockResponse = mock('RestClient::Response')
|
101
|
+
mockResponse.stubs(:code).returns(400)
|
102
|
+
mockResponse.stubs(:body).returns('{"error":true}')
|
103
|
+
|
104
|
+
ex = RestClient::ExceptionWithResponse.new
|
105
|
+
ex.response = mockResponse
|
106
|
+
|
107
|
+
RestClient::Request.any_instance.expects(:execute).raises(ex)
|
108
|
+
|
109
|
+
client = Invoiced::Client.new('test')
|
110
|
+
|
111
|
+
assert_raise Invoiced::InvalidRequestError do
|
112
|
+
client.request("POST", "/invoices")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
should "handle an authentication error" do
|
117
|
+
mockResponse = mock('RestClient::Response')
|
118
|
+
mockResponse.stubs(:code).returns(401)
|
119
|
+
mockResponse.stubs(:body).returns('{"error":true}')
|
120
|
+
|
121
|
+
ex = RestClient::ExceptionWithResponse.new
|
122
|
+
ex.response = mockResponse
|
123
|
+
|
124
|
+
RestClient::Request.any_instance.expects(:execute).raises(ex)
|
125
|
+
|
126
|
+
client = Invoiced::Client.new('test')
|
127
|
+
|
128
|
+
assert_raise Invoiced::AuthenticationError do
|
129
|
+
client.request("POST", "/invoices")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
should "handle an api error" do
|
134
|
+
mockResponse = mock('RestClient::Response')
|
135
|
+
mockResponse.stubs(:code).returns(500)
|
136
|
+
mockResponse.stubs(:body).returns('{"error":true}')
|
137
|
+
|
138
|
+
ex = RestClient::ExceptionWithResponse.new
|
139
|
+
ex.response = mockResponse
|
140
|
+
|
141
|
+
RestClient::Request.any_instance.expects(:execute).raises(ex)
|
142
|
+
|
143
|
+
client = Invoiced::Client.new('test')
|
144
|
+
|
145
|
+
assert_raise Invoiced::ApiError do
|
146
|
+
client.request("POST", "/invoices")
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
should "handle an api error with invalid json" do
|
151
|
+
mockResponse = mock('RestClient::Response')
|
152
|
+
mockResponse.stubs(:code).returns(500)
|
153
|
+
mockResponse.stubs(:body).returns('not valid json')
|
154
|
+
|
155
|
+
ex = RestClient::ExceptionWithResponse.new
|
156
|
+
ex.response = mockResponse
|
157
|
+
|
158
|
+
RestClient::Request.any_instance.expects(:execute).raises(ex)
|
159
|
+
|
160
|
+
client = Invoiced::Client.new('test')
|
161
|
+
|
162
|
+
assert_raise Invoiced::ApiError do
|
163
|
+
client.request("POST", "/invoices")
|
164
|
+
end
|
165
|
+
end
|
23
166
|
end
|
24
167
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require File.expand_path('../../test_helper', __FILE__)
|
2
|
+
|
3
|
+
module Invoiced
|
4
|
+
class ObjectTest < Test::Unit::TestCase
|
5
|
+
should "create an invoiced object" do
|
6
|
+
client = Client.new("test")
|
7
|
+
|
8
|
+
object = Object.new(client, 123, {:name => "Pied Piper"})
|
9
|
+
|
10
|
+
assert_equal(client, object.client)
|
11
|
+
assert_equal("{\n \"name\": \"Pied Piper\",\n \"id\": 123\n}", object.to_s)
|
12
|
+
assert_equal("#<#{object.class}:0x#{object.object_id.to_s(16)} id=123> JSON: {\n \"name\": \"Pied Piper\",\n \"id\": 123\n}", object.inspect)
|
13
|
+
assert_equal("{\"name\":\"Pied Piper\",\"id\":123}", object.to_json)
|
14
|
+
assert_equal({:name => "Pied Piper", :id => 123}, object.to_hash)
|
15
|
+
|
16
|
+
object.each do |value|
|
17
|
+
# NOP
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
should "throw exception when retrieving with no id" do
|
22
|
+
assert_raise ArgumentError do
|
23
|
+
client = Client.new "test"
|
24
|
+
|
25
|
+
object = Object.new client
|
26
|
+
|
27
|
+
object.retrieve nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
should "add and remove accessors" do
|
32
|
+
client = Client.new("test")
|
33
|
+
|
34
|
+
object = Object.new(client, 123, {:name => "Pied Piper"})
|
35
|
+
|
36
|
+
assert_equal(123, object.id)
|
37
|
+
assert_equal("Pied Piper", object.name)
|
38
|
+
assert_equal("Pied Piper", object["name"])
|
39
|
+
|
40
|
+
object.name = "Renamed"
|
41
|
+
assert_equal("Renamed", object.name)
|
42
|
+
|
43
|
+
object["name"] = "Pied Piper"
|
44
|
+
assert_equal("Pied Piper", object["name"])
|
45
|
+
|
46
|
+
assert_equal([:name, :id], object.keys)
|
47
|
+
assert_equal(["Pied Piper", 123], object.values)
|
48
|
+
|
49
|
+
object.refresh_from({:id => 123, :notes => "...."})
|
50
|
+
|
51
|
+
# the name attribute should no longer be available
|
52
|
+
assert_equal("....", object.notes)
|
53
|
+
assert_raise NoMethodError do
|
54
|
+
object.name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
should "raise exception when setting missing attribute" do
|
59
|
+
client = Client.new("test")
|
60
|
+
|
61
|
+
object = Object.new(client, 123)
|
62
|
+
|
63
|
+
assert_raise NoMethodError do
|
64
|
+
object.non_existent_method
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
should "raise exception when setting empty string" do
|
69
|
+
client = Client.new("test")
|
70
|
+
|
71
|
+
object = Object.new(client, 123, {:name => "Pied Piper"})
|
72
|
+
|
73
|
+
assert_raise ArgumentError do
|
74
|
+
object.name = ""
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
should "raise exception when attempting to set permanent attribute" do
|
79
|
+
client = Client.new("test")
|
80
|
+
|
81
|
+
object = Object.new(client, 123)
|
82
|
+
|
83
|
+
assert_raise NoMethodError do
|
84
|
+
object.id = "fail"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -3,36 +3,115 @@ require File.expand_path('../../test_helper', __FILE__)
|
|
3
3
|
module Invoiced
|
4
4
|
class TransactionTest < Test::Unit::TestCase
|
5
5
|
should "create a transaction" do
|
6
|
-
|
7
|
-
|
6
|
+
mockResponse = mock('RestClient::Response')
|
7
|
+
mockResponse.stubs(:code).returns(201)
|
8
|
+
mockResponse.stubs(:body).returns('{"id":123,"amount":100}')
|
9
|
+
mockResponse.stubs(:headers).returns({})
|
10
|
+
|
11
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
8
12
|
|
9
|
-
|
10
|
-
|
11
|
-
|
13
|
+
transaction = @client.Transaction.create({:amount => 800})
|
14
|
+
|
15
|
+
assert_instance_of(Invoiced::Transaction, transaction)
|
16
|
+
assert_equal(123, transaction.id)
|
17
|
+
assert_equal(100, transaction.amount)
|
18
|
+
end
|
12
19
|
|
13
20
|
should "retrieve a transaction" do
|
14
|
-
|
21
|
+
mockResponse = mock('RestClient::Response')
|
22
|
+
mockResponse.stubs(:code).returns(200)
|
23
|
+
mockResponse.stubs(:body).returns('{"id":123,"amount":100}')
|
24
|
+
mockResponse.stubs(:headers).returns({})
|
25
|
+
|
26
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
27
|
+
|
28
|
+
transaction = @client.Transaction.retrieve(123)
|
29
|
+
|
30
|
+
assert_instance_of(Invoiced::Transaction, transaction)
|
31
|
+
assert_equal(123, transaction.id)
|
32
|
+
assert_equal(100, transaction.amount)
|
33
|
+
end
|
34
|
+
|
35
|
+
should "not update a transaction when no params" do
|
36
|
+
transaction = Transaction.new(@client, 123)
|
37
|
+
assert_false(transaction.save)
|
15
38
|
end
|
16
39
|
|
17
40
|
should "update a transaction" do
|
18
|
-
|
19
|
-
|
41
|
+
mockResponse = mock('RestClient::Response')
|
42
|
+
mockResponse.stubs(:code).returns(200)
|
43
|
+
mockResponse.stubs(:body).returns('{"id":123,"sent":true}')
|
44
|
+
mockResponse.stubs(:headers).returns({})
|
45
|
+
|
46
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
47
|
+
|
48
|
+
transaction = Transaction.new(@client, 123)
|
49
|
+
transaction.sent = true
|
20
50
|
assert_true(transaction.save)
|
51
|
+
|
52
|
+
assert_true(transaction.sent)
|
53
|
+
end
|
54
|
+
|
55
|
+
should "list all transactions" do
|
56
|
+
mockResponse = mock('RestClient::Response')
|
57
|
+
mockResponse.stubs(:code).returns(200)
|
58
|
+
mockResponse.stubs(:body).returns('[{"id":123,"amount":100}]')
|
59
|
+
mockResponse.stubs(:headers).returns(:x_total_count => 15, :link => '<https://api.invoiced.com/transactions?per_page=25&page=1>; rel="self", <https://api.invoiced.com/transactions?per_page=25&page=1>; rel="first", <https://api.invoiced.com/transactions?per_page=25&page=1>; rel="last"')
|
60
|
+
|
61
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
62
|
+
|
63
|
+
transactions, metadata = @client.Transaction.list
|
64
|
+
|
65
|
+
assert_instance_of(Array, transactions)
|
66
|
+
assert_equal(1, transactions.length)
|
67
|
+
assert_equal(123, transactions[0].id)
|
68
|
+
|
69
|
+
assert_instance_of(Invoiced::List, metadata)
|
70
|
+
assert_equal(15, metadata.total_count)
|
71
|
+
end
|
72
|
+
|
73
|
+
should "delete a transaction" do
|
74
|
+
mockResponse = mock('RestClient::Response')
|
75
|
+
mockResponse.stubs(:code).returns(204)
|
76
|
+
mockResponse.stubs(:body).returns('')
|
77
|
+
mockResponse.stubs(:headers).returns({})
|
78
|
+
|
79
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
80
|
+
|
81
|
+
transaction = Transaction.new(@client, 123)
|
82
|
+
assert_true(transaction.delete)
|
21
83
|
end
|
22
84
|
|
23
85
|
should "send a payment receipt" do
|
86
|
+
mockResponse = mock('RestClient::Response')
|
87
|
+
mockResponse.stubs(:code).returns(201)
|
88
|
+
mockResponse.stubs(:body).returns('[{"id":4567,"email":"test@example.com"}]')
|
89
|
+
mockResponse.stubs(:headers).returns({})
|
90
|
+
|
91
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
92
|
+
|
24
93
|
transaction = Transaction.new(@client, 1234)
|
25
94
|
emails = transaction.send
|
95
|
+
|
96
|
+
assert_instance_of(Array, emails)
|
97
|
+
assert_equal(1, emails.length)
|
98
|
+
assert_instance_of(Invoiced::Email, emails[0])
|
99
|
+
assert_equal(4567, emails[0].id)
|
26
100
|
end
|
27
101
|
|
28
102
|
should "refund a transaction" do
|
103
|
+
mockResponse = mock('RestClient::Response')
|
104
|
+
mockResponse.stubs(:code).returns(201)
|
105
|
+
mockResponse.stubs(:body).returns('{"id":456}')
|
106
|
+
mockResponse.stubs(:headers).returns({})
|
107
|
+
|
108
|
+
RestClient::Request.any_instance.expects(:execute).returns(mockResponse)
|
109
|
+
|
29
110
|
transaction = Transaction.new(@client, 1234)
|
30
111
|
refund = transaction.refund({:amount => 800})
|
31
|
-
end
|
32
112
|
|
33
|
-
|
34
|
-
|
35
|
-
assert_true(transaction.delete)
|
113
|
+
assert_instance_of(Invoiced::Transaction, refund)
|
114
|
+
assert_equal(456, refund.id)
|
36
115
|
end
|
37
116
|
end
|
38
117
|
end
|
data/test/invoiced/util_test.rb
CHANGED
@@ -10,11 +10,19 @@ module Invoiced
|
|
10
10
|
params = {
|
11
11
|
"test" => "property",
|
12
12
|
"filter" => {
|
13
|
-
"levels" => "work"
|
14
|
-
|
13
|
+
"levels" => "work",
|
14
|
+
"nesting" => {
|
15
|
+
"works" => true
|
16
|
+
}
|
17
|
+
},
|
18
|
+
"array" => [
|
19
|
+
"should",
|
20
|
+
{"also" => true},
|
21
|
+
["work"]
|
22
|
+
]
|
15
23
|
}
|
16
24
|
|
17
|
-
assert_equal("test=property&filter[levels]=work", Util.uri_encode(params))
|
25
|
+
assert_equal("test=property&filter[levels]=work&filter[nesting][works]=true&array[]=should&array[][also]=true&array[]=work", Util.uri_encode(params))
|
18
26
|
end
|
19
27
|
|
20
28
|
should "create a Customer object" do
|
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'simplecov-console'
|
3
|
+
require 'coveralls'
|
4
|
+
|
5
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
6
|
+
SimpleCov::Formatter::Console,
|
7
|
+
Coveralls::SimpleCov::Formatter
|
8
|
+
]
|
9
|
+
SimpleCov.start do
|
10
|
+
add_filter 'test/'
|
11
|
+
end
|
12
|
+
|
1
13
|
require 'invoiced'
|
2
14
|
require 'test/unit'
|
3
15
|
require 'mocha/setup'
|
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.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jared King
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -124,6 +124,7 @@ files:
|
|
124
124
|
- lib/invoiced.rb
|
125
125
|
- lib/invoiced/customer.rb
|
126
126
|
- lib/invoiced/email.rb
|
127
|
+
- lib/invoiced/error/api_connection_error.rb
|
127
128
|
- lib/invoiced/error/api_error.rb
|
128
129
|
- lib/invoiced/error/authentication_error.rb
|
129
130
|
- lib/invoiced/error/error_base.rb
|
@@ -141,8 +142,10 @@ files:
|
|
141
142
|
- lib/invoiced/util.rb
|
142
143
|
- lib/invoiced/version.rb
|
143
144
|
- test/invoiced/customer_test.rb
|
145
|
+
- test/invoiced/error_test.rb
|
144
146
|
- test/invoiced/invoice_test.rb
|
145
147
|
- test/invoiced/invoiced_test.rb
|
148
|
+
- test/invoiced/object_test.rb
|
146
149
|
- test/invoiced/transaction_test.rb
|
147
150
|
- test/invoiced/util_test.rb
|
148
151
|
- test/test_helper.rb
|
@@ -172,8 +175,10 @@ specification_version: 4
|
|
172
175
|
summary: Ruby client library for the Invoiced API
|
173
176
|
test_files:
|
174
177
|
- test/invoiced/customer_test.rb
|
178
|
+
- test/invoiced/error_test.rb
|
175
179
|
- test/invoiced/invoice_test.rb
|
176
180
|
- test/invoiced/invoiced_test.rb
|
181
|
+
- test/invoiced/object_test.rb
|
177
182
|
- test/invoiced/transaction_test.rb
|
178
183
|
- test/invoiced/util_test.rb
|
179
184
|
- test/test_helper.rb
|