tlconnor-xero_gateway 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.textile +5 -0
- data/README.textile +21 -3
- data/init.rb +1 -0
- data/lib/xero_gateway/contact.rb +5 -1
- data/lib/xero_gateway/gateway.rb +55 -3
- data/lib/xero_gateway/messages/account_message.rb +12 -24
- data/lib/xero_gateway/messages/tracking_category_message.rb +32 -0
- data/lib/xero_gateway/response.rb +1 -0
- data/lib/xero_gateway/tracking_category.rb +20 -0
- data/lib/xero_gateway.rb +2 -0
- data/test/integration/create_contact_test.rb +27 -0
- data/test/integration/create_invoice_test.rb +27 -0
- data/test/integration/get_accounts_test.rb +25 -0
- data/test/integration/get_contact_test.rb +29 -0
- data/test/integration/get_contacts_test.rb +29 -0
- data/test/integration/get_invoice_test.rb +32 -0
- data/test/integration/get_invoices_test.rb +40 -0
- data/test/integration/get_tracking_categories_test.rb +23 -0
- data/test/integration/integration_test_methods.rb +64 -0
- data/test/integration/stub_responses/tracking_categories.xml +1 -0
- data/test/integration/update_contact_test.rb +32 -0
- data/test/unit/messages/account_message_test.rb +34 -0
- data/test/unit/messages/tracking_category_message_test.rb +30 -0
- data/xero_gateway.gemspec +22 -7
- metadata +21 -6
data/CHANGELOG.textile
CHANGED
data/README.textile
CHANGED
@@ -52,7 +52,7 @@ Gets all contact records for a particular Xero customer.
|
|
52
52
|
|
53
53
|
h3. PUT /api.xro/1.0/contact
|
54
54
|
|
55
|
-
Saves a contact record for a particular Xero customer.
|
55
|
+
Saves a contact record for a particular Xero customer.
|
56
56
|
<pre><code>
|
57
57
|
contact = XeroGateway::Contact.new
|
58
58
|
contact.name = "The contacts name"
|
@@ -69,6 +69,16 @@ Saves a contact record for a particular Xero customer. If the contact record doe
|
|
69
69
|
</code></pre>
|
70
70
|
|
71
71
|
|
72
|
+
h3. POST /api.xro/1.0/contact
|
73
|
+
|
74
|
+
Updates an existing contact record.
|
75
|
+
<pre><code>
|
76
|
+
contact_retrieved_from_xero.email = "something_new@something.com"
|
77
|
+
|
78
|
+
gateway.update_contact(contact)
|
79
|
+
</code></pre>
|
80
|
+
|
81
|
+
|
72
82
|
|
73
83
|
h3. GET /api.xro/1.0/invoice (get_invoice_by_id)
|
74
84
|
|
@@ -92,7 +102,8 @@ h3. GET /api.xro/1.0/invoices (get_invoices)
|
|
92
102
|
|
93
103
|
Gets all invoice records for a particular Xero customer.
|
94
104
|
<pre><code>
|
95
|
-
gateway.get_invoices
|
105
|
+
gateway.get_invoices
|
106
|
+
gateway.get_invoices(1.month.ago) # modified since 1 month ago
|
96
107
|
</code></pre>
|
97
108
|
|
98
109
|
|
@@ -128,10 +139,17 @@ Inserts an invoice for a specific organization in Xero. (Currently only adding n
|
|
128
139
|
</code></pre>
|
129
140
|
|
130
141
|
|
131
|
-
|
132
142
|
h3. GET /api.xro/1.0/accounts
|
133
143
|
|
134
144
|
Gets all accounts for a specific organization in Xero.
|
135
145
|
<pre><code>
|
136
146
|
gateway.get_accounts
|
147
|
+
</code></pre>
|
148
|
+
|
149
|
+
|
150
|
+
h3. GET /api.xro/1.0/tracking
|
151
|
+
|
152
|
+
Gets all tracking categories and their options for a specific organization in Xero.
|
153
|
+
<pre><code>
|
154
|
+
gateway.get_tracking_categories
|
137
155
|
</code></pre>
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'xero_gateway'
|
data/lib/xero_gateway/contact.rb
CHANGED
data/lib/xero_gateway/gateway.rb
CHANGED
@@ -51,7 +51,7 @@ module XeroGateway
|
|
51
51
|
get_contact(nil, contact_number)
|
52
52
|
end
|
53
53
|
|
54
|
-
|
54
|
+
#
|
55
55
|
# Creates a contact in Xero
|
56
56
|
#
|
57
57
|
# Usage :
|
@@ -68,7 +68,7 @@ module XeroGateway
|
|
68
68
|
# contact.address.country = "NEW ZEALAND"
|
69
69
|
# contact.address.post_code = "6021"
|
70
70
|
#
|
71
|
-
#
|
71
|
+
# create_contact(contact)
|
72
72
|
def create_contact(contact)
|
73
73
|
request_xml = XeroGateway::Messages::ContactMessage.build_xml(contact)
|
74
74
|
response_xml = http_put("#{@xero_url}/contact", request_xml, {})
|
@@ -86,9 +86,40 @@ module XeroGateway
|
|
86
86
|
# Add the request and response XML to the response object
|
87
87
|
response.request_xml = request_xml
|
88
88
|
response.response_xml = response_xml
|
89
|
-
|
90
89
|
response
|
91
90
|
end
|
91
|
+
|
92
|
+
#
|
93
|
+
# Updates an existing Xero contact
|
94
|
+
#
|
95
|
+
# Usage :
|
96
|
+
#
|
97
|
+
# contact = xero_gateway.get_contact(some_contact_id)
|
98
|
+
# contact.email = "a_new_email_ddress"
|
99
|
+
#
|
100
|
+
# xero_gateway.update_contact(contact)
|
101
|
+
def update_contact(contact)
|
102
|
+
raise "contact_id or contact_number is required for updating contacts" if contact.contact_id.nil? and contact.contact_number.nil?
|
103
|
+
|
104
|
+
request_xml = XeroGateway::Messages::ContactMessage.build_xml(contact)
|
105
|
+
response_xml = http_post("#{@xero_url}/contact", request_xml, {})
|
106
|
+
|
107
|
+
doc = REXML::Document.new(response_xml)
|
108
|
+
|
109
|
+
# Create the response object
|
110
|
+
response = build_response(doc)
|
111
|
+
|
112
|
+
# Add the invoice to the response
|
113
|
+
if response.success?
|
114
|
+
response.response_item = XeroGateway::Messages::ContactMessage.from_xml(REXML::XPath.first(doc, "/Response/Contact"))
|
115
|
+
end
|
116
|
+
|
117
|
+
# Add the request and response XML to the response object
|
118
|
+
response.request_xml = request_xml
|
119
|
+
response.response_xml = response_xml
|
120
|
+
|
121
|
+
response
|
122
|
+
end
|
92
123
|
|
93
124
|
# Retrieves an invoice from Xero based on its GUID
|
94
125
|
#
|
@@ -202,6 +233,27 @@ module XeroGateway
|
|
202
233
|
response
|
203
234
|
end
|
204
235
|
|
236
|
+
#
|
237
|
+
# Gets all tracking categories for a specific organization in Xero.
|
238
|
+
#
|
239
|
+
def get_tracking_categories
|
240
|
+
response_xml = http_get("#{xero_url}/tracking")
|
241
|
+
|
242
|
+
doc = REXML::Document.new(response_xml)
|
243
|
+
|
244
|
+
# Create the response object
|
245
|
+
response = build_response(doc)
|
246
|
+
|
247
|
+
# Add the accounts to the response
|
248
|
+
response.response_item = []
|
249
|
+
REXML::XPath.first(doc, "/Response/Tracking").children.each do |tracking_category_element|
|
250
|
+
response.response_item << XeroGateway::Messages::TrackingCategoryMessage.from_xml(tracking_category_element)
|
251
|
+
end
|
252
|
+
|
253
|
+
# Add the request and response XML to the response object
|
254
|
+
response.response_xml = response_xml
|
255
|
+
response
|
256
|
+
end
|
205
257
|
|
206
258
|
|
207
259
|
private
|
@@ -2,6 +2,18 @@ module XeroGateway
|
|
2
2
|
module Messages
|
3
3
|
class AccountMessage
|
4
4
|
|
5
|
+
def self.build_xml(account)
|
6
|
+
b = Builder::XmlMarkup.new
|
7
|
+
|
8
|
+
b.Account {
|
9
|
+
b.Code account.code
|
10
|
+
b.Name account.name
|
11
|
+
b.Type account.type
|
12
|
+
b.TaxType account.tax_type
|
13
|
+
b.Description account.description
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
5
17
|
# Take an Account element and convert it into an Account object
|
6
18
|
def self.from_xml(account_element)
|
7
19
|
account = Account.new
|
@@ -16,30 +28,6 @@ module XeroGateway
|
|
16
28
|
end
|
17
29
|
account
|
18
30
|
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def self.parse_line_item(line_item_element)
|
23
|
-
line_item = LineItem.new
|
24
|
-
line_item_element.children.each do |element|
|
25
|
-
case(element.name)
|
26
|
-
when "LineItemID" then line_item.line_item_id = element.text
|
27
|
-
when "Description" then line_item.description = element.text
|
28
|
-
when "Quantity" then line_item.quantity = element.text.to_i
|
29
|
-
when "UnitAmount" then line_item.unit_amount = BigDecimal.new(element.text)
|
30
|
-
when "TaxType" then line_item.tax_type = element.text
|
31
|
-
when "TaxAmount" then line_item.tax_amount = BigDecimal.new(element.text)
|
32
|
-
when "LineAmount" then line_item.line_amount = BigDecimal.new(element.text)
|
33
|
-
when "AccountCode" then line_item.account_code = element.text
|
34
|
-
when "Tracking" then
|
35
|
-
if element.elements['TrackingCategory']
|
36
|
-
line_item.tracking_category = element.elements['TrackingCategory/Name'].text
|
37
|
-
line_item.tracking_option = element.elements['TrackingCategory/Option'].text
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
line_item
|
42
|
-
end
|
43
31
|
end
|
44
32
|
end
|
45
33
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module XeroGateway
|
2
|
+
module Messages
|
3
|
+
class TrackingCategoryMessage
|
4
|
+
|
5
|
+
def self.build_xml(tracking_category)
|
6
|
+
b = Builder::XmlMarkup.new
|
7
|
+
|
8
|
+
b.TrackingCategory {
|
9
|
+
b.Name tracking_category.name
|
10
|
+
b.Options {
|
11
|
+
tracking_category.options.each do |option|
|
12
|
+
b.Option {
|
13
|
+
b.Name option
|
14
|
+
}
|
15
|
+
end
|
16
|
+
}
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.from_xml(tracking_category_element)
|
21
|
+
tracking_category = TrackingCategory.new
|
22
|
+
tracking_category_element.children.each do |element|
|
23
|
+
case(element.name)
|
24
|
+
when "Name" then tracking_category.name = element.text
|
25
|
+
when "Options" then element.children.each {|option| tracking_category.options << option.children.first.text}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
tracking_category
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module XeroGateway
|
2
|
+
class TrackingCategory
|
3
|
+
attr_accessor :name, :options
|
4
|
+
|
5
|
+
def initialize(params = {})
|
6
|
+
@options = []
|
7
|
+
params.each do |k,v|
|
8
|
+
self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
|
9
|
+
self.send("#{k}=", v)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def ==(other)
|
14
|
+
[:name, :options].each do |field|
|
15
|
+
return false if send(field) != other.send(field)
|
16
|
+
end
|
17
|
+
return true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/xero_gateway.rb
CHANGED
@@ -15,7 +15,9 @@ require File.dirname(__FILE__) + "/xero_gateway/contact"
|
|
15
15
|
require File.dirname(__FILE__) + "/xero_gateway/address"
|
16
16
|
require File.dirname(__FILE__) + "/xero_gateway/phone"
|
17
17
|
require File.dirname(__FILE__) + "/xero_gateway/account"
|
18
|
+
require File.dirname(__FILE__) + "/xero_gateway/tracking_category"
|
18
19
|
require File.dirname(__FILE__) + "/xero_gateway/messages/contact_message"
|
19
20
|
require File.dirname(__FILE__) + "/xero_gateway/messages/invoice_message"
|
20
21
|
require File.dirname(__FILE__) + "/xero_gateway/messages/account_message"
|
22
|
+
require File.dirname(__FILE__) + "/xero_gateway/messages/tracking_category_message"
|
21
23
|
require File.dirname(__FILE__) + "/xero_gateway/gateway"
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class CreateContactTest < Test::Unit::TestCase
|
4
|
+
include IntegrationTestMethods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gateway = XeroGateway::Gateway.new(
|
8
|
+
:customer_key => CUSTOMER_KEY,
|
9
|
+
:api_key => API_KEY
|
10
|
+
)
|
11
|
+
|
12
|
+
if STUB_XERO_CALLS
|
13
|
+
@gateway.xero_url = "DUMMY_URL"
|
14
|
+
|
15
|
+
@gateway.stubs(:http_put).with {|url, body, params| url =~ /contact$/ }.returns(get_file_as_string("contact.xml"))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_create_contact
|
20
|
+
example_contact = dummy_contact
|
21
|
+
|
22
|
+
result = @gateway.create_contact(example_contact)
|
23
|
+
assert result.success?
|
24
|
+
assert !result.contact.contact_id.nil?
|
25
|
+
assert_equal result.contact.name, example_contact.name
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class CreateInvoiceTest < Test::Unit::TestCase
|
4
|
+
include IntegrationTestMethods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gateway = XeroGateway::Gateway.new(
|
8
|
+
:customer_key => CUSTOMER_KEY,
|
9
|
+
:api_key => API_KEY
|
10
|
+
)
|
11
|
+
|
12
|
+
if STUB_XERO_CALLS
|
13
|
+
@gateway.xero_url = "DUMMY_URL"
|
14
|
+
|
15
|
+
@gateway.stubs(:http_put).with {|url, body, params| url =~ /invoice$/ }.returns(get_file_as_string("invoice.xml"))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_create_invoice
|
20
|
+
example_invoice = dummy_invoice
|
21
|
+
|
22
|
+
result = @gateway.create_invoice(example_invoice)
|
23
|
+
assert result.success?
|
24
|
+
assert !result.invoice.invoice_id.nil?
|
25
|
+
assert result.invoice.invoice_number == example_invoice.invoice_number
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class GetAccountsTest < Test::Unit::TestCase
|
4
|
+
include IntegrationTestMethods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gateway = XeroGateway::Gateway.new(
|
8
|
+
:customer_key => CUSTOMER_KEY,
|
9
|
+
:api_key => API_KEY
|
10
|
+
)
|
11
|
+
|
12
|
+
if STUB_XERO_CALLS
|
13
|
+
@gateway.xero_url = "DUMMY_URL"
|
14
|
+
|
15
|
+
@gateway.stubs(:http_get).with {|url, params| url =~ /accounts$/ }.returns(get_file_as_string("accounts.xml"))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_get_accounts
|
20
|
+
result = @gateway.get_accounts
|
21
|
+
assert result.success?
|
22
|
+
assert result.accounts.size > 0
|
23
|
+
assert_equal XeroGateway::Account, result.accounts.first.class
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class GetContactTest < Test::Unit::TestCase
|
4
|
+
include IntegrationTestMethods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gateway = XeroGateway::Gateway.new(
|
8
|
+
:customer_key => CUSTOMER_KEY,
|
9
|
+
:api_key => API_KEY
|
10
|
+
)
|
11
|
+
|
12
|
+
if STUB_XERO_CALLS
|
13
|
+
@gateway.xero_url = "DUMMY_URL"
|
14
|
+
|
15
|
+
@gateway.stubs(:http_get).with {|url, params| url =~ /contact$/ }.returns(get_file_as_string("contact.xml"))
|
16
|
+
@gateway.stubs(:http_put).with {|url, body, params| url =~ /contact$/ }.returns(get_file_as_string("contact.xml"))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_get_contact
|
21
|
+
# Make sure there is an contact in Xero to retrieve
|
22
|
+
contact = @gateway.create_contact(dummy_contact).contact
|
23
|
+
flunk "get_contact could not be tested because create_contact failed" if contact.nil?
|
24
|
+
|
25
|
+
result = @gateway.get_contact_by_id(contact.contact_id)
|
26
|
+
assert result.success?
|
27
|
+
assert_equal result.contact.name, contact.name
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class GetContactsTest < Test::Unit::TestCase
|
4
|
+
include IntegrationTestMethods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gateway = XeroGateway::Gateway.new(
|
8
|
+
:customer_key => CUSTOMER_KEY,
|
9
|
+
:api_key => API_KEY
|
10
|
+
)
|
11
|
+
|
12
|
+
if STUB_XERO_CALLS
|
13
|
+
@gateway.xero_url = "DUMMY_URL"
|
14
|
+
|
15
|
+
@gateway.stubs(:http_get).with {|url, params| url =~ /contacts$/ }.returns(get_file_as_string("contacts.xml"))
|
16
|
+
@gateway.stubs(:http_put).with {|url, body, params| url =~ /contact$/ }.returns(get_file_as_string("contact.xml"))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_get_contacts
|
21
|
+
# Make sure there is an contact in Xero to retrieve
|
22
|
+
contact = @gateway.create_contact(dummy_contact).contact
|
23
|
+
flunk "get_contacts could not be tested because create_contact failed" if contact.nil?
|
24
|
+
|
25
|
+
result = @gateway.get_contacts
|
26
|
+
assert result.success?
|
27
|
+
assert result.contacts.collect {|c| c.contact_id}.include?(contact.contact_id)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class GetInvoiceTest < Test::Unit::TestCase
|
4
|
+
include IntegrationTestMethods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gateway = XeroGateway::Gateway.new(
|
8
|
+
:customer_key => CUSTOMER_KEY,
|
9
|
+
:api_key => API_KEY
|
10
|
+
)
|
11
|
+
|
12
|
+
if STUB_XERO_CALLS
|
13
|
+
@gateway.xero_url = "DUMMY_URL"
|
14
|
+
|
15
|
+
@gateway.stubs(:http_get).with {|url, params| url =~ /invoice$/ }.returns(get_file_as_string("invoice.xml"))
|
16
|
+
@gateway.stubs(:http_put).with {|url, body, params| url =~ /invoice$/ }.returns(get_file_as_string("invoice.xml"))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_get_invoice
|
21
|
+
# Make sure there is an invoice in Xero to retrieve
|
22
|
+
invoice = @gateway.create_invoice(dummy_invoice).invoice
|
23
|
+
|
24
|
+
result = @gateway.get_invoice_by_id(invoice.invoice_id)
|
25
|
+
assert result.success?
|
26
|
+
assert_equal result.invoice.invoice_number, invoice.invoice_number
|
27
|
+
|
28
|
+
result = @gateway.get_invoice_by_number(invoice.invoice_number)
|
29
|
+
assert result.success?
|
30
|
+
assert_equal result.invoice.invoice_id, invoice.invoice_id
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class GetInvoicesTest < Test::Unit::TestCase
|
4
|
+
include IntegrationTestMethods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gateway = XeroGateway::Gateway.new(
|
8
|
+
:customer_key => CUSTOMER_KEY,
|
9
|
+
:api_key => API_KEY
|
10
|
+
)
|
11
|
+
|
12
|
+
if STUB_XERO_CALLS
|
13
|
+
@gateway.xero_url = "DUMMY_URL"
|
14
|
+
|
15
|
+
@gateway.stubs(:http_get).with {|url, params| url =~ /invoices$/ }.returns(get_file_as_string("invoices.xml"))
|
16
|
+
@gateway.stubs(:http_put).with {|url, body, params| url =~ /invoice$/ }.returns(get_file_as_string("invoice.xml"))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_get_invoices
|
21
|
+
# Make sure there is an invoice in Xero to retrieve
|
22
|
+
invoice = @gateway.create_invoice(dummy_invoice).invoice
|
23
|
+
|
24
|
+
result = @gateway.get_invoices
|
25
|
+
assert result.success?
|
26
|
+
assert result.invoices.collect {|i| i.invoice_number}.include?(invoice.invoice_number)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_get_invoices_with_modified_since_date
|
30
|
+
# Create a test invoice
|
31
|
+
invoice = dummy_invoice
|
32
|
+
@gateway.create_invoice(invoice)
|
33
|
+
|
34
|
+
# Check that it is returned
|
35
|
+
result = @gateway.get_invoices(Date.today - 1)
|
36
|
+
assert result.success?
|
37
|
+
assert result.request_params.keys.include?(:modifiedSince) # make sure the flag was sent
|
38
|
+
assert result.invoices.collect {|response_invoice| response_invoice.invoice_number}.include?(invoice.invoice_number)
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class GetTrackingCategoriesTest < Test::Unit::TestCase
|
4
|
+
include IntegrationTestMethods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gateway = XeroGateway::Gateway.new(:customer_key => CUSTOMER_KEY, :api_key => API_KEY)
|
8
|
+
|
9
|
+
if STUB_XERO_CALLS
|
10
|
+
@gateway.xero_url = "DUMMY_URL"
|
11
|
+
|
12
|
+
@gateway.stubs(:http_get).with {|url, params| url =~ /tracking$/ }.returns(get_file_as_string("tracking_categories.xml"))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_get_tracking_categories
|
17
|
+
result = @gateway.get_tracking_categories
|
18
|
+
assert result.success?
|
19
|
+
if STUB_XERO_CALLS
|
20
|
+
assert result.tracking_categories.size == 2
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module IntegrationTestMethods
|
2
|
+
# The integration tests can be run against the Xero test environment. You mush have a company set up in the test
|
3
|
+
# environment, and you must have set up a customer key for that account.
|
4
|
+
#
|
5
|
+
# You can then run the tests against the test environment using the commands (linux or mac):
|
6
|
+
# export STUB_XERO_CALLS=false
|
7
|
+
# export API_KEY=[your_api_key]
|
8
|
+
# export CUSTOMER_KEY=[your_customer_key]
|
9
|
+
# rake test
|
10
|
+
STUB_XERO_CALLS = ENV["STUB_XERO_CALLS"].nil? ? true : (ENV["STUB_XERO_CALLS"] == "true") unless defined? STUB_XERO_CALLS
|
11
|
+
|
12
|
+
API_KEY = ENV["API_KEY"] unless defined? API_KEY
|
13
|
+
CUSTOMER_KEY = ENV["CUSTOMER_KEY"] unless defined? CUSTOMER_KEY
|
14
|
+
|
15
|
+
|
16
|
+
def dummy_invoice
|
17
|
+
invoice = XeroGateway::Invoice.new({
|
18
|
+
:invoice_type => "ACCREC",
|
19
|
+
:due_date => Date.today + 20,
|
20
|
+
:invoice_number => STUB_XERO_CALLS ? "INV-0001" : "#{Time.now.to_f}",
|
21
|
+
:reference => "YOUR REFERENCE (NOT NECESSARILY UNIQUE!)",
|
22
|
+
:sub_total => 1000,
|
23
|
+
:total_tax => 125,
|
24
|
+
:total => 1125
|
25
|
+
})
|
26
|
+
invoice.contact = dummy_contact
|
27
|
+
invoice.line_items << XeroGateway::LineItem.new(
|
28
|
+
:description => "THE DESCRIPTION OF THE LINE ITEM",
|
29
|
+
:unit_amount => 1000,
|
30
|
+
:tax_amount => 125,
|
31
|
+
:line_amount => 1000,
|
32
|
+
:tracking_category => "THE TRACKING CATEGORY FOR THE LINE ITEM",
|
33
|
+
:tracking_option => "THE TRACKING OPTION FOR THE LINE ITEM"
|
34
|
+
)
|
35
|
+
invoice
|
36
|
+
end
|
37
|
+
|
38
|
+
def dummy_contact
|
39
|
+
unique_id = Time.now.to_f
|
40
|
+
contact = XeroGateway::Contact.new(:name => STUB_XERO_CALLS ? "CONTACT NAME" : "THE NAME OF THE CONTACT #{unique_id}")
|
41
|
+
contact.email = "bob#{unique_id}@example.com"
|
42
|
+
contact.phone.number = "12345"
|
43
|
+
contact.address.line_1 = "LINE 1 OF THE ADDRESS"
|
44
|
+
contact.address.line_2 = "LINE 2 OF THE ADDRESS"
|
45
|
+
contact.address.line_3 = "LINE 3 OF THE ADDRESS"
|
46
|
+
contact.address.line_4 = "LINE 4 OF THE ADDRESS"
|
47
|
+
contact.address.city = "WELLINGTON"
|
48
|
+
contact.address.region = "WELLINGTON"
|
49
|
+
contact.address.country = "NEW ZEALAND"
|
50
|
+
contact.address.post_code = "6021"
|
51
|
+
|
52
|
+
contact
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_file_as_string(filename)
|
56
|
+
data = ''
|
57
|
+
f = File.open(File.dirname(__FILE__) + "/stub_responses/" + filename, "r")
|
58
|
+
f.each_line do |line|
|
59
|
+
data += line
|
60
|
+
end
|
61
|
+
f.close
|
62
|
+
return data
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<?xml version="1.0"?><Response><ID>89a1d50c-f528-470c-b924-c10281ff67d6</ID><Status>OK</Status><ProviderName>XeroTest</ProviderName><DateTimeUTC>2008-04-20T20:16:51.7072529Z</DateTimeUTC><Tracking><TrackingCategory><Name>Activity/Workstream</Name><Options><Option><Name>Documentation</Name></Option><Option><Name>Onsite consultancy</Name></Option><Option><Name>Publications</Name></Option><Option><Name>Support</Name></Option><Option><Name>Training</Name></Option><Option><Name>Website management</Name></Option></Options></TrackingCategory><TrackingCategory><Name>Consultant/Sales person</Name><Options><Option><Name>Felix Wilson</Name></Option><Option><Name>Martin Hudson</Name></Option><Option><Name>Orlena Greenville</Name></Option></Options></TrackingCategory></Tracking></Response>
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class UpdateContactTest < Test::Unit::TestCase
|
4
|
+
include IntegrationTestMethods
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@gateway = XeroGateway::Gateway.new(
|
8
|
+
:customer_key => CUSTOMER_KEY,
|
9
|
+
:api_key => API_KEY
|
10
|
+
)
|
11
|
+
|
12
|
+
if STUB_XERO_CALLS
|
13
|
+
@gateway.xero_url = "DUMMY_URL"
|
14
|
+
|
15
|
+
@gateway.stubs(:http_put).with {|url, body, params| url =~ /contact$/ }.returns(get_file_as_string("contact.xml"))
|
16
|
+
@gateway.stubs(:http_post).with {|url, body, params| url =~ /contact$/ }.returns(get_file_as_string("contact.xml"))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_update_contact
|
21
|
+
# Make sure there is a contact in Xero to retrieve
|
22
|
+
contact = @gateway.create_contact(dummy_contact).contact
|
23
|
+
|
24
|
+
contact.phone.number = "123 4567"
|
25
|
+
|
26
|
+
result = @gateway.update_contact(contact)
|
27
|
+
|
28
|
+
assert result.success?
|
29
|
+
assert_equal contact.contact_id, result.contact.contact_id
|
30
|
+
assert_equal "123 4567", result.contact.phone.number if !STUB_XERO_CALLS
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../test_helper.rb')
|
2
|
+
|
3
|
+
class AccountMessageTest < Test::Unit::TestCase
|
4
|
+
# Tests that an account can be converted into XML that Xero can understand, and then converted back to an account
|
5
|
+
def test_build_and_parse_xml
|
6
|
+
account = create_test_account
|
7
|
+
|
8
|
+
# Generate the XML message
|
9
|
+
account_as_xml = XeroGateway::Messages::AccountMessage.build_xml(account)
|
10
|
+
|
11
|
+
# Parse the XML message and retrieve the account element
|
12
|
+
account_element = REXML::XPath.first(REXML::Document.new(account_as_xml), "/Account")
|
13
|
+
|
14
|
+
# Build a new account from the XML
|
15
|
+
result_account = XeroGateway::Messages::AccountMessage.from_xml(account_element)
|
16
|
+
|
17
|
+
# Check the account details
|
18
|
+
assert_equal account, result_account
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def create_test_account
|
25
|
+
account = XeroGateway::Account.new
|
26
|
+
account.code = "200"
|
27
|
+
account.name = "Sales"
|
28
|
+
account.type = "REVENUE"
|
29
|
+
account.tax_type = "OUTPUT"
|
30
|
+
account.description = "Income from any normal business activity"
|
31
|
+
|
32
|
+
account
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '../../test_helper.rb')
|
2
|
+
|
3
|
+
class TrackingCategoryMessageTest < Test::Unit::TestCase
|
4
|
+
# Tests that a tracking category can be converted into XML that Xero can understand, and then converted back to a tracking category
|
5
|
+
def test_build_and_parse_xml
|
6
|
+
tracking_category = create_test_tracking_category
|
7
|
+
|
8
|
+
# Generate the XML message
|
9
|
+
tracking_category_as_xml = XeroGateway::Messages::TrackingCategoryMessage.build_xml(tracking_category)
|
10
|
+
|
11
|
+
# Parse the XML message and retrieve the tracking category element
|
12
|
+
tracking_category_element = REXML::XPath.first(REXML::Document.new(tracking_category_as_xml), "/TrackingCategory")
|
13
|
+
|
14
|
+
# Build a new tracking category from the XML
|
15
|
+
result_tracking_category = XeroGateway::Messages::TrackingCategoryMessage.from_xml(tracking_category_element)
|
16
|
+
|
17
|
+
# Check the tracking category details
|
18
|
+
assert_equal tracking_category, result_tracking_category
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def create_test_tracking_category
|
25
|
+
tracking_category = XeroGateway::TrackingCategory.new
|
26
|
+
tracking_category.name = "REGION"
|
27
|
+
tracking_category.options = ["NORTH", "SOUTH", "CENTRAL"]
|
28
|
+
tracking_category
|
29
|
+
end
|
30
|
+
end
|
data/xero_gateway.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "xero_gateway"
|
3
|
-
s.version = "1.0.
|
4
|
-
s.date = "2008-12-
|
3
|
+
s.version = "1.0.2"
|
4
|
+
s.date = "2008-12-04"
|
5
5
|
s.summary = "Enables ruby based applications to communicate with the Xero API"
|
6
6
|
s.email = "tlconnor@gmail.com"
|
7
7
|
s.homepage = "http://github.com/tlconnor/xero_gateway"
|
@@ -9,10 +9,11 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.has_rdoc = false
|
10
10
|
s.authors = ["Tim Connor"]
|
11
11
|
s.add_dependency('builder', '>= 2.1.2')
|
12
|
-
s.files = ["
|
13
|
-
"
|
12
|
+
s.files = ["CHANGELOG.textile",
|
13
|
+
"init.rb",
|
14
14
|
"LICENSE",
|
15
15
|
"Rakefile",
|
16
|
+
"README.textile",
|
16
17
|
"lib/xero_gateway.rb",
|
17
18
|
"lib/xero_gateway/account.rb",
|
18
19
|
"lib/xero_gateway/address.rb",
|
@@ -25,18 +26,32 @@ Gem::Specification.new do |s|
|
|
25
26
|
"lib/xero_gateway/money.rb",
|
26
27
|
"lib/xero_gateway/phone.rb",
|
27
28
|
"lib/xero_gateway/response.rb",
|
29
|
+
"lib/xero_gateway/tracking_category.rb",
|
28
30
|
"lib/xero_gateway/messages/account_message.rb",
|
29
31
|
"lib/xero_gateway/messages/contact_message.rb",
|
30
32
|
"lib/xero_gateway/messages/invoice_message.rb",
|
33
|
+
"lib/xero_gateway/messages/tracking_category_message.rb",
|
31
34
|
"test/test_helper.rb",
|
32
|
-
"test/
|
33
|
-
"test/
|
34
|
-
"test/integration/
|
35
|
+
"test/integration/create_contact_test.rb",
|
36
|
+
"test/integration/create_invoice_test.rb",
|
37
|
+
"test/integration/get_accounts_test.rb",
|
38
|
+
"test/integration/get_contact_test.rb",
|
39
|
+
"test/integration/get_contacts_test.rb",
|
40
|
+
"test/integration/get_invoice_test.rb",
|
41
|
+
"test/integration/get_invoices_test.rb",
|
42
|
+
"test/integration/get_tracking_categories_test.rb",
|
43
|
+
"test/integration/integration_test_methods.rb",
|
35
44
|
"test/integration/stub_responses/accounts.xml",
|
36
45
|
"test/integration/stub_responses/contact.xml",
|
37
46
|
"test/integration/stub_responses/contacts.xml",
|
38
47
|
"test/integration/stub_responses/invoice.xml",
|
39
48
|
"test/integration/stub_responses/invoices.xml",
|
49
|
+
"test/integration/stub_responses/tracking_categories.xml",
|
50
|
+
"test/integration/update_contact_test.rb",
|
51
|
+
"test/unit/messages/account_message_test.rb",
|
52
|
+
"test/unit/messages/contact_message_test.rb",
|
53
|
+
"test/unit/messages/invoice_message_test.rb",
|
54
|
+
"test/unit/messages/tracking_category_message_test.rb",
|
40
55
|
"test/xsd/README",
|
41
56
|
"test/xsd/create_contact.xsd",
|
42
57
|
"test/xsd/create_invoice.xsd",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tlconnor-xero_gateway
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Connor
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-12-
|
12
|
+
date: 2008-12-04 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,10 +30,11 @@ extensions: []
|
|
30
30
|
extra_rdoc_files: []
|
31
31
|
|
32
32
|
files:
|
33
|
-
- README.textile
|
34
33
|
- CHANGELOG.textile
|
34
|
+
- init.rb
|
35
35
|
- LICENSE
|
36
36
|
- Rakefile
|
37
|
+
- README.textile
|
37
38
|
- lib/xero_gateway.rb
|
38
39
|
- lib/xero_gateway/account.rb
|
39
40
|
- lib/xero_gateway/address.rb
|
@@ -46,18 +47,32 @@ files:
|
|
46
47
|
- lib/xero_gateway/money.rb
|
47
48
|
- lib/xero_gateway/phone.rb
|
48
49
|
- lib/xero_gateway/response.rb
|
50
|
+
- lib/xero_gateway/tracking_category.rb
|
49
51
|
- lib/xero_gateway/messages/account_message.rb
|
50
52
|
- lib/xero_gateway/messages/contact_message.rb
|
51
53
|
- lib/xero_gateway/messages/invoice_message.rb
|
54
|
+
- lib/xero_gateway/messages/tracking_category_message.rb
|
52
55
|
- test/test_helper.rb
|
53
|
-
- test/
|
54
|
-
- test/
|
55
|
-
- test/integration/
|
56
|
+
- test/integration/create_contact_test.rb
|
57
|
+
- test/integration/create_invoice_test.rb
|
58
|
+
- test/integration/get_accounts_test.rb
|
59
|
+
- test/integration/get_contact_test.rb
|
60
|
+
- test/integration/get_contacts_test.rb
|
61
|
+
- test/integration/get_invoice_test.rb
|
62
|
+
- test/integration/get_invoices_test.rb
|
63
|
+
- test/integration/get_tracking_categories_test.rb
|
64
|
+
- test/integration/integration_test_methods.rb
|
56
65
|
- test/integration/stub_responses/accounts.xml
|
57
66
|
- test/integration/stub_responses/contact.xml
|
58
67
|
- test/integration/stub_responses/contacts.xml
|
59
68
|
- test/integration/stub_responses/invoice.xml
|
60
69
|
- test/integration/stub_responses/invoices.xml
|
70
|
+
- test/integration/stub_responses/tracking_categories.xml
|
71
|
+
- test/integration/update_contact_test.rb
|
72
|
+
- test/unit/messages/account_message_test.rb
|
73
|
+
- test/unit/messages/contact_message_test.rb
|
74
|
+
- test/unit/messages/invoice_message_test.rb
|
75
|
+
- test/unit/messages/tracking_category_message_test.rb
|
61
76
|
- test/xsd/README
|
62
77
|
- test/xsd/create_contact.xsd
|
63
78
|
- test/xsd/create_invoice.xsd
|