Class: SpreeCmCommissioner::GoogleRoutesDistanceCalculator
- Inherits:
-
BaseInteractor
- Object
- BaseInteractor
- SpreeCmCommissioner::GoogleRoutesDistanceCalculator
- Defined in:
- app/interactors/spree_cm_commissioner/google_routes_distance_calculator.rb
Overview
Interactor wrapper around Google Routes API v2 to compute trip details.
Inputs via context:
-
origin: { lat:, lng: } | “lat,lng” | [lat, lng]
-
destination: { lat:, lng: } | “lat,lng” | [lat, lng]
-
Optional arrays: waypoints, pickups, dropoffs (same point formats)
-
Optional boolean: optimize (default: true) to let Google reorder waypoints
Outputs via context on success:
-
distance_km (Float)
-
ordered_waypoints (Array<Hash>)
-
ordered_points (Array<Hash>)
-
directions_url (String)
-
estimated_time_minutes (Integer) when optimize is true
On failure, context.fail!(message: …) is called.
Instance Method Summary collapse
Instance Method Details
#call ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/interactors/spree_cm_commissioner/google_routes_distance_calculator.rb', line 21 def call via_points = combined_via_points json = fetch_routes_for(@origin, @destination, via_points) if json.nil? context.fail!(message: 'Failed to fetch routes from Google API') return end if json['error'] = json.dig('error', 'message') || json.dig('error', 'status') context.fail!(message: "Google Routes API error: #{error_message}") return end route = json['routes']&.first context.fail!(message: 'No route found') if route.nil? distance_meters = total_distance_of(route) duration_seconds = total_duration_of(route) ordered_waypoints = build_ordered_waypoints_from(route, via_points) ordered_points = [@origin] + ordered_waypoints + [@destination] context.distance_km = (distance_meters / 1000.0).round(3) context.ordered_waypoints = ordered_waypoints context.ordered_points = ordered_points context.directions_url = build_human_link(ordered_waypoints) # Only expose estimated time when optimize is enabled (per requirement) context.estimated_time_minutes = (@optimize ? (duration_seconds / 60.0).round : nil) end |