Class: XeroGateway::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/xero_gateway/address.rb

Constant Summary collapse

ADDRESS_TYPE =
{
  'STREET' =>     'Street',
  'POBOX' =>      'PO Box'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Address

Returns a new instance of Address.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/xero_gateway/address.rb', line 14

def initialize(params = {})
  @errors ||= []
  
  params = {
    :address_type => "POBOX"
  }.merge(params)
  
  params.each do |k,v|
    self.send("#{k}=", v)
  end
end

Instance Attribute Details

#address_typeObject

Returns the value of attribute address_type.



12
13
14
# File 'lib/xero_gateway/address.rb', line 12

def address_type
  @address_type
end

#cityObject

Returns the value of attribute city.



12
13
14
# File 'lib/xero_gateway/address.rb', line 12

def city
  @city
end

#countryObject

Returns the value of attribute country.



12
13
14
# File 'lib/xero_gateway/address.rb', line 12

def country
  @country
end

#errorsObject (readonly)

Any errors that occurred when the #valid? method called.



10
11
12
# File 'lib/xero_gateway/address.rb', line 10

def errors
  @errors
end

#line_1Object

Returns the value of attribute line_1.



12
13
14
# File 'lib/xero_gateway/address.rb', line 12

def line_1
  @line_1
end

#line_2Object

Returns the value of attribute line_2.



12
13
14
# File 'lib/xero_gateway/address.rb', line 12

def line_2
  @line_2
end

#line_3Object

Returns the value of attribute line_3.



12
13
14
# File 'lib/xero_gateway/address.rb', line 12

def line_3
  @line_3
end

#line_4Object

Returns the value of attribute line_4.



12
13
14
# File 'lib/xero_gateway/address.rb', line 12

def line_4
  @line_4
end

#post_codeObject

Returns the value of attribute post_code.



12
13
14
# File 'lib/xero_gateway/address.rb', line 12

def post_code
  @post_code
end

#regionObject

Returns the value of attribute region.



12
13
14
# File 'lib/xero_gateway/address.rb', line 12

def region
  @region
end

Class Method Details

.from_xml(address_element) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/xero_gateway/address.rb', line 56

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 "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
    end
  end
  address
end

.parse(string) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/xero_gateway/address.rb', line 74

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

Instance Method Details

#==(other) ⇒ Object



89
90
91
92
93
94
# File 'lib/xero_gateway/address.rb', line 89

def ==(other)
  [:address_type, :line_1, :line_2, :line_3, :line_4, :city, :region, :post_code, :country].each do |field|
    return false if send(field) != other.send(field)
  end
  return true
end

#to_xml(b = Builder::XmlMarkup.new) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/xero_gateway/address.rb', line 42

def to_xml(b = Builder::XmlMarkup.new)
  b.Address {
    b.AddressType address_type
    b.AddressLine1 line_1 if line_1
    b.AddressLine2 line_2 if line_2
    b.AddressLine3 line_3 if line_3
    b.AddressLine4 line_4 if line_4
    b.City city if city
    b.Region region if region
    b.PostalCode post_code if post_code
    b.Country country if country
  }
end

#valid?Boolean

Validate the Address record according to what will be valid by the gateway.

Usage:

address.valid?     # Returns true/false

Additionally sets address.errors array to an array of field/error.

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
# File 'lib/xero_gateway/address.rb', line 32

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