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_insurance_charge, #add_itemized_payment_information, #add_master_carton_id, #add_master_carton_indicator, #add_package, #add_payment_information, #add_rate_information, #add_request, #add_ship_from, #add_ship_to, #add_shipment_delivery_confirmation, #add_shipment_direct_delivery_only, #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



79
80
81
# File 'lib/ups/builders/address_builder.rb', line 79

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



86
87
88
89
# File 'lib/ups/builders/address_builder.rb', line 86

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



94
95
96
# File 'lib/ups/builders/address_builder.rb', line 94

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



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

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

#email_addressObject

Since:

  • 0.1.0



119
120
121
# File 'lib/ups/builders/address_builder.rb', line 119

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



68
69
70
71
72
73
74
# File 'lib/ups/builders/address_builder.rb', line 68

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



56
57
58
59
60
61
62
# File 'lib/ups/builders/address_builder.rb', line 56

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



108
109
110
# File 'lib/ups/builders/address_builder.rb', line 108

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



101
102
103
# File 'lib/ups/builders/address_builder.rb', line 101

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



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/ups/builders/address_builder.rb', line 126

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
47
48
49
50
# 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'
    if opts[:skip_ireland_state_validation]
      '_' # UPS requires at least one character for Ireland
    else
      UPS::Data.ie_state_matcher(opts[:state])
    end
  else
    ''
  end
end