Class: FriendlyShipping::Services::Usps::ChoosePackageRate

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/usps/choose_package_rate.rb

Defined Under Namespace

Classes: CannotDetermineRate

Constant Summary collapse

FLAT_RATE_BOX =

Some shipping rates use 'Flat Rate Boxes', indicating that they are available for ALL flat rate boxes.

/Flat Rate Box/i

Class Method Summary collapse

Class Method Details

.call(shipping_method, rates, package_options) ⇒ FriendlyShipping::Rate

Select the corresponding rate for a package from all the rates USPS returns to us

Parameters:

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/friendly_shipping/services/usps/choose_package_rate.rb', line 19

def self.call(shipping_method, rates, package_options)
  # Keep all rates with the requested shipping method
  rates_with_this_shipping_method = rates.select { |r| r.shipping_method == shipping_method }

  # Keep only rates with the package type of this package
  rates_with_this_package_type = rates_with_this_shipping_method.select do |r|
    r.data[:box_name] == package_options.box_name
  end

  # Filter by our package's `hold_for_pickup` option
  rates_with_this_hold_for_pickup_option = rates_with_this_package_type.select do |r|
    r.data[:hold_for_pickup] == package_options.hold_for_pickup
  end

  # At this point, we have one or two rates left, and they're similar enough.
  # Once this poses an actual problem, we'll fix it.
  rates_with_this_hold_for_pickup_option.first
end