Class: FriendlyShipping::Rate
- Inherits:
-
Object
- Object
- FriendlyShipping::Rate
- Defined in:
- lib/friendly_shipping/rate.rb
Overview
Base class for a shipping rate returned by a carrier API.
Defined Under Namespace
Classes: NoAmountsGiven
Instance Attribute Summary collapse
-
#amounts ⇒ Hash{String,Symbol=>Money}
readonly
The amounts that make up the rate.
-
#data ⇒ Hash
readonly
Additional data related to the rate.
-
#delivery_date ⇒ Time
(also: #delivery)
readonly
The delivery date for the rate.
-
#errors ⇒ Array
readonly
Any errors that were generated while getting this rate.
-
#guaranteed ⇒ Boolean
readonly
Whether the delivery date is guaranteed.
-
#pickup_date ⇒ Time
(also: #pickup)
readonly
The pickup date for the rate.
-
#remote_service_id ⇒ Integer
readonly
The remote service ID for the rate.
-
#shipping_method ⇒ ShippingMethod
readonly
The rate's shipping method.
-
#warnings ⇒ Array
readonly
Any warnings that were generated while getting this rate.
Instance Method Summary collapse
-
#initialize(shipping_method:, amounts:, remote_service_id: nil, pickup_date: nil, delivery_date: nil, guaranteed: false, warnings: [], errors: [], data: {}) ⇒ Rate
constructor
A new instance of Rate.
-
#total_amount ⇒ Money
Sums all the amounts in the rate, returning the total amount.
Constructor Details
#initialize(shipping_method:, amounts:, remote_service_id: nil, pickup_date: nil, delivery_date: nil, guaranteed: false, warnings: [], errors: [], data: {}) ⇒ Rate
Returns a new instance of Rate.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/friendly_shipping/rate.rb', line 45 def initialize( shipping_method:, amounts:, remote_service_id: nil, pickup_date: nil, delivery_date: nil, guaranteed: false, warnings: [], errors: [], data: {} ) @remote_service_id = remote_service_id @shipping_method = shipping_method @amounts = amounts @pickup_date = pickup_date @delivery_date = delivery_date @guaranteed = guaranteed @warnings = warnings @errors = errors @data = data end |
Instance Attribute Details
#amounts ⇒ Hash{String,Symbol=>Money} (readonly)
Returns the amounts that make up the rate.
13 14 15 |
# File 'lib/friendly_shipping/rate.rb', line 13 def amounts @amounts end |
#data ⇒ Hash (readonly)
Returns additional data related to the rate.
34 35 36 |
# File 'lib/friendly_shipping/rate.rb', line 34 def data @data end |
#delivery_date ⇒ Time (readonly) Also known as: delivery
Returns the delivery date for the rate.
22 23 24 |
# File 'lib/friendly_shipping/rate.rb', line 22 def delivery_date @delivery_date end |
#errors ⇒ Array (readonly)
Returns any errors that were generated while getting this rate.
31 32 33 |
# File 'lib/friendly_shipping/rate.rb', line 31 def errors @errors end |
#guaranteed ⇒ Boolean (readonly)
Returns whether the delivery date is guaranteed.
25 26 27 |
# File 'lib/friendly_shipping/rate.rb', line 25 def guaranteed @guaranteed end |
#pickup_date ⇒ Time (readonly) Also known as: pickup
Returns the pickup date for the rate.
19 20 21 |
# File 'lib/friendly_shipping/rate.rb', line 19 def pickup_date @pickup_date end |
#remote_service_id ⇒ Integer (readonly)
Returns the remote service ID for the rate.
16 17 18 |
# File 'lib/friendly_shipping/rate.rb', line 16 def remote_service_id @remote_service_id end |
#shipping_method ⇒ ShippingMethod (readonly)
Returns the rate's shipping method.
10 11 12 |
# File 'lib/friendly_shipping/rate.rb', line 10 def shipping_method @shipping_method end |
#warnings ⇒ Array (readonly)
Returns any warnings that were generated while getting this rate.
28 29 30 |
# File 'lib/friendly_shipping/rate.rb', line 28 def warnings @warnings end |
Instance Method Details
#total_amount ⇒ Money
Sums all the amounts in the rate, returning the total amount.
74 75 76 77 78 |
# File 'lib/friendly_shipping/rate.rb', line 74 def total_amount raise NoAmountsGiven if amounts.empty? amounts.map { |_name, amount| amount }.sum(Money.new(0, 'USD')) end |