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.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 15

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])
  @currency = options[:currency]
  @delivery_date = options[:delivery_date]
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)

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



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

def delivery_date
  @delivery_date
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

#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

Instance Method Details

#add(package, rate = nil) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

def package_count
  package_rates.length
end

#packagesObject



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

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

#total_priceObject Also known as: price



28
29
30
31
32
33
34
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 28

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