Class: UPS::Builders::AddressBuilder
- Inherits:
-
BuilderBase
- Object
- BuilderBase
- UPS::Builders::AddressBuilder
- Includes:
- Ox
- Defined in:
- lib/ups/builders/address_builder.rb
Overview
The AddressBuilder class builds UPS XML Address Objects.
Instance Attribute Summary collapse
-
#opts ⇒ Hash
The Address Parts.
Attributes inherited from BuilderBase
#access_request, #document, #license_number, #password, #root, #shipment_root, #user_id
Instance Method Summary collapse
-
#address_line_1 ⇒ Ox::Element
Returns an XML representation of address_line_1.
-
#address_line_2 ⇒ Ox::Element
Returns an XML representation of address_line_2.
-
#city ⇒ Ox::Element
Returns an XML representation of city.
-
#country ⇒ Ox::Element
Returns an XML representation of country.
- #email_address ⇒ Object
-
#initialize(opts = {}) ⇒ AddressBuilder
constructor
Initializes a new AddressBuilder object.
-
#normalize_ca_state(state) ⇒ String
Changes :state based on UPS requirements for CA Addresses.
-
#normalize_us_state(state) ⇒ String
Changes :state based on UPS requirements for US Addresses.
-
#postal_code ⇒ Ox::Element
Returns an XML representation of postal_code.
-
#state ⇒ Ox::Element
Returns an XML representation of state.
-
#to_xml ⇒ Ox::Element
Returns an XML representation of a UPS Address.
-
#validate ⇒ void
Changes :state part of the address based on UPS requirements.
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
25 26 27 28 |
# File 'lib/ups/builders/address_builder.rb', line 25 def initialize(opts = {}) self.opts = opts validate end |
Instance Attribute Details
#opts ⇒ Hash
The Address Parts
10 11 12 |
# File 'lib/ups/builders/address_builder.rb', line 10 def opts @opts end |
Instance Method Details
#address_line_1 ⇒ Ox::Element
Returns an XML representation of address_line_1
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_2 ⇒ Ox::Element
Returns an XML representation of address_line_2
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 |
#city ⇒ Ox::Element
Returns an XML representation of city
90 91 92 |
# File 'lib/ups/builders/address_builder.rb', line 90 def city element_with_value('City', opts[:city][0..29]) end |
#country ⇒ Ox::Element
Returns an XML representation of country
111 112 113 |
# File 'lib/ups/builders/address_builder.rb', line 111 def country element_with_value('CountryCode', opts[:country][0..1]) end |
#email_address ⇒ Object
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
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
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_code ⇒ Ox::Element
Returns an XML representation of postal_code
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 |
#state ⇒ Ox::Element
Returns an XML representation of state
97 98 99 |
# File 'lib/ups/builders/address_builder.rb', line 97 def state element_with_value('StateProvinceCode', opts[:state]) end |
#to_xml ⇒ Ox::Element
Returns an XML representation of a UPS Address
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 |
#validate ⇒ void
This method returns an undefined value.
Changes :state part of the address based on UPS requirements
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 |