Skip to content

Commit

Permalink
Merge pull request #85 from armstrjare/items
Browse files Browse the repository at this point in the history
Items
  • Loading branch information
nikz authored Sep 15, 2016
2 parents b46ef54 + 22a8c3f commit f21b953
Show file tree
Hide file tree
Showing 13 changed files with 362 additions and 204 deletions.
2 changes: 2 additions & 0 deletions lib/xero_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require File.join(File.dirname(__FILE__), 'xero_gateway', 'money')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'line_item_calculations')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'response')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'base_record')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'account')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'accounts_list')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'tracking_category')
Expand All @@ -36,6 +37,7 @@
require File.join(File.dirname(__FILE__), 'xero_gateway', 'organisation')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'tax_rate')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'currency')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'item')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'error')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'oauth')
require File.join(File.dirname(__FILE__), 'xero_gateway', 'exceptions')
Expand Down
97 changes: 97 additions & 0 deletions lib/xero_gateway/base_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module XeroGateway
class BaseRecord
class_attribute :element_name
class_attribute :attribute_definitions

class << self
def attributes(hash)
hash.each do |k, v|
attribute k, v
end
end

def attribute(name, value)
self.attribute_definitions ||= {}
self.attribute_definitions[name] = value

case value
when Hash
value.each do |k, v|
attribute("#{name}#{k}", v)
end
else
attr_accessor name.underscore
end
end

def from_xml(base_element)
new.from_xml(base_element)
end

def xml_element
element_name || self.name.split('::').last
end
end

def initialize(params = {})
params.each do |k,v|
self.send("#{k}=", v) if respond_to?("#{k}=")
end
end

def ==(other)
to_xml == other.to_xml
end

def to_xml
builder = Builder::XmlMarkup.new
builder.__send__(self.class.xml_element) do
to_xml_attributes(builder)
end
end

def from_xml(base_element)
from_xml_attributes(base_element)
self
end

def from_xml_attributes(element, attribute = nil, attr_definition = self.class.attribute_definitions)
if Hash === attr_definition
element.children.each do |child|
next unless child.respond_to?(:name)

child_attribute = child.name
child_attr_definition = attr_definition[child_attribute]
next unless child_attr_definition

from_xml_attributes(child, "#{attribute}#{child_attribute}", child_attr_definition)
end

return
end

value = case attr_definition
when :boolean then element.text == "true"
when :float then element.text.to_f
when :integer then element.text.to_i
else element.text
end if element.text.present?

send("#{attribute.underscore}=", value)
end

def to_xml_attributes(builder = Builder::XmlMarkup.new, path = nil, attr_definitions = self.class.attribute_definitions)
attr_definitions.each do |attr, value|
case value
when Hash
builder.__send__(attr) do
to_xml_attributes(builder, "#{path}#{attr}", value)
end
else
builder.__send__(attr, send("#{path}#{attr}".underscore))
end
end
end

end
end
62 changes: 8 additions & 54 deletions lib/xero_gateway/currency.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,10 @@
module XeroGateway
class Currency

unless defined? ATTRS
ATTRS = {
"Code" => :string, # 3 letter alpha code for the currency – see list of currency codes
"Description" => :string, # Name of Currency
}
end

attr_accessor *ATTRS.keys.map(&:underscore)

def initialize(params = {})
params.each do |k,v|
self.send("#{k}=", v)
end
end

def ==(other)
ATTRS.keys.map(&:underscore).each do |field|
return false if send(field) != other.send(field)
end
return true
end

def to_xml
b = Builder::XmlMarkup.new

b.Currency do
ATTRS.keys.each do |attr|
eval("b.#{attr} '#{self.send(attr.underscore.to_sym)}'")
end
end
end

def self.from_xml(currency_element)
Currency.new.tap do |currency|
currency_element.children.each do |element|

attribute = element.name
underscored_attribute = element.name.underscore

raise "Unknown attribute: #{attribute}" unless ATTRS.keys.include?(attribute)

case (ATTRS[attribute])
when :boolean then currency.send("#{underscored_attribute}=", (element.text == "true"))
when :float then currency.send("#{underscored_attribute}=", element.text.to_f)
else currency.send("#{underscored_attribute}=", element.text)
end

end
end
end

class Currency < BaseRecord

attributes({
"Code" => :string, # 3 letter alpha code for the currency – see list of currency codes
"Description" => :string, # Name of Currency
})

end
end
end
9 changes: 9 additions & 0 deletions lib/xero_gateway/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,14 @@ def get_tax_rates
parse_response(response_xml, {}, {:request_signature => 'GET/tax_rates'})
end

#
# Gets all Items for a specific organisation in Xero
#
def get_items
response_xml = http_get(@client, "#{xero_url}/Items")
parse_response(response_xml, {}, {:request_signature => 'GET/items'})
end

#
# Create Payment record in Xero
#
Expand Down Expand Up @@ -761,6 +769,7 @@ def parse_response(raw_response, request = {}, options = {})
when "CreditNotes" then element.children.each {|child| response.response_item << CreditNote.from_xml(child, self, {:line_items_downloaded => options[:request_signature] != "GET/CreditNotes"}) }
when "Accounts" then element.children.each {|child| response.response_item << Account.from_xml(child) }
when "TaxRates" then element.children.each {|child| response.response_item << TaxRate.from_xml(child) }
when "Items" then element.children.each {|child| response.response_item << Item.from_xml(child) }
when "Currencies" then element.children.each {|child| response.response_item << Currency.from_xml(child) }
when "Organisations" then response.response_item = Organisation.from_xml(element.children.first) # Xero only returns the Authorized Organisation
when "TrackingCategories" then element.children.each {|child| response.response_item << TrackingCategory.from_xml(child) }
Expand Down
27 changes: 27 additions & 0 deletions lib/xero_gateway/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module XeroGateway
class Item < BaseRecord
attributes({
"ItemID" => :string,
"Code" => :string,
"InventoryAssetAccountCode" => :string,
"Name" => :string,
"IsSold" => :boolean,
"IsPurchased" => :boolean,
"Description" => :string,
"PurchaseDescription" => :string,
"IsTrackedAsInventory" => :boolean,
"TotalCostPool" => :float,
"QuantityOnHand" => :integer,

"SalesDetails" => {
"UnitPrice" => :float,
"AccountCode" => :string
},

"PurchaseDetails" => {
"UnitPrice" => :float,
"AccountCode" => :string
}
})
end
end
98 changes: 22 additions & 76 deletions lib/xero_gateway/organisation.rb
Original file line number Diff line number Diff line change
@@ -1,78 +1,24 @@
module XeroGateway
class Organisation

unless defined? ATTRS
ATTRS = {
"Name" => :string, # Display name of organisation shown in Xero
"LegalName" => :string, # Organisation name shown on Reports
"PaysTax" => :boolean, # Boolean to describe if organisation is registered with a local tax authority i.e. true, false
"Version" => :string, # See Version Types
"BaseCurrency" => :string, # Default currency for organisation. See Currency types
"OrganisationType" => :string, # only returned for "real" (i.e non-demo) companies
"OrganisationStatus" => :string,
"IsDemoCompany" => :boolean,
"APIKey" => :string, # returned if organisations are linked via Xero Network
"CountryCode" => :string,
"TaxNumber" => :string,
"FinancialYearEndDay" => :string,
"FinancialYearEndMonth" => :string,
"PeriodLockDate" => :string,
"CreatedDateUTC" => :string,
"ShortCode" => :string,
"Timezone" => :string,
"LineOfBusiness" => :string
}
end

attr_accessor *ATTRS.keys.map(&:underscore)

def initialize(params = {})
params.each do |k,v|
self.send("#{k}=", v)
end
end

def ==(other)
ATTRS.keys.map(&:underscore).each do |field|
return false if send(field) != other.send(field)
end
return true
end

def to_xml
b = Builder::XmlMarkup.new

b.Organisation do
ATTRS.keys.each do |attr|
eval("b.#{attr} '#{self.send(attr.underscore.to_sym)}'")
end
end
end

def self.from_xml(organisation_element)
Organisation.new.tap do |org|
organisation_element.children.each do |element|

attribute = element.name
underscored_attribute = element.name.underscore

if ATTRS.keys.include?(attribute)

case (ATTRS[attribute])
when :boolean then org.send("#{underscored_attribute}=", (element.text == "true"))
when :float then org.send("#{underscored_attribute}=", element.text.to_f)
else org.send("#{underscored_attribute}=", element.text)
end

else

warn "Ignoring unknown attribute: #{attribute}"

end

end
end
end

class Organisation < BaseRecord
attributes({
"Name" => :string, # Display name of organisation shown in Xero
"LegalName" => :string, # Organisation name shown on Reports
"PaysTax" => :boolean, # Boolean to describe if organisation is registered with a local tax authority i.e. true, false
"Version" => :string, # See Version Types
"BaseCurrency" => :string, # Default currency for organisation. See Currency types
"OrganisationType" => :string, # only returned for "real" (i.e non-demo) companies
"OrganisationStatus" => :string,
"IsDemoCompany" => :boolean,
"APIKey" => :string, # returned if organisations are linked via Xero Network
"CountryCode" => :string,
"TaxNumber" => :string,
"FinancialYearEndDay" => :string,
"FinancialYearEndMonth" => :string,
"PeriodLockDate" => :string,
"CreatedDateUTC" => :string,
"ShortCode" => :string,
"Timezone" => :string,
"LineOfBusiness" => :string
})
end
end
end
1 change: 1 addition & 0 deletions lib/xero_gateway/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def array_wrapped_response_item
alias_method :accounts, :array_wrapped_response_item
alias_method :tracking_categories, :array_wrapped_response_item
alias_method :tax_rates, :array_wrapped_response_item
alias_method :items, :array_wrapped_response_item
alias_method :currencies, :array_wrapped_response_item
alias_method :payments, :array_wrapped_response_item

Expand Down
Loading

0 comments on commit f21b953

Please sign in to comment.