Class: UPS::Builders::AddressBuilder

Inherits:
BuilderBase show all
Includes:
Ox
Defined in:
lib/ups/builders/address_builder.rb

Overview

The AddressBuilder class builds UPS XML Address Objects.

Author:

  • Paul Trippett

Since:

  • 0.1.0

Instance Attribute Summary collapse

Attributes inherited from BuilderBase

#access_request, #document, #license_number, #password, #root, #shipment_root, #user_id

Instance Method Summary collapse

Methods inherited from BuilderBase

#add_access_request, #add_package, #add_payment_information, #add_rate_information, #add_request, #add_ship_from, #add_ship_to, #add_shipper, #add_sold_to

Constructor Details

#initialize(opts = {}) ⇒ AddressBuilder

Initializes a new UPS::Builders::AddressBuilder object

Parameters:

  • opts (Hash) (defaults to: {})

    The Address Parts

Options Hash (opts):

  • :address_line_1 (String)

    Address Line 1

  • :city (String)

    City

  • :state (String)

    State

  • :postal_code (String)

    Zip or Postal Code

  • :country (String)

    Country

Raises:

Since:

  • 0.1.0



25
26
27
28
# File 'lib/ups/builders/address_builder.rb', line 25

def initialize(opts = {})
  self.opts = opts
  validate
end

Instance Attribute Details

#optsHash

The Address Parts

Returns:

  • (Hash)

    the current value of opts

Since:

  • 0.1.0



10
11
12
# File 'lib/ups/builders/address_builder.rb', line 10

def opts
  @opts
end

Instance Method Details

#address_line_1Ox::Element

Returns an XML representation of address_line_1

Returns:

  • (Ox::Element)

    XML representation of address_line_1 address part

Since:

  • 0.1.0



75
76
77
# File 'lib/ups/builders/address_builder.rb', line 75

def address_line_1
  element_with_value('AddressLine1', opts[:address_line_1][0..34])
end

#address_line_2Ox::Element

Returns an XML representation of address_line_2

Returns:

  • (Ox::Element)

    XML representation of address_line_2 address part

Since:

  • 0.1.0



82
83
84
85
# File 'lib/ups/builders/address_builder.rb', line 82

def address_line_2
  data = (opts.key? :address_line_2) ? opts[:address_line_2][0..34] : ''
  element_with_value('AddressLine2', data)
end

#cityOx::Element

Returns an XML representation of city

Returns:

  • (Ox::Element)

    XML representation of the city address part

Since:

  • 0.1.0



90
91
92
# File 'lib/ups/builders/address_builder.rb', line 90

def city
  element_with_value('City', opts[:city][0..29])
end

#countryOx::Element

Returns an XML representation of country

Returns:

  • (Ox::Element)

    XML representation of the country address part

Since:

  • 0.1.0



111
112
113
# File 'lib/ups/builders/address_builder.rb', line 111

def country
  element_with_value('CountryCode', opts[:country][0..1])
end

#email_addressObject

Since:

  • 0.1.0



115
116
117
# File 'lib/ups/builders/address_builder.rb', line 115

def email_address
  element_with_value('EmailAddress', opts[:email_address][0..49])
end

#normalize_ca_state(state) ⇒ String

Changes :state based on UPS requirements for CA Addresses

Parameters:

  • state (String)

    The CA State to normalize

Returns:

  • (String)

Since:

  • 0.1.0



64
65
66
67
68
69
70
# File 'lib/ups/builders/address_builder.rb', line 64

def normalize_ca_state(state)
  if state.to_str.length > 2
    UPS::Data::CANADIAN_STATES[state] || state
  else
    state.upcase
  end
end

#normalize_us_state(state) ⇒ String

Changes :state based on UPS requirements for US Addresses

Parameters:

  • state (String)

    The US State to normalize

Returns:

  • (String)

Since:

  • 0.1.0



52
53
54
55
56
57
58
# File 'lib/ups/builders/address_builder.rb', line 52

def normalize_us_state(state)
  if state.to_str.length > 2
    UPS::Data::US_STATES[state] || state
  else
    state.upcase
  end
end

#postal_codeOx::Element

Returns an XML representation of postal_code

Returns:

  • (Ox::Element)

    XML representation of the postal_code address part

Since:

  • 0.1.0



104
105
106
# File 'lib/ups/builders/address_builder.rb', line 104

def postal_code
  element_with_value('PostalCode', opts[:postal_code][0..9])
end

#stateOx::Element

Returns an XML representation of state

Returns:

  • (Ox::Element)

    XML representation of the state address part

Since:

  • 0.1.0



97
98
99
# File 'lib/ups/builders/address_builder.rb', line 97

def state
  element_with_value('StateProvinceCode', opts[:state])
end

#to_xmlOx::Element

Returns an XML representation of a UPS Address

Returns:

  • (Ox::Element)

    XML representation of the current object

Since:

  • 0.1.0



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ups/builders/address_builder.rb', line 122

def to_xml
  Element.new('Address').tap do |address|
    address << address_line_1
    address << address_line_2
    address << email_address if opts[:email_address]
    address << city
    address << state
    address << postal_code
    address << country
  end
end

#validatevoid

This method returns an undefined value.

Changes :state part of the address based on UPS requirements

Raises:

Since:

  • 0.1.0



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ups/builders/address_builder.rb', line 35

def validate
  opts[:state] = case opts[:country].downcase
                 when 'us'
                   normalize_us_state(opts[:state])
                 when 'ca'
                   normalize_ca_state(opts[:state])
                 when 'ie'
                   UPS::Data.ie_state_matcher(opts[:state])
                 else
                   ''
                 end
end