Class: SpreeCmCommissioner::Distance

Inherits:
Object
  • Object
show all
Defined in:
lib/spree_cm_commissioner/distance.rb

Overview

Generic value object representing a computed trip distance and detours. This is the canonical class used by distance calculation services and line item metadata (see LineItemTransitable concern).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Distance

Returns a new instance of Distance.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spree_cm_commissioner/distance.rb', line 16

def initialize(options = {})
  options = options.stringify_keys if options.is_a?(Hash)

  @id = SecureRandom.hex
  @distance_km = options['distance_km']&.to_f
  @ordered_points = options['ordered_points']
  @estimated_time_minutes = options['estimated_time_minutes']&.to_i
  @base_km = options['base_km']&.to_f
  @detour_pickup_km = options['detour_pickup_km']&.to_f
  @detour_dropoff_km = options['detour_dropoff_km']&.to_f
  @extra_pickup_km = options['extra_pickup_km']&.to_f
  @extra_dropoff_km = options['extra_dropoff_km']&.to_f
end

Instance Attribute Details

#base_kmObject

Returns the value of attribute base_km.



6
7
8
# File 'lib/spree_cm_commissioner/distance.rb', line 6

def base_km
  @base_km
end

#detour_dropoff_kmObject

Returns the value of attribute detour_dropoff_km.



6
7
8
# File 'lib/spree_cm_commissioner/distance.rb', line 6

def detour_dropoff_km
  @detour_dropoff_km
end

#detour_pickup_kmObject

Returns the value of attribute detour_pickup_km.



6
7
8
# File 'lib/spree_cm_commissioner/distance.rb', line 6

def detour_pickup_km
  @detour_pickup_km
end

#distance_kmObject

Returns the value of attribute distance_km.



6
7
8
# File 'lib/spree_cm_commissioner/distance.rb', line 6

def distance_km
  @distance_km
end

#estimated_time_minutesObject

Returns the value of attribute estimated_time_minutes.



6
7
8
# File 'lib/spree_cm_commissioner/distance.rb', line 6

def estimated_time_minutes
  @estimated_time_minutes
end

#extra_dropoff_kmObject

Returns the value of attribute extra_dropoff_km.



6
7
8
# File 'lib/spree_cm_commissioner/distance.rb', line 6

def extra_dropoff_km
  @extra_dropoff_km
end

#extra_pickup_kmObject

Returns the value of attribute extra_pickup_km.



6
7
8
# File 'lib/spree_cm_commissioner/distance.rb', line 6

def extra_pickup_km
  @extra_pickup_km
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/spree_cm_commissioner/distance.rb', line 6

def id
  @id
end

#ordered_pointsObject

Returns the value of attribute ordered_points.



6
7
8
# File 'lib/spree_cm_commissioner/distance.rb', line 6

def ordered_points
  @ordered_points
end

Class Method Details

.from_hash(hash) ⇒ Object



30
31
32
# File 'lib/spree_cm_commissioner/distance.rb', line 30

def self.from_hash(hash)
  new(hash || {})
end

.from_signed_hash(hash) ⇒ Object

Build a Distance from a signed hash (used for request-time verification).



35
36
37
38
39
# File 'lib/spree_cm_commissioner/distance.rb', line 35

def self.from_signed_hash(hash)
  return nil unless valid_signature?(hash)

  new(hash || {})
end

.valid_signature?(signed_hash) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
# File 'lib/spree_cm_commissioner/distance.rb', line 41

def self.valid_signature?(signed_hash)
  return false if signed_hash.nil?

  verify_result = SpreeCmCommissioner::Signing::VerifySignature.call(
    signed_hash: signed_hash,
    signing_key: ENV.fetch('DISTANCE_SIGNING_KEY')
  )

  verify_result.success? && verify_result.value == true
end

Instance Method Details

#to_hObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/spree_cm_commissioner/distance.rb', line 52

def to_h
  {
    'distance_km' => @distance_km,
    'ordered_points' => @ordered_points,
    'estimated_time_minutes' => @estimated_time_minutes,
    'base_km' => @base_km,
    'detour_pickup_km' => @detour_pickup_km,
    'detour_dropoff_km' => @detour_dropoff_km,
    'extra_pickup_km' => @extra_pickup_km,
    'extra_dropoff_km' => @extra_dropoff_km
  }.compact
end

#to_signed_hObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/spree_cm_commissioner/distance.rb', line 65

def to_signed_h
  hash = {
    'distance_km' => @distance_km,
    'ordered_points' => @ordered_points,
    'estimated_time_minutes' => @estimated_time_minutes,
    'base_km' => @base_km,
    'detour_pickup_km' => @detour_pickup_km,
    'detour_dropoff_km' => @detour_dropoff_km,
    'extra_pickup_km' => @extra_pickup_km,
    'extra_dropoff_km' => @extra_dropoff_km,
    'signed_at' => Time.current.utc.iso8601
  }.compact

  sign_result = SpreeCmCommissioner::Signing::SignData.call(
    hash: hash,
    signing_key: ENV.fetch('DISTANCE_SIGNING_KEY')
  )

  return hash unless sign_result.success?

  hash.merge('signature' => sign_result.value)
end