Sha256: d541c515bcedc04b968747da763c718ff53f159c824aa699c7d513e9902bf244

Contents?: true

Size: 1.15 KB

Versions: 56

Compression:

Stored size: 1.15 KB

Contents

# The time a 3rd-party shipping provider takes to respond to a request varies greatly.
# This class simulates these unpredictable delays in shipping rate retrieval so that
# load-testing tools run into situations that more accurately reflect the real world.

module ActiveShipping
  class BenchmarkCarrier < Carrier
    cattr_reader :name
    @@name = "Benchmark Carrier"

    def find_rates(origin, destination, packages, options = {})
      origin = Location.from(origin)
      destination = Location.from(destination)
      packages = Array(packages)

      delay_time = generate_simulated_lag

      bogus_estimate = RateEstimate.new(
        origin, destination, @@name,
        "Free Benchmark Shipping", :total_price => 0, :currency => 'USD',
                                   :packages => packages, :delivery_range => [Time.now.utc.strftime("%Y-%d-%m"), Time.now.utc.strftime("%Y-%d-%m")]
        )
      RateResponse.new(true, "Success (delayed #{delay_time} seconds)", {:rate => 'free'}, :rates => [bogus_estimate], :xml => "<rate>free</rate>")
    end

    private

    def generate_simulated_lag(max_delay = 30)
      sleep Random.rand * max_delay
    end
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
active_shipping-2.1.1 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-2.1.0 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-2.0.1 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-2.0.0 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.14.2 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.14.1 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.14.0 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.13.4 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.13.3 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.13.2 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.13.1 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.13.0 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.12.1 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.12.0 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.11.1 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.11.0 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.10.1 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.9.2 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.9.1 lib/active_shipping/carriers/benchmark_carrier.rb
active_shipping-1.9.0 lib/active_shipping/carriers/benchmark_carrier.rb