class UPS::Builders::BuilderBase

The {BuilderBase} class builds UPS XML Address Objects.

@author Paul Trippett @since 0.1.0 @abstract @attr [Ox::Document] document The XML Document being built @attr [Ox::Element] root The XML Root @attr [Ox::Element] shipment_root The XML Shipment Element @attr [Ox::Element] access_request The XML AccessRequest Element @attr [String] license_number The UPS API Key @attr [String] user_id The UPS Username @attr [String] password The UPS Password

Attributes

access_request[RW]
document[RW]
license_number[RW]
password[RW]
root[RW]
shipment_root[RW]
user_id[RW]

Public Class Methods

new(root_name) { |self| ... } click to toggle source

Initializes a new {BuilderBase} object

@param [String] root_name The Name of the XML Root @return [void]

# File lib/ups/builders/builder_base.rb, line 33
def initialize(root_name)
  initialize_xml_roots root_name

  document << access_request
  document << root

  yield self if block_given?
end

Public Instance Methods

add_access_request(license_number, user_id, password) click to toggle source

Initializes a new {BuilderBase} object

@param [String] license_number The UPS API Key @param [String] user_id The UPS Username @param [String] password The UPS Password @return [void]

# File lib/ups/builders/builder_base.rb, line 48
def add_access_request(license_number, user_id, password)
  self.license_number = license_number
  self.user_id = user_id
  self.password = password

  access_request << element_with_value('AccessLicenseNumber',
                                       license_number)
  access_request << element_with_value('UserId', user_id)
  access_request << element_with_value('Password', password)
end
add_insurance_charge(value) click to toggle source

Adds an InsuranceCharges section to the XML document being built

@param [String] value The MonetaryValue of the InsuranceCharge @return [void]

# File lib/ups/builders/builder_base.rb, line 63
def add_insurance_charge(value)
  shipment_root << insurance_charge(value)
end
add_itemized_payment_information(ship_number) click to toggle source
# File lib/ups/builders/builder_base.rb, line 167
def add_itemized_payment_information(ship_number)
  shipment_charge << Element.new('BillShipper').tap do |bill_shipper|
    bill_shipper << element_with_value('AccountNumber', ship_number)
  end
end
add_master_carton_id(master_carton_id) click to toggle source

Adds MasterCartonID to the shipment

@return [void]

# File lib/ups/builders/builder_base.rb, line 211
def add_master_carton_id(master_carton_id)
  shipment_root << element_with_value('MasterCartonID', master_carton_id)
end
add_master_carton_indicator() click to toggle source

Adds MasterCartonIndicator to the shipment

@return [void]

# File lib/ups/builders/builder_base.rb, line 204
def add_master_carton_indicator
  shipment_root << Element.new('MasterCartonIndicator')
end
add_package(opts = {}) click to toggle source

Adds a Package section to the XML document being built

@param [Hash] opts A Hash of data to build the requested section @return [void]

# File lib/ups/builders/builder_base.rb, line 149
def add_package(opts = {})
  shipment_root << PackageBuilder.new('Package', opts).to_xml
end
add_payment_information(ship_number) click to toggle source

Adds a PaymentInformation section to the XML document being built

@param [String] ship_number The UPS Shipper Number @return [void]

# File lib/ups/builders/builder_base.rb, line 157
def add_payment_information(ship_number)
  shipment_root << Element.new('PaymentInformation').tap do |payment|
    payment << Element.new('Prepaid').tap do |prepaid|
      prepaid << Element.new('BillShipper').tap do |bill_shipper|
        bill_shipper << element_with_value('AccountNumber', ship_number)
      end
    end
  end
end
add_rate_information() click to toggle source

Adds a RateInformation/NegotiatedRatesIndicator section to the XML document being built

@return [void]

# File lib/ups/builders/builder_base.rb, line 177
def add_rate_information
  shipment_root << Element.new('RateInformation').tap do |rate_info|
    rate_info << element_with_value('NegotiatedRatesIndicator', '1')
  end
end
add_request(action, option, sub_version: nil) click to toggle source

Adds a Request section to the XML document being built

@param [String] action The UPS API Action requested @param [String] option The UPS API Option @return [void]

# File lib/ups/builders/builder_base.rb, line 72
def add_request(action, option, sub_version: nil)
  root << Element.new('Request').tap do |request|
    request << element_with_value('RequestAction', action)
    request << element_with_value('RequestOption', option)

    unless sub_version.nil?
      request << element_with_value('SubVersion', sub_version)
    end
  end
end
add_ship_from(opts = {}) click to toggle source

Adds a ShipFrom section to the XML document being built

@param [Hash] opts A Hash of data to build the requested section @option opts [String] :company_name Company Name @option opts [String] :phone_number Phone Number @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 @option opts [String] :shipper_number UPS Account Number @return [void]

# File lib/ups/builders/builder_base.rb, line 141
def add_ship_from(opts = {})
  shipment_root << OrganisationBuilder.new('ShipFrom', opts).to_xml
end
add_ship_to(opts = {}) click to toggle source

Adds a ShipTo section to the XML document being built

@param [Hash] opts A Hash of data to build the requested section @option opts [String] :company_name Company Name @option opts [String] :phone_number Phone Number @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 @return [void]

# File lib/ups/builders/builder_base.rb, line 110
def add_ship_to(opts = {})
  shipment_root << OrganisationBuilder.new('ShipTo', opts).to_xml
end
add_shipment_delivery_confirmation(dcis_type) click to toggle source

Adds a Delivery Confirmation DCIS Type to the shipment service options

@param [String] dcis_type DCIS type @return [void]

# File lib/ups/builders/builder_base.rb, line 187
def add_shipment_delivery_confirmation(dcis_type)
  shipment_service_options <<
    Element.new('DeliveryConfirmation').tap do |delivery_confirmation|
      delivery_confirmation << element_with_value('DCISType', dcis_type)
    end
end
add_shipment_direct_delivery_only() click to toggle source

Adds Direct Delivery Only indicator to the shipment service options

@return [void]

# File lib/ups/builders/builder_base.rb, line 197
def add_shipment_direct_delivery_only
  shipment_service_options << Element.new('DirectDeliveryOnlyIndicator')
end
add_shipper(opts = {}) click to toggle source

Adds a Shipper section to the XML document being built

@param [Hash] opts A Hash of data to build the requested section @option opts [String] :company_name Company Name @option opts [String] :phone_number Phone Number @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 @option opts [String] :shipper_number UPS Account Number @return [void]

# File lib/ups/builders/builder_base.rb, line 95
def add_shipper(opts = {})
  shipment_root << ShipperBuilder.new(opts).to_xml
end
add_sold_to(opts = {}) click to toggle source

Adds a SoldTo section to the XML document being built

@param [Hash] opts A Hash of data to build the requested section @option opts [String] :company_name Company Name @option opts [String] :phone_number Phone Number @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 @return [void]

# File lib/ups/builders/builder_base.rb, line 125
def add_sold_to(opts = {})
  shipment_root << OrganisationBuilder.new('SoldTo', opts).to_xml
end
to_xml() click to toggle source

Returns a String representation of the XML document being built

@return [String]

# File lib/ups/builders/builder_base.rb, line 218
def to_xml
  Ox.to_xml document
end

Private Instance Methods

code_description(name, code, description) click to toggle source
# File lib/ups/builders/builder_base.rb, line 261
def code_description(name, code, description)
  multi_valued(name, Code: code, Description: description)
end
element_with_value(name, value) click to toggle source
# File lib/ups/builders/builder_base.rb, line 254
def element_with_value(name, value)
  fail InvalidAttributeError, name unless value.respond_to?(:to_str)
  Element.new(name).tap do |request_action|
    request_action << value.to_str
  end
end
initialize_xml_roots(root_name) click to toggle source
# File lib/ups/builders/builder_base.rb, line 224
def initialize_xml_roots(root_name)
  self.document = Document.new
  self.root = Element.new(root_name)
  self.shipment_root = Element.new('Shipment')
  self.access_request = Element.new('AccessRequest')
  root << shipment_root
end
insurance_charge(value) click to toggle source
# File lib/ups/builders/builder_base.rb, line 265
def insurance_charge(value)
  multi_valued('InsuranceCharges', MonetaryValue: value)
end
multi_valued(name, params) click to toggle source
# File lib/ups/builders/builder_base.rb, line 269
def multi_valued(name, params)
  Element.new(name).tap do |e|
    params.each { |key, value| e << element_with_value(key, value) }
  end
end
shipment_charge() click to toggle source
# File lib/ups/builders/builder_base.rb, line 240
def shipment_charge
  @shipment_charge ||= begin
    element = Element.new('ShipmentCharge')
    shipment_root << (Element.new('ItemizedPaymentInformation') << element)
    element
  end
end
shipment_service_options() click to toggle source
# File lib/ups/builders/builder_base.rb, line 232
def shipment_service_options
  @shipment_service_options ||= begin
    Element.new('ShipmentServiceOptions').tap do |element|
      shipment_root << element
    end
  end
end
unit_of_measurement(unit) click to toggle source
# File lib/ups/builders/builder_base.rb, line 248
def unit_of_measurement(unit)
  Element.new('UnitOfMeasurement').tap do |org|
    org << element_with_value('Code', unit.to_s)
  end
end