Skip to content

Commit

Permalink
Add XeroGateway::Gateway.update_contacts to create/update a list of c…
Browse files Browse the repository at this point in the history
…ontacts with a single API request.
  • Loading branch information
waynerobinson committed Aug 13, 2009
1 parent 0e20284 commit f8c9bd2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.textile
Expand Up @@ -8,6 +8,8 @@ h2. 1.0.4, released 13/08/2009
* [WCR] Add useful API constants throughout the library.
* [WCR] Allow Invoice and LineItem to auto-calculate line_amount, sub_total, total_tax and total fields. Existing setters for these fields remain (for backwards compatability) but will be deprecated in the future.
* [WCR] Create XeroGateway::AccountList class to make working with the accounts response more powerful and faster (as it caches the last result).
* [WCR] Add :updated_after parameter to XeroGateway::Gateway#get_contacts
* [WCR] Add XeroGateway::Gateway#update_contacts

h2. 1.0.3, released 09/12/2008

Expand Down
11 changes: 10 additions & 1 deletion README.textile
Expand Up @@ -74,12 +74,21 @@ h3. POST /api.xro/1.0/contact

Updates an existing contact record.
<pre><code>
contact_retrieved_from_xero.email = "something_new@something.com"
contact_retrieved_from_xero.email = "something_new@something.com"

gateway.update_contact(contact)
</code></pre>


h3. POST /api.xro/1.0/contacts

Creates a list of contacts or updates them if they have a matching contact_id, contact_number or name.
This method uses only a single API request to create/update multiple contacts.
<pre><code>
contacts = [XeroGateway::Contact.new(:name => 'Joe Bloggs'), XeroGateway::Contact.new(:name => 'Jane Doe')]
result = gateway.update_contacts(contacts)
</code></pre>


h3. GET /api.xro/1.0/invoice (get_invoice_by_id)

Expand Down
25 changes: 25 additions & 0 deletions lib/xero_gateway/gateway.rb
Expand Up @@ -83,6 +83,31 @@ def update_contact(contact)
save_contact(contact)
end

#
# Updates an array of contacts in a single API operation.
#
# Usage :
# contacts = [XeroGateway::Contact.new(:name => 'Joe Bloggs'), XeroGateway::Contact.new(:name => 'Jane Doe')]
# result = gateway.update_contacts(contacts)
#
# Will update contacts with matching contact_id, contact_number or name or create if they don't exist.
def update_contacts(contacts)
b = Builder::XmlMarkup.new
request_xml = b.Contacts {
contacts.each do | contact |
contact.to_xml(b)
end
}

response_xml = http_post("#{@xero_url}/contacts", request_xml, {})

response = parse_response(response_xml, :request_xml => request_xml)
response.contacts.each_with_index do | response_contact, index |
contacts[index].contact_id = response_contact.contact_id if response_contact && response_contact.contact_id
end
response
end

# Retrieves an invoice from Xero based on its GUID
#
# Usage : get_invoice_by_id("8c69117a-60ae-4d31-9eb4-7f5a76bc4947")
Expand Down

0 comments on commit f8c9bd2

Please sign in to comment.