Class: FriendlyShipping::Rate

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • shipping_method (ShippingMethod)

    the rate's shipping method

  • amounts (Hash{String,Symbol=>Money})

    the amounts that make up the rate

  • remote_service_id (Integer) (defaults to: nil)

    the remote service ID for the rate

  • pickup_date (Time) (defaults to: nil)

    the pickup date for the rate

  • delivery_date (Time) (defaults to: nil)

    the delivery date for the rate

  • guaranteed (Boolean) (defaults to: false)

    whether the delivery date is guaranteed

  • warnings (Array) (defaults to: [])

    any warnings that were generated while getting this rate

  • errors (Array) (defaults to: [])

    any errors that were generated while getting this rate

  • data (Hash) (defaults to: {})

    additional data related to the 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

#amountsHash{String,Symbol=>Money} (readonly)

Returns the amounts that make up the rate.

Returns:

  • (Hash{String,Symbol=>Money})

    the amounts that make up the rate



13
14
15
# File 'lib/friendly_shipping/rate.rb', line 13

def amounts
  @amounts
end

#dataHash (readonly)

Returns additional data related to the rate.

Returns:

  • (Hash)

    additional data related to the rate



34
35
36
# File 'lib/friendly_shipping/rate.rb', line 34

def data
  @data
end

#delivery_dateTime (readonly) Also known as: delivery

Returns the delivery date for the rate.

Returns:

  • (Time)

    the delivery date for the rate



22
23
24
# File 'lib/friendly_shipping/rate.rb', line 22

def delivery_date
  @delivery_date
end

#errorsArray (readonly)

Returns any errors that were generated while getting this rate.

Returns:

  • (Array)

    any errors that were generated while getting this rate



31
32
33
# File 'lib/friendly_shipping/rate.rb', line 31

def errors
  @errors
end

#guaranteedBoolean (readonly)

Returns whether the delivery date is guaranteed.

Returns:

  • (Boolean)

    whether the delivery date is guaranteed



25
26
27
# File 'lib/friendly_shipping/rate.rb', line 25

def guaranteed
  @guaranteed
end

#pickup_dateTime (readonly) Also known as: pickup

Returns the pickup date for the rate.

Returns:

  • (Time)

    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_idInteger (readonly)

Returns the remote service ID for the rate.

Returns:

  • (Integer)

    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_methodShippingMethod (readonly)

Returns the rate's shipping method.

Returns:



10
11
12
# File 'lib/friendly_shipping/rate.rb', line 10

def shipping_method
  @shipping_method
end

#warningsArray (readonly)

Returns any warnings that were generated while getting this rate.

Returns:

  • (Array)

    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_amountMoney

Sums all the amounts in the rate, returning the total amount.

Returns:

  • (Money)

Raises:



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