Skip to content

Commit

Permalink
Merge pull request #90 from xero-gateway/add-attention-to-for-addresses
Browse files Browse the repository at this point in the history
adds attention to field to contact addresses
  • Loading branch information
nikz authored Sep 15, 2016
2 parents f21b953 + 2ae189f commit 6e8cd13
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 42 deletions.
56 changes: 29 additions & 27 deletions lib/xero_gateway/address.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
module XeroGateway
class Address

ADDRESS_TYPE = {
'STREET' => 'Street',
'POBOX' => 'PO Box'
} unless defined?(ADDRESS_TYPE)

# Any errors that occurred when the #valid? method called.
attr_reader :errors
attr_accessor :address_type, :line_1, :line_2, :line_3, :line_4, :city, :region, :post_code, :country

attr_accessor :address_type, :line_1, :line_2, :line_3, :line_4, :city, :region, :post_code, :country, :attention_to

def initialize(params = {})
@errors ||= []

params = {
:address_type => "POBOX"
}.merge(params)

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

# Validate the Address record according to what will be valid by the gateway.
#
# Usage:
# Usage:
# address.valid? # Returns true/false
#
#
# Additionally sets address.errors array to an array of field/error.
def valid?
@errors = []

if address_type && !ADDRESS_TYPE[address_type]
@errors << ['address_type', "must be one of #{ADDRESS_TYPE.keys.join('/')} and is currently #{address_type}"]
end

@errors.size == 0
end
end

def to_xml(b = Builder::XmlMarkup.new)
b.Address {
b.AddressType address_type
Expand All @@ -50,44 +50,46 @@ def to_xml(b = Builder::XmlMarkup.new)
b.Region region if region
b.PostalCode post_code if post_code
b.Country country if country
b.AttentionTo attention_to if attention_to
}
end

def self.from_xml(address_element)
address = Address.new
address_element.children.each do |element|
case(element.name)
when "AddressType" then address.address_type = element.text
when "AddressType" then address.address_type = element.text
when "AddressLine1" then address.line_1 = element.text
when "AddressLine2" then address.line_2 = element.text
when "AddressLine3" then address.line_3 = element.text
when "AddressLine4" then address.line_4 = element.text
when "City" then address.city = element.text
when "Region" then address.region = element.text
when "PostalCode" then address.post_code = element.text
when "Country" then address.country = element.text
when "AddressLine4" then address.line_4 = element.text
when "City" then address.city = element.text
when "Region" then address.region = element.text
when "PostalCode" then address.post_code = element.text
when "Country" then address.country = element.text
when "AttentionTo" then address.attention_to = element.text
end
end
address
end

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

parts = string.split("\r\n")

if(parts.size > 3)
parts = [parts.shift, parts.shift, parts.shift, parts.join(", ")]
end

parts.each_with_index do |line, index|
address.send("line_#{index+1}=", line)
end
address
end

def ==(other)
[:address_type, :line_1, :line_2, :line_3, :line_4, :city, :region, :post_code, :country].each do |field|
[:address_type, :line_1, :line_2, :line_3, :line_4, :city, :region, :post_code, :country, :attention_to].each do |field|
return false if send(field) != other.send(field)
end
return true
Expand Down
34 changes: 34 additions & 0 deletions test/unit/address_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require File.join(File.dirname(__FILE__), '../test_helper.rb')

class AddressTest < Test::Unit::TestCase

test "build and parse XML" do
address = create_test_address

# Generate the XML message
address_xml = address.to_xml

# Parse the XML message and retrieve the address element
address_element = REXML::XPath.first(REXML::Document.new(address_xml), "/Address")

# Build a new contact from the XML
result_address = XeroGateway::Address.from_xml(address_element)

# Check the contact details
assert_equal address, result_address
end

private

def create_test_address
XeroGateway::Address.new(
line_1: "25 Taranaki St",
line_2: "Te Aro",
city: "Wellington",
post_code: "6011",
country: "New Zealand",
attention_to: "Hashigo Zake"
)
end

end
30 changes: 15 additions & 15 deletions test/unit/contact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ class ContactTest < Test::Unit::TestCase
def setup
@schema = LibXML::XML::Schema.document(LibXML::XML::Document.file(File.join(File.dirname(__FILE__), '../xsd/create_contact.xsd')))
end

# Tests that the XML generated from a contact object validates against the Xero XSD
def test_build_xml
contact = create_test_contact

message = contact.to_xml

# Check that the document matches the XSD
assert LibXML::XML::Parser.string(message).parse.validate_schema(@schema), "The XML document generated did not validate against the XSD"
end

# Tests that a contact can be converted into XML that Xero can understand, and then converted back to a contact
def test_build_and_parse_xml
contact = create_test_contact

# Generate the XML message
contact_as_xml = contact.to_xml

Expand All @@ -27,17 +27,17 @@ def test_build_and_parse_xml

# Build a new contact from the XML
result_contact = XeroGateway::Contact.from_xml(contact_element)

# Check the contact details
assert_equal contact, result_contact
end

# Test Contact#add_address helper creates a valid XeroGateway::Contact object with the passed in values
# and appends it to the Contact#addresses attribute.
def test_add_address_helper
contact = create_test_contact
assert_equal(1, contact.addresses.size)

new_values = {
:address_type => 'POBOX',
:line_1 => 'NEW LINE 1',
Expand All @@ -50,26 +50,26 @@ def test_add_address_helper
:country => 'Australia'
}
contact.add_address(new_values)

assert_equal(2, contact.addresses.size)
assert_kind_of(XeroGateway::Address, contact.addresses.last)
new_values.each { |k,v| assert_equal(v, contact.addresses.last.send("#{k}")) }
end

# Test Contact#add_phone helper creates a valid XeroGateway::Phone object with the passed in values
# and appends it to the Contact#phones attribute.
def test_add_address_helper2
def test_add_phone_helper
contact = create_test_contact
assert_equal(1, contact.phones.size)

new_values = {
:phone_type => 'MOBILE',
:country_code => '61',
:area_code => '406',
:number => '123456'
}
contact.add_phone(new_values)

assert_equal(2, contact.phones.size)
assert_kind_of(XeroGateway::Phone, contact.phones.last)
new_values.each { |k,v| assert_equal(v, contact.phones.last.send("#{k}")) }
Expand All @@ -84,7 +84,7 @@ def test_valid_phone_number
})
assert(phone.valid?)
end

def test_invalid_phone_number
phone = XeroGateway::Phone.new({
:phone_type => 'MOBILE',
Expand All @@ -94,9 +94,9 @@ def test_invalid_phone_number
})
assert(!phone.valid?)
end

private

def create_test_contact
contact = XeroGateway::Contact.new(:contact_id => "55555")
contact.contact_number = "aaa111"
Expand All @@ -113,4 +113,4 @@ def create_test_contact

contact
end
end
end

0 comments on commit 6e8cd13

Please sign in to comment.