Navigation
« Twitter Updates for 2008-08-05 | Main | Connecting to the Xero API: Part 1 [Ruby, GET, Contacts] »
Thursday
Jul312008

Xero API + Ruby Revisited

Oh my, thanks very much to Tim Haines for pointing out HTTParty, this little gem makes writing an API for the Xero code really, really easy:

here’s all the methods for getting info out of Xero:
require 'HTTParty'
require 'pp'
class Xero
include HTTParty
format :XML
def initialize(url,api_key)
Xero.base_uri(url)
Xero.default_params({:apiKey=>api_key})
end
def contacts(customerKey)
Xero.get("/contacts",:query=>{:xeroKey=>customerKey})["Response"]
end
def contact(customerKey,opts)
opts = {:xeroKey=>customerKey}.merge(opts)
Xero.get("/contact",:query=>opts)["Response"]
end
def accounts(customerKey)
Xero.get("/accounts",:query=>{:xeroKey=>customerKey})["Response"]
end
def tracking(customerKey)
Xero.get("/tracking",:query=>{:xeroKey=>customerKey})["Response"]
end
def invoice(customerKey,opts)
opts = {:xeroKey=>customerKey}.merge(opts)
Xero.get("/invoice",:query=>opts)["Response"]
end
end

that's it.... that's all there is to it. IT's EASY AS PIE, Xero effort as I like to call it.

With this class I can then do:

Xero.new("http://xeronetworkurl","MyAPIKey").accounts("MyCustomerKey”) and I get a hash of all my accounts

contacts can be selected by contactNumber or contactID

just add the option:

Xero.new("http://xeronetworkurl","MyAPIKey").contact(“MyCustomerKey”,{:contactID => “contactid”})

Xero.new("http://xeronetworkurl","MyAPIKey").contact(“MyCustomerKey”,{:contactNumber=> “contactNumber”})

Invoices the same.

It's a breeze

as promised I will be doing some posting examples, but I’m so glad I found HTTParty it’s going to make my life sooo much easier.

Reader Comments (3)

Sweet... have you also see ROXML (http://roxml.rubyforge.org/%29?">http://roxml.rubyforge.org/)?

I'm about to start my own REST API for a side project, and ROXML sounds the business - it provides easy marsheling "Using simple annotations, it enables Ruby classes to be custom-mapped to XML."

August 10, 2008 | Unregistered CommenterAdam

Being a rails noob this looks like even I could use HTTParty. Maybe a stupid question but would the code above be in a new class file "Xero.rb" just as above ? How would the methods be referenced from other Rails controllers ?

October 16, 2008 | Unregistered Commenterbohtho

Hey bohtho,
sorry for my tardiness in replying.

simply just put a require in to the top of the class file. Just copy the code into ruby.rb and as long as their is an include statment that tells ruby where to load the file from ie if it's in the same directory: include 'xero.rb'
then you can create a new instance of the Xero object using Xero.new(”http://xeronetworkurl”,”MyAPIKey”);

and use the methods as described.

There are other ways to use it too. you can include it on a base controller to stop you having to include it with every class etc.

It's not quite plugin ready, but there might be a good reason to start distributing it as a gem, i'll have to think about it.

Cheers
Owen

October 22, 2008 | Unregistered Commenterbuildmaster

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>