Class: ActiveMerchant::Shipping::RateEstimate

Inherits:
Object
  • Object
show all
Defined in:
lib/active_shipping/shipping/rate_estimate.rb

Direct Known Subclasses

StampsRateEstimate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination, carrier, service_name, options = {}) ⇒ RateEstimate

Returns a new instance of RateEstimate


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 18

def initialize(origin, destination, carrier, service_name, options = {})
  @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name
  @service_code = options[:service_code]
  if options[:package_rates]
    @package_rates = options[:package_rates].map { |p| p.update(:rate => Package.cents_from(p[:rate])) }
  else
    @package_rates = Array(options[:packages]).map { |p| {:package => p} }
  end
  @total_price = Package.cents_from(options[:total_price])
  @negotiated_rate = options[:negotiated_rate] ? Package.cents_from(options[:negotiated_rate]) : nil
  @currency = ActiveMerchant::CurrencyCode.standardize(options[:currency])
  @delivery_range = options[:delivery_range] ? options[:delivery_range].map { |date| date_for(date) }.compact : []
  @shipping_date = date_for(options[:shipping_date])
  @delivery_date = @delivery_range.last
  @insurance_price = Package.cents_from(options[:insurance_price])
end

Instance Attribute Details

#carrierObject (readonly)

Carrier.name ('USPS', 'FedEx', etc.)


7
8
9
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 7

def carrier
  @carrier
end

#currencyObject (readonly)

'USD', 'CAD', etc.


10
11
12
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 10

def currency
  @currency
end

#delivery_dateObject (readonly)

Usually only available for express shipments


13
14
15
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 13

def delivery_date
  @delivery_date
end

#delivery_rangeObject (readonly)

Min and max delivery estimate in days


14
15
16
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 14

def delivery_range
  @delivery_range
end

#destinationObject (readonly)

Returns the value of attribute destination


5
6
7
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 5

def destination
  @destination
end

#insurance_priceObject (readonly)

Returns the value of attribute insurance_price


16
17
18
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 16

def insurance_price
  @insurance_price
end

#negotiated_rateObject (readonly)

Returns the value of attribute negotiated_rate


15
16
17
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 15

def negotiated_rate
  @negotiated_rate
end

#originObject (readonly)

Location objects


4
5
6
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 4

def origin
  @origin
end

#package_ratesObject (readonly)

array of hashes in the form of => <Package>, :rate => 500


6
7
8
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 6

def package_rates
  @package_rates
end

#service_codeObject (readonly)

Returns the value of attribute service_code


9
10
11
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 9

def service_code
  @service_code
end

#service_nameObject (readonly)

name of service (“First Class Ground”, etc.)


8
9
10
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 8

def service_name
  @service_name
end

#shipping_dateObject (readonly)


12
13
14
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 12

def shipping_date
  @shipping_date
end

Instance Method Details

#add(package, rate = nil) ⇒ Object

Raises:

  • (ArgumentError)

42
43
44
45
46
47
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 42

def add(package, rate = nil)
  cents = Package.cents_from(rate)
  raise ArgumentError.new("New packages must have valid rate information since this RateEstimate has no total_price set.") if cents.nil? and total_price.nil?
  @package_rates << {:package => package, :rate => cents}
  self
end

#package_countObject


53
54
55
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 53

def package_count
  package_rates.length
end

#packagesObject


49
50
51
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 49

def packages
  package_rates.map { |p| p[:package] }
end

#total_priceObject Also known as: price


35
36
37
38
39
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 35

def total_price
  @total_price || @package_rates.sum { |p| p[:rate] }
rescue NoMethodError
  raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.")
end