Sha256: 76fd0ba4f17a6519b5b3be1b3027230f649ef521cbaa1beb887f68efccbeb049

Contents?: true

Size: 1.58 KB

Versions: 62

Compression:

Stored size: 1.58 KB

Contents

module Workarea
  class Shipping
    class RateLookup
      attr_reader :origin, :destination, :packages, :options

      def initialize(origin, destination, packages, options = {})
        @origin = origin
        @destination = destination
        @packages = packages
        @options = options
      end

      def localized_services
        @localized_services ||= if destination.present?
                                  Shipping::Service.for_location(
                                    destination.country,
                                    destination.region
                                  )
                                else
                                  Shipping::Service.all.to_a
                                end
      end

      def subtotal
        @subtotal ||= packages.map { |p| Money.new(p.value, p.currency) }.sum
      end

      def valid_services
        localized_services & Shipping::Service.by_price(subtotal)
      end

      def response
        ActiveShipping::RateResponse.new(
          true, # success
          'success', # message
          {}, # params
          rates: valid_services.map do |service|
            total = service.find_rate(subtotal).try(:price)
            next unless total.present?

            ActiveShipping::RateEstimate.new(
              origin,
              destination,
              service.carrier,
              service.name,
              service_code: service.service_code,
              total_price: total.cents,
              currency: total.currency.to_s
            )
          end.compact
        )
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.26 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.4.45 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.25 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.23 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.4.44 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.22 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.4.43 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.21 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.4.42 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.20 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.4.41 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.19 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.4.40 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.18 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.4.39 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.17 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.4.38 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.5.16 app/models/workarea/shipping/rate_lookup.rb
workarea-core-3.4.37 app/models/workarea/shipping/rate_lookup.rb