class SimpleShipping::Ups::PackageBuilder

Builds hash structure for Savon that represents package element in UPS’s API.

Constants

CUSTOM_PACKAGE_ORDER

Custom package order.

DIMENSION_UNITS

Mapping for UPS dimension units.

PACKAGING_TYPES

Mapping for UPS packaging types Not all UPS values listed here in order to provide common interface with FedEx.

STANDARD_PACKAGE_ORDER

Standard package order.

WEIGHT_UNITS

Mapping for UPS weight units.

Public Instance Methods

base_package() click to toggle source

Build basic skeleton for package element. It can be customized by overriding some subelements.

@return [Hash]

# File lib/simple_shipping/ups/package_builder.rb, line 39
def base_package
  base = {
    'Packaging' => {
      'Code' => PACKAGING_TYPES[@model.packaging_type]
    },
    'PackageWeight' => {
      'UnitOfMeasurement' => {
        'Code' => WEIGHT_UNITS[@model.weight_units]
      },
      'Weight' => @model.weight,
      :order!  => ['UnitOfMeasurement', 'Weight']
    },
    'PackageServiceOptions' => {}
  }

  if @model.insured_value
    base['PackageServiceOptions']['InsuredValue'] = {
      'CurrencyCode'  => 'USD',
      'MonetaryValue' => @model.insured_value
    }
  end

  if @model.declared_value
    base['PackageServiceOptions']['DeclaredValue'] = {
      'CurrencyCode'  => 'USD',
      'MonetaryValue' => @model.declared_value
    }
  end

  base
end
build() click to toggle source

Build the package, whether custom or standard.

# File lib/simple_shipping/ups/package_builder.rb, line 97
def build
  @model.custom_package? ? custom_package : standard_package
end
custom_package() click to toggle source

Build a hash from a custom {Package package} which will be used by Savon. A custom package requires specification of LWH dimensions.

# File lib/simple_shipping/ups/package_builder.rb, line 73
def custom_package
  base_package.tap do |package|
    package['Dimensions'] = {
      'UnitOfMeasurement' => {
        'Code' => DIMENSION_UNITS[@model.dimension_units].clone
      },
      'Length' => @model.length,
      'Width'  => @model.width,
      'Height' => @model.height,
      :order! => ['UnitOfMeasurement', 'Length', 'Width', 'Height']
    }
    package[:order!] = CUSTOM_PACKAGE_ORDER.clone
  end
end
standard_package() click to toggle source

Build a hash from a standard {Package package} which will be used by Savon. A standard package requires no specification of LWH dimensions.

# File lib/simple_shipping/ups/package_builder.rb, line 90
def standard_package
  base_package.tap do |package|
    package[:order!] = STANDARD_PACKAGE_ORDER.clone
  end
end