Class: ActiveMerchant::Shipping::RateEstimate

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RateEstimate.



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

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 = 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.)



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

def carrier
  @carrier
end

#currencyObject (readonly)

Returns the value of attribute currency.



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

def currency
  @currency
end

#delivery_dateObject (readonly)

Usually only available for express shipments



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

def delivery_date
  @delivery_date
end

#delivery_rangeObject (readonly)

Min and max delivery estimate in days



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

def delivery_range
  @delivery_range
end

#destinationObject (readonly)

Returns the value of attribute destination.



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

def destination
  @destination
end

#insurance_priceObject (readonly)

Returns the value of attribute insurance_price.



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

def insurance_price
  @insurance_price
end

#negotiated_rateObject (readonly)

Returns the value of attribute negotiated_rate.



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

def negotiated_rate
  @negotiated_rate
end

#originObject (readonly)

Location objects



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

def origin
  @origin
end

#package_ratesObject (readonly)

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



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

def package_rates
  @package_rates
end

#service_codeObject (readonly)

Returns the value of attribute service_code.



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

def service_code
  @service_code
end

#service_nameObject (readonly)

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



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

def service_name
  @service_name
end

#shipping_dateObject (readonly)

‘USD’, ‘CAD’, etc. en.wikipedia.org/wiki/ISO_4217



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

def shipping_date
  @shipping_date
end

Instance Method Details

#add(package, rate = nil) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 45

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



56
57
58
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 56

def package_count
  package_rates.length
end

#packagesObject



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

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

#total_priceObject Also known as: price



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

def total_price
  begin
    @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
end