class UPS::Builders::AddressBuilder

The {AddressBuilder} class builds UPS XML Address Objects.

@author Paul Trippett @since 0.1.0 @attr [Hash] opts The Address Parts

Attributes

opts[RW]

Public Class Methods

new(opts = {}) click to toggle source

Initializes a new {AddressBuilder} object

@param [Hash] opts The Address Parts @option opts [String] :address_line_1 Address Line 1 @option opts [String] :city City @option opts [String] :state State @option opts [String] :postal_code Zip or Postal Code @option opts [String] :country Country @raise [InvalidAttributeError] If the passed :state is nil or an

empty string and the :country is IE
# File lib/ups/builders/address_builder.rb, line 25
def initialize(opts = {})
  self.opts = opts
  validate
end

Public Instance Methods

address_line_1() click to toggle source

Returns an XML representation of address_line_1

@return [Ox::Element] XML representation of address_line_1 address part

# 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_2() click to toggle source

Returns an XML representation of address_line_2

@return [Ox::Element] XML representation of address_line_2 address part

# 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
city() click to toggle source

Returns an XML representation of city

@return [Ox::Element] XML representation of the city address part

# File lib/ups/builders/address_builder.rb, line 94
def city
  element_with_value('City', opts[:city][0..29])
end
country() click to toggle source

Returns an XML representation of country

@return [Ox::Element] XML representation of the country address part

# File lib/ups/builders/address_builder.rb, line 115
def country
  element_with_value('CountryCode', opts[:country][0..1])
end
email_address() click to toggle source
# 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) click to toggle source

Changes :state based on UPS requirements for CA Addresses

@param [String] state The CA State to normalize @return [String]

# 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) click to toggle source

Changes :state based on UPS requirements for US Addresses

@param [String] state The US State to normalize @return [String]

# 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_code() click to toggle source

Returns an XML representation of postal_code

@return [Ox::Element] XML representation of the postal_code address part

# File lib/ups/builders/address_builder.rb, line 108
def postal_code
  element_with_value('PostalCode', opts[:postal_code][0..9])
end
state() click to toggle source

Returns an XML representation of state

@return [Ox::Element] XML representation of the state address part

# File lib/ups/builders/address_builder.rb, line 101
def state
  element_with_value('StateProvinceCode', opts[:state])
end
to_xml() click to toggle source

Returns an XML representation of a UPS Address

@return [Ox::Element] XML representation of the current object

# 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
validate() click to toggle source

Changes :state part of the address based on UPS requirements

@raise [InvalidAttributeError] If the passed :state is nil or an

empty string and the :country is IE

@return [void]

# 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