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.



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

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_range = options[:delivery_range] ? options[:delivery_range].map { |date| date_for(date) }.compact : []
  @delivery_date = @delivery_range.last
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

#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.



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)


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

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



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

def package_count
  package_rates.length
end

#packagesObject



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

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

#total_priceObject Also known as: price



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

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