From 2ae189ffc9da3a80f834c45a9cf12952877b0bc2 Mon Sep 17 00:00:00 2001 From: Nik Wakelin Date: Thu, 15 Sep 2016 20:54:54 +0100 Subject: [PATCH] adds attention to field to contact addresses --- lib/xero_gateway/address.rb | 56 +++++++++++++++++++------------------ test/unit/address_test.rb | 34 ++++++++++++++++++++++ test/unit/contact_test.rb | 30 ++++++++++---------- 3 files changed, 78 insertions(+), 42 deletions(-) create mode 100644 test/unit/address_test.rb diff --git a/lib/xero_gateway/address.rb b/lib/xero_gateway/address.rb index d0f2a169..de4e89bb 100644 --- a/lib/xero_gateway/address.rb +++ b/lib/xero_gateway/address.rb @@ -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 @@ -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 diff --git a/test/unit/address_test.rb b/test/unit/address_test.rb new file mode 100644 index 00000000..a7c66605 --- /dev/null +++ b/test/unit/address_test.rb @@ -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 diff --git a/test/unit/contact_test.rb b/test/unit/contact_test.rb index 21503955..3939de7f 100644 --- a/test/unit/contact_test.rb +++ b/test/unit/contact_test.rb @@ -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 @@ -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', @@ -50,7 +50,7 @@ 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}")) } @@ -58,10 +58,10 @@ def test_add_address_helper # 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', @@ -69,7 +69,7 @@ def test_add_address_helper2 :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}")) } @@ -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', @@ -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" @@ -113,4 +113,4 @@ def create_test_contact contact end -end \ No newline at end of file +end