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

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


27
28
29
30
# File 'lib/ups/builders/address_builder.rb', line 27

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


12
13
14
# File 'lib/ups/builders/address_builder.rb', line 12

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


77
78
79
# File 'lib/ups/builders/address_builder.rb', line 77

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


84
85
86
87
# File 'lib/ups/builders/address_builder.rb', line 84

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


92
93
94
# File 'lib/ups/builders/address_builder.rb', line 92

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


113
114
115
# File 'lib/ups/builders/address_builder.rb', line 113

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

#email_addressOx::Element

Returns an XML representation of email

Returns:

  • (Ox::Element)

    XML representation of the email address

Since:

  • 0.1.0


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

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


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

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


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

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


106
107
108
# File 'lib/ups/builders/address_builder.rb', line 106

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


99
100
101
# File 'lib/ups/builders/address_builder.rb', line 99

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

#to_xmlOx::Element

Returns an XML representation of a UPS Address

rubocop:disable Metrics/AbcSize

Returns:

  • (Ox::Element)

    XML representation of the current object

Since:

  • 0.1.0


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

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


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

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