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_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
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
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_2 ⇒ Ox::Element
Returns an XML representation of address_line_2
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 |
#city ⇒ Ox::Element
Returns an XML representation of city
94 95 96 |
# File 'lib/ups/builders/address_builder.rb', line 94 def city element_with_value('City', opts[:city][0..29]) end |
#country ⇒ Ox::Element
Returns an XML representation of country
115 116 117 |
# File 'lib/ups/builders/address_builder.rb', line 115 def country element_with_value('CountryCode', opts[:country][0..1]) end |
#email_address ⇒ Object
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
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
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_code ⇒ Ox::Element
Returns an XML representation of postal_code
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 |
#state ⇒ Ox::Element
Returns an XML representation of state
101 102 103 |
# File 'lib/ups/builders/address_builder.rb', line 101 def state element_with_value('StateProvinceCode', opts[:state]) end |
#to_xml ⇒ Ox::Element
Returns an XML representation of a UPS Address
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 |
#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 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 |