Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tlconnor committed Nov 30, 2008
0 parents commit d431493
Show file tree
Hide file tree
Showing 28 changed files with 1,589 additions and 0 deletions.
14 changes: 14 additions & 0 deletions LICENSE
@@ -0,0 +1,14 @@
Copyright (c) 2008 Tim Connor <tlconnor@gmail.com>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

112 changes: 112 additions & 0 deletions README.textile
@@ -0,0 +1,112 @@
h1. Xero API wrapper

h2. Introduction

This library is designed to help ruby based applications communicate with the publicly available API for Xero. If you are unfamiliar with the API, you should first read the documentation, located here "https://network.xero.com/Help/Xero%20API%20Reference%201.0.htm":https://network.xero.com/Help/Xero%20API%20Reference%201.0.htm

h2. Prerequisites

To use the Xero API you must have a Xero API Key. IF you don't know what this is, and don't know how to get one, this library is probably not for you.

h2. Usage

<pre><code>
gateway = XeroGateway::Gateway.new(
:customer_key => "THE_CUSTOMER_KEY_GENERATED_FOR_YOUR_APP",
:api_key => "YOUR_XERO_API_KEY"
)
</code></pre>


h2. Implemented interface methods

h3. GET /api.xro/1.0/contact (get_contact_by_id)

Example:
<pre><code>
result = gateway.get_contact_by_id(contact_id)
contact = result.contact if result.success?
</code></pre>

h3. GET /api.xro/1.0/contact (get_contact_by_number)

Example:
<pre><code>
gateway.get_contact_by_number(contact_number)
</code></pre>

h3. GET /api.xro/1.0/contacts (get_contacts)

Example:
<pre><code>
gateway.get_contacts(:type => :all, :sort => :name, :direction => :desc)
</code></pre>

h3. PUT /api.xro/1.0/contact

Example:
<pre><code>
contact = XeroGateway::Contact.new
contact.name = "The contacts name"
contact.email = "whoever@something.com"
contact.phone.number = "555 123 4567"
contact.address.line_1 = "LINE 1 OF THE ADDRESS"
contact.address.line_2 = "LINE 2 OF THE ADDRESS"
contact.address.city = "WELLINGTON"
contact.address.region = "WELLINGTON"
contact.address.country = "NEW ZEALAND"
contact.address.post_code = "6021"

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

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

Example:
<pre><code>
gateway.get_invoice_by_id(invoice_id)
</code></pre>

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

Example:
<pre><code>
gateway.get_invoice_by_number(invoice_number)
</code></pre>

h3. GET /api.xro/1.0/invoices (get_invoices)

Example:
<pre><code>
gateway.get_invoices(modified_since = nil)
</code></pre>

h3. PUT /api.xro/1.0/invoice

Example:
<pre><code>
invoice = XeroGateway::Invoice.new({
:invoice_type => "ACCREC",
:due_date => 1.month.from_now,
:invoice_number => "YOUR INVOICE NUMBER",
:reference => "YOUR REFERENCE (NOT NECESSARILY UNIQUE!)",
:tax_inclusive => true,
:includes_tax => false,
:sub_total => 1000,
:total_tax => 125,
:total => 1250
})
invoice.contact = XeroGateway::Contact.new(:name => "THE NAME OF THE CONTACT")
invoice.contact.phone.number = "12345"
invoice.contact.address.line_1 = "LINE 1 OF THE ADDRESS"
invoice.line_items << XeroGateway::LineItem.new(
:description => "THE DESCRIPTION OF THE LINE ITEM",
:unit_amount => 1000,
:tax_amount => 125,
:line_amount => 1000,
:tracking_category => "THE TRACKING CATEGORY FOR THE LINE ITEM",
:tracking_option => "THE TRACKING OPTION FOR THE LINE ITEM"
)

gateway.create_invoice(invoice)
</code></pre>
14 changes: 14 additions & 0 deletions Rakefile
@@ -0,0 +1,14 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the xero gateway.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

34 changes: 34 additions & 0 deletions lib/xero_gateway.rb
@@ -0,0 +1,34 @@
# Copyright (c) 2008 Tim Connor <tlconnor@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


require "CGI"
require "URI"
require 'net/https'
require "rexml/document"
require "builder"
require "bigdecimal"

require File.dirname(__FILE__) + "/xero_gateway/http"
require File.dirname(__FILE__) + "/xero_gateway/dates"
require File.dirname(__FILE__) + "/xero_gateway/money"
require File.dirname(__FILE__) + "/xero_gateway/response"
require File.dirname(__FILE__) + "/xero_gateway/line_item"
require File.dirname(__FILE__) + "/xero_gateway/invoice"
require File.dirname(__FILE__) + "/xero_gateway/contact"
require File.dirname(__FILE__) + "/xero_gateway/address"
require File.dirname(__FILE__) + "/xero_gateway/phone"
require File.dirname(__FILE__) + "/xero_gateway/messages/contact_message"
require File.dirname(__FILE__) + "/xero_gateway/messages/invoice_message"
require File.dirname(__FILE__) + "/xero_gateway/gateway"
47 changes: 47 additions & 0 deletions lib/xero_gateway/address.rb
@@ -0,0 +1,47 @@
# Copyright (c) 2008 Tim Connor <tlconnor@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

module XeroGateway
class Address
attr_accessor :address_type, :line_1, :line_2, :line_3, :line_4, :city, :region, :post_code, :country

def initialize(params = {})
params = {
:address_type => "DEFAULT"
}.merge(params)

params.each do |k,v|
self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
self.send("#{k}=", v)
end
end

def self.parse(string)
address = Address.new

string.split("\r\n").each_with_index do |line, index|
address.instance_variable_set("@line_#{index+1}", line)
end
address
end

def ==(other)
equal = true
[:address_type, :line_1, :line_2, :line_3, :line_4, :city, :region, :post_code, :country].each do |field|
equal &&= (send(field) == other.send(field))
end
return equal
end
end
end
54 changes: 54 additions & 0 deletions lib/xero_gateway/contact.rb
@@ -0,0 +1,54 @@
# Copyright (c) 2008 Tim Connor <tlconnor@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

module XeroGateway
class Contact
attr_accessor :id, :contact_number, :status, :name, :email, :addresses, :phones, :updated_at

def initialize(params = {})
params = {}.merge(params)
params.each do |k,v|
self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
self.send("#{k}=", v)
end

@phones ||= []
@addresses ||= []
end

def address=(address)
self.addresses = [address]
end

def address
self.addresses[0] ||= Address.new
end

def phone=(phone)
self.phones = [phone]
end

def phone
self.phones[0] ||= Phone.new
end

def ==(other)
equal = true
[:contact_number, :status, :name, :email, :addresses, :phones].each do |field|
equal &&= (send(field) == other.send(field))
end
return equal
end
end
end
31 changes: 31 additions & 0 deletions lib/xero_gateway/dates.rb
@@ -0,0 +1,31 @@
# Copyright (c) 2008 Tim Connor <tlconnor@gmail.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

module XeroGateway
module Dates
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def format_date_time(time)
return time.strftime("%Y-%m-%dT%H:%M:%S")
end

def parse_date_time(time)
Time.local(time[0..3].to_i, time[5..6].to_i, time[8..9].to_i, time[11..12].to_i, time[14..15].to_i, time[17..18].to_i)
end
end
end
end

0 comments on commit d431493

Please sign in to comment.