Class: FriendlyShipping::Timing

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/timing.rb

Overview

Base class for a transit timing estimate returned by a carrier API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shipping_method:, pickup:, delivery:, guaranteed: false, warnings: [], errors: [], properties: {}, data: {}) ⇒ Timing

Returns a new instance of Timing.

Parameters:

  • shipping_method (ShippingMethod)

    this timing's shipping method

  • pickup (Time)

    the pickup estimate

  • delivery (Time)

    the delivery estimate

  • guaranteed (Boolean) (defaults to: false)

    whether the delivery estimate is guaranteed

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

    any warnings that were generated

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

    any errors that were generated

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

    additional data related to the timing

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

    additional data related to the timing (DEPRECATED: use data instead)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/friendly_shipping/timing.rb', line 38

def initialize(
  shipping_method:,
  pickup:,
  delivery:,
  guaranteed: false,
  warnings: [],
  errors: [],
  properties: {},
  data: {}
)
  @shipping_method = shipping_method
  @pickup = pickup
  @delivery = delivery
  @guaranteed = guaranteed
  @warnings = warnings
  @errors = errors
  @data = @properties = properties.presence || data
  warn "[DEPRECATION] `properties` is deprecated.  Please use `data` instead." if properties.present?
end

Instance Attribute Details

#dataHash (readonly)

Returns additional data related to the timing.

Returns:

  • (Hash)

    additional data related to the timing



28
29
30
# File 'lib/friendly_shipping/timing.rb', line 28

def data
  @data
end

#deliveryTime (readonly)

Returns the delivery estimate.

Returns:

  • (Time)

    the delivery estimate



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

def delivery
  @delivery
end

#errorsArray (readonly)

Returns any errors that were generated.

Returns:

  • (Array)

    any errors that were generated



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

def errors
  @errors
end

#guaranteedBoolean (readonly)

Returns whether the delivery estimate is guaranteed.

Returns:

  • (Boolean)

    whether the delivery estimate is guaranteed



16
17
18
# File 'lib/friendly_shipping/timing.rb', line 16

def guaranteed
  @guaranteed
end

#pickupTime (readonly)

Returns the pickup estimate.

Returns:

  • (Time)

    the pickup estimate



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

def pickup
  @pickup
end

#propertiesHash (readonly)

Returns additional data related to the timing (DEPRECATED: use data instead).

Returns:

  • (Hash)

    additional data related to the timing (DEPRECATED: use data instead)



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

def properties
  @properties
end

#shipping_methodShippingMethod (readonly)

Returns this timing's shipping method.

Returns:



7
8
9
# File 'lib/friendly_shipping/timing.rb', line 7

def shipping_method
  @shipping_method
end

#warningsArray (readonly)

Returns any warnings that were generated.

Returns:

  • (Array)

    any warnings that were generated



19
20
21
# File 'lib/friendly_shipping/timing.rb', line 19

def warnings
  @warnings
end

Instance Method Details

#time_in_transitTime

Calculates and returns the time between pickup and delivery.

Returns:

  • (Time)


60
61
62
# File 'lib/friendly_shipping/timing.rb', line 60

def time_in_transit
  delivery - pickup
end