-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from armstrjare/items
Items
- Loading branch information
Showing
13 changed files
with
362 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.