Class: SpreeCmCommissioner::Distance
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::Distance
- 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
-
#base_km ⇒ Object
Returns the value of attribute base_km.
-
#detour_dropoff_km ⇒ Object
Returns the value of attribute detour_dropoff_km.
-
#detour_pickup_km ⇒ Object
Returns the value of attribute detour_pickup_km.
-
#distance_km ⇒ Object
Returns the value of attribute distance_km.
-
#estimated_time_minutes ⇒ Object
Returns the value of attribute estimated_time_minutes.
-
#extra_dropoff_km ⇒ Object
Returns the value of attribute extra_dropoff_km.
-
#extra_pickup_km ⇒ Object
Returns the value of attribute extra_pickup_km.
-
#id ⇒ Object
Returns the value of attribute id.
-
#ordered_points ⇒ Object
Returns the value of attribute ordered_points.
Class Method Summary collapse
- .from_hash(hash) ⇒ Object
-
.from_signed_hash(hash) ⇒ Object
Build a Distance from a signed hash (used for request-time verification).
- .valid_signature?(signed_hash) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Distance
constructor
A new instance of Distance.
- #to_h ⇒ Object
- #to_signed_h ⇒ Object
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( = {}) = .stringify_keys if .is_a?(Hash) @id = SecureRandom.hex @distance_km = ['distance_km']&.to_f @ordered_points = ['ordered_points'] @estimated_time_minutes = ['estimated_time_minutes']&.to_i @base_km = ['base_km']&.to_f @detour_pickup_km = ['detour_pickup_km']&.to_f @detour_dropoff_km = ['detour_dropoff_km']&.to_f @extra_pickup_km = ['extra_pickup_km']&.to_f @extra_dropoff_km = ['extra_dropoff_km']&.to_f end |
Instance Attribute Details
#base_km ⇒ Object
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_km ⇒ Object
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_km ⇒ Object
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_km ⇒ Object
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_minutes ⇒ Object
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_km ⇒ Object
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_km ⇒ Object
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 |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/spree_cm_commissioner/distance.rb', line 6 def id @id end |
#ordered_points ⇒ Object
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
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_h ⇒ Object
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_h ⇒ Object
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 |