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,apikey)
Xero.baseuri(url)
Xero.defaultparams({:apiKey=>apikey})
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.
Sphere: Related Content-
Adam
-
Thomas B
-
buildmaster