Class: ActiveShipping::RateEstimate
- Inherits:
-
Object
- Object
- ActiveShipping::RateEstimate
- Defined in:
- lib/active_shipping/rate_estimate.rb
Overview
Class representing a shipping option with estimated price.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#carrier ⇒ String
The name of the carrier (i.e. , e.g. 'USPS', 'FedEx').
-
#currency ⇒ String
ISO4217 currency code of the quoted rate estimates, e.g.
-
#delivery_date ⇒ Date
The date on which the shipment will be delivered.
-
#delivery_range ⇒ Array<Date>
The minimum and maximum date of when the shipment is expected to be delivered.
-
#desination ⇒ ActiveShipping::Location
The desination of the shipment.
-
#destination ⇒ Object
readonly
Returns the value of attribute destination.
-
#estimate_reference ⇒ Object
readonly
Returns the value of attribute estimate_reference.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#insurance_price ⇒ Integer
The price of insurance in cents.
-
#negotiated_rate ⇒ Integer
The negotiated rate in cents.
-
#origin ⇒ ActiveShipping::Location
The origin of the shipment.
-
#package_rates ⇒ Array<{:rate => Integer, :package => ActiveShipping::Package}>
A list of rates for all the packages in the shipment.
-
#pickup_time ⇒ Object
readonly
Returns the value of attribute pickup_time.
-
#service_code ⇒ String
The code of the shipping service.
-
#service_name ⇒ String
The name of the shipping service (e.g.
"First Class Ground"
). -
#shipping_date ⇒ Date
The date on which the shipment will be expected.
Instance Method Summary collapse
-
#add(package, rate = nil) ⇒ self
Adds a package to this rate estimate.
-
#initialize(origin, destination, carrier, service_name, options = {}) ⇒ RateEstimate
constructor
A new instance of RateEstimate.
-
#package_count ⇒ Integer
The number of packages for which rate estimates are given.
-
#packages ⇒ Array<ActiveShipping::Package>
The list of packages for which rate estimates are given.
-
#total_price ⇒ Integer
(also: #price)
The total price of the shipments in cents.
Constructor Details
#initialize(origin, destination, carrier, service_name, options = {}) ⇒ RateEstimate
Returns a new instance of RateEstimate.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/active_shipping/rate_estimate.rb', line 64 def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end |
Instance Attribute Details
#carrier ⇒ String
The name of the carrier (i.e. , e.g. 'USPS', 'FedEx')
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#currency ⇒ String
ISO4217 currency code of the quoted rate estimates, e.g. CAD
, EUR
, or USD
.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#delivery_date ⇒ Date
The date on which the shipment will be delivered. This is usually only availablee for express shipments; in order cases a #delivery_range is given instead.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#delivery_range ⇒ Array<Date>
The minimum and maximum date of when the shipment is expected to be delivered.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#desination ⇒ ActiveShipping::Location
The desination of the shipment
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#destination ⇒ Object (readonly)
Returns the value of attribute destination.
58 59 60 |
# File 'lib/active_shipping/rate_estimate.rb', line 58 def destination @destination end |
#estimate_reference ⇒ Object (readonly)
Returns the value of attribute estimate_reference.
58 59 60 |
# File 'lib/active_shipping/rate_estimate.rb', line 58 def estimate_reference @estimate_reference end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
58 59 60 |
# File 'lib/active_shipping/rate_estimate.rb', line 58 def expires_at @expires_at end |
#insurance_price ⇒ Integer
The price of insurance in cents.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#negotiated_rate ⇒ Integer
The negotiated rate in cents
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#origin ⇒ ActiveShipping::Location
The origin of the shipment
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#package_rates ⇒ Array<{:rate => Integer, :package => ActiveShipping::Package}>
A list of rates for all the packages in the shipment.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#pickup_time ⇒ Object (readonly)
Returns the value of attribute pickup_time.
58 59 60 |
# File 'lib/active_shipping/rate_estimate.rb', line 58 def pickup_time @pickup_time end |
#service_code ⇒ String
The code of the shipping service
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#service_name ⇒ String
The name of the shipping service (e.g. "First Class Ground"
)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
#shipping_date ⇒ Date
The date on which the shipment will be expected. Normally, this means that the delivery date range can only pe prmoised if the shipment is handed over on or before this date.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/active_shipping/rate_estimate.rb', line 57 class RateEstimate attr_reader :origin, :destination, :package_rates, :carrier, :service_name, :service_code, :shipping_date, :delivery_date, :delivery_range, :currency, :negotiated_rate, :insurance_price, :estimate_reference, :expires_at, :pickup_time def initialize(origin, destination, carrier, service_name, = {}) @origin, @destination, @carrier, @service_name = origin, destination, carrier, service_name @service_code = [:service_code] @estimate_reference = [:estimate_reference] @pickup_time = [:pickup_time] @expires_at = [:expires_at] 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]) @negotiated_rate = [:negotiated_rate] ? Package.cents_from([:negotiated_rate]) : nil @currency = ActiveUtils::CurrencyCode.standardize([:currency]) @delivery_range = [:delivery_range] ? [:delivery_range].map { |date| date_for(date) }.compact : [] @shipping_date = date_for([:shipping_date]) @delivery_date = @delivery_range.last @insurance_price = Package.cents_from([:insurance_price]) end # The total price of the shipments in cents. # @return [Integer] def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end alias_method :price, :total_price # Adds a package to this rate estimate # @param package [ActiveShipping::Package] The package to add. # @param rate [#cents, Float, String, nil] The rate for this package. This is only required if # there is no total price for this shipment # @return [self] 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 # The list of packages for which rate estimates are given. # @return [Array<ActiveShipping::Package>] def packages package_rates.map { |p| p[:package] } end # The number of packages for which rate estimates are given. # @return [Integer] def package_count package_rates.length end private # Returns a Date object for a given input # @param [String, Date, Time, DateTime, ...] The object to infer a date from. # @return [Date, nil] The Date object absed on the input, or `nil` if no date # could be determined. def date_for(date) date && DateTime.strptime(date.to_s, "%Y-%m-%d") rescue ArgumentError nil end end |
Instance Method Details
#add(package, rate = nil) ⇒ self
Adds a package to this rate estimate
98 99 100 101 102 103 |
# File 'lib/active_shipping/rate_estimate.rb', line 98 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 ⇒ Integer
The number of packages for which rate estimates are given.
113 114 115 |
# File 'lib/active_shipping/rate_estimate.rb', line 113 def package_count package_rates.length end |
#packages ⇒ Array<ActiveShipping::Package>
The list of packages for which rate estimates are given.
107 108 109 |
# File 'lib/active_shipping/rate_estimate.rb', line 107 def packages package_rates.map { |p| p[:package] } end |
#total_price ⇒ Integer Also known as: price
The total price of the shipments in cents.
86 87 88 89 90 |
# File 'lib/active_shipping/rate_estimate.rb', line 86 def total_price @total_price || @package_rates.sum { |pr| pr[:rate] } rescue NoMethodError raise ArgumentError.new("RateEstimate must have a total_price set, or have a full set of valid package rates.") end |