Class: AddressValidator::Address

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

Constant Summary collapse

CLASSIFICATION_UNKNOWN =
0
CLASSIFICATION_COMMERCIAL =
1
CLASSIFICATION_RESIDENTIAL =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: name(), street1: street1(), street2: street2(), street3: street3(), city: city(), state: state(), zip: zip(), country: country(), classification: classification()) ⇒ Address

Returns a new instance of Address.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/address_validator/address.rb', line 41

def initialize(name: name(), street1: street1(), street2: street2(), street3: street3(), city: city(), state: state(), zip: zip(), country: country(), classification: classification())
  @name = name
  @street1 = street1
  @street2 = street2
  @street3 = street3
  @city = city
  @state = state
  @zip = zip
  @country = country
  @classification = (classification || CLASSIFICATION_UNKNOWN).to_i
end

Instance Attribute Details

#cityObject

Returns the value of attribute city.



39
40
41
# File 'lib/address_validator/address.rb', line 39

def city
  @city
end

#classificationObject

Returns the value of attribute classification.



39
40
41
# File 'lib/address_validator/address.rb', line 39

def classification
  @classification
end

#countryObject

Returns the value of attribute country.



39
40
41
# File 'lib/address_validator/address.rb', line 39

def country
  @country
end

#nameObject

Returns the value of attribute name.



39
40
41
# File 'lib/address_validator/address.rb', line 39

def name
  @name
end

#stateObject

Returns the value of attribute state.



39
40
41
# File 'lib/address_validator/address.rb', line 39

def state
  @state
end

#street1Object

Returns the value of attribute street1.



39
40
41
# File 'lib/address_validator/address.rb', line 39

def street1
  @street1
end

#street2Object

Returns the value of attribute street2.



39
40
41
# File 'lib/address_validator/address.rb', line 39

def street2
  @street2
end

#street3Object

Returns the value of attribute street3.



39
40
41
# File 'lib/address_validator/address.rb', line 39

def street3
  @street3
end

#zipObject

Returns the value of attribute zip.



39
40
41
# File 'lib/address_validator/address.rb', line 39

def zip
  @zip
end

Class Method Details

.from_xml(attrs = {}) ⇒ Object

Public: Build an address from the API’s response xml.

attrs - Hash of address attributes.

Returns an address.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/address_validator/address.rb', line 15

def from_xml(attrs = {})
  classification = nil

  if _classification = attrs['AddressClassification']
    classification = _classification['Code']
  end

  # AddressLine can come in as a single element, or upto 3 elements according
  # to the UPS docs, so we force it into an array and split them up
  address_lines = Array(attrs['AddressLine'])

  new(
    street1: address_lines[0],
    street2: address_lines[1],
    street3: address_lines[2],
    city: attrs['PoliticalDivision2'],
    state: attrs['PoliticalDivision1'],
    zip: attrs['PostcodePrimaryLow'],
    country: attrs['CountryCode'],
    classification: classification
  )
end

Instance Method Details

#commercial?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/address_validator/address.rb', line 57

def commercial?
  classification == CLASSIFICATION_COMMERCIAL
end

#residential?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/address_validator/address.rb', line 53

def residential?
  classification == CLASSIFICATION_RESIDENTIAL
end

#to_xml(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/address_validator/address.rb', line 61

def to_xml(options={})

  xml = Builder::XmlMarkup.new(options)

  xml.AddressKeyFormat do
    xml.ConsigneeName(self.name)
    xml.tag! 'AddressLine', self.street1
    xml.tag! 'AddressLine', self.street2 if self.street2
    xml.tag! 'AddressLine', self.street3 if self.street3
    xml.PoliticalDivision2(self.city)
    xml.PoliticalDivision1(self.state)
    xml.PostcodePrimaryLow(self.zip)
    xml.CountryCode(self.country)
  end

  xml.target!
end