Sha256: 5e58361af468138a5d77c46e4553822030b2fc357bf2678123d4dd00da4383bb

Contents?: true

Size: 1.71 KB

Versions: 16

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module FriendlyShipping
  module Services
    class Usps
      class ChoosePackageRate
        class CannotDetermineRate < StandardError; end
        # Some shipping rates use 'Flat Rate Boxes', indicating that
        # they are available for ALL flat rate boxes.
        FLAT_RATE_BOX = /Flat Rate Box/i.freeze

        # Select the corresponding rate for a package from all the rates USPS returns to us
        #
        # @param [FriendlyShipping::ShippingMethod] shipping_method The shipping method we want to filter by
        # @param [Physical::Package] package The package we want to match with a rate
        # @param [Array<FriendlyShipping::Rate>] The rates we select from
        #
        # @return [FriendlyShipping::Rate] The rate that most closely matches our package
        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
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
friendly_shipping-0.8.1 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.8.0 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.7.3 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.7.2 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.7.1 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.7.0 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.6.5 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.6.4 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.6.3 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.6.2 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.6.1 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.6.0 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.5.3 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.5.2 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.5.1 lib/friendly_shipping/services/usps/choose_package_rate.rb
friendly_shipping-0.5 lib/friendly_shipping/services/usps/choose_package_rate.rb