Class: ActiveMerchant::Shipping::RateEstimate
- Defined in:
- lib/active_shipping/shipping/rate_estimate.rb
Instance Attribute Summary collapse
-
#carrier ⇒ Object
readonly
Carrier.name (‘USPS’, ‘FedEx’, etc.).
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#delivery_date ⇒ Object
readonly
‘USD’, ‘CAD’, etc.
-
#delivery_range ⇒ Object
readonly
Min and max delivery estimate in days.
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#origin ⇒ Object
readonly
Location objects.
-
#package_rates ⇒ Object
readonly
array of hashes in the form of => <Package>, :rate => 500.
-
#service_code ⇒ Object
readonly
Returns the value of attribute service_code.
-
#service_name ⇒ Object
readonly
name of service (“First Class Ground”, etc.).
Instance Method Summary collapse
- #add(package, rate = nil) ⇒ Object
-
#initialize(origin, destination, carrier, service_name, options = {}) ⇒ RateEstimate
constructor
A new instance of RateEstimate.
- #package_count ⇒ Object
- #packages ⇒ Object
- #total_price ⇒ Object (also: #price)
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, ={}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] if [:package_rates] @package_rates = [:package_rates].map {|p| p.update({:rate => Package.cents_from(p[:rate])}) } else @package_rates = Array([:packages]).map {|p| {:package => p}} end @total_price = Package.cents_from([:total_price]) @currency = [:currency] @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @delivery_date = @delivery_range.last end |
Instance Attribute Details
#carrier ⇒ Object (readonly)
Carrier.name (‘USPS’, ‘FedEx’, etc.)
8 9 10 |
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 8 def carrier @carrier end |
#currency ⇒ Object (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_date ⇒ Object (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_range ⇒ Object (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 |
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
6 7 8 |
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 6 def destination @destination end |
#origin ⇒ Object (readonly)
Location objects
5 6 7 |
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 5 def origin @origin end |
#package_rates ⇒ Object (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_code ⇒ Object (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_name ⇒ Object (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
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_count ⇒ Object
50 51 52 |
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 50 def package_count package_rates.length end |
#packages ⇒ Object
46 47 48 |
# File 'lib/active_shipping/shipping/rate_estimate.rb', line 46 def packages package_rates.map {|p| p[:package]} end |
#total_price ⇒ Object 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 |