Class: GoogleDistanceMatrix::RoutesFinder
- Inherits:
-
Object
- Object
- GoogleDistanceMatrix::RoutesFinder
- Defined in:
- lib/google_distance_matrix/routes_finder.rb
Overview
Public: Has logic for doing finder operations on a matrix.
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#matrix ⇒ Object
readonly
Returns the value of attribute matrix.
Instance Method Summary collapse
-
#initialize(matrix) ⇒ RoutesFinder
constructor
A new instance of RoutesFinder.
-
#route_for(options = {}) ⇒ Object
Public: Finds a route for you based on one origin and destination.
-
#route_for!(options = {}) ⇒ Object
Public: Finds a route for you based on one origin and destination.
-
#routes_for(place_or_object_place_was_built_from) ⇒ Object
Public: Finds routes for given place.
-
#routes_for!(place_or_object_place_was_built_from) ⇒ Object
Public: Finds routes for given place.
-
#shortest_route_by_distance_to(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by distance to a place.
-
#shortest_route_by_distance_to!(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by distance to a place.
-
#shortest_route_by_duration_in_traffic_to(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by duration in traffic to a place.
-
#shortest_route_by_duration_in_traffic_to!(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by duration in traffic to a place.
-
#shortest_route_by_duration_to(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by duration to a place.
-
#shortest_route_by_duration_to!(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by duration to a place.
Constructor Details
#initialize(matrix) ⇒ RoutesFinder
Returns a new instance of RoutesFinder.
11 12 13 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 11 def initialize(matrix) @matrix = matrix end |
Instance Attribute Details
#matrix ⇒ Object (readonly)
Returns the value of attribute matrix.
8 9 10 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 8 def matrix @matrix end |
Instance Method Details
#route_for(options = {}) ⇒ Object
Public: Finds a route for you based on one origin and destination
origin - A place representing the origin, or an object which you built the origin from destination - A place representing the destination, or an object which you built the
destination from
A Route for given origin and destination
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 51 def route_for( = {}) = .with_indifferent_access origin = ensure_place [:origin] destination = ensure_place [:destination] raise ArgumentError, 'Must provide origin and destination' if origin.nil? || destination.nil? routes_for(origin).detect { |route| route.destination == destination } end |
#route_for!(options = {}) ⇒ Object
Public: Finds a route for you based on one origin and destination
Behaviour is same as without a bang, except it fails unless route are ok.
66 67 68 69 70 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 66 def route_for!( = {}) route_for().tap do |route| fail_unless_route_is_ok route end end |
#routes_for(place_or_object_place_was_built_from) ⇒ Object
Public: Finds routes for given place.
place - Either an origin or destination, or an object which you built the place from
Returns the place’s routes
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 20 def routes_for(place_or_object_place_was_built_from) place = ensure_place place_or_object_place_was_built_from if origins.include? place routes_for_origin place elsif destinations.include? place routes_for_destination place else raise ArgumentError, 'Given place not an origin nor destination.' end end |
#routes_for!(place_or_object_place_was_built_from) ⇒ Object
Public: Finds routes for given place.
Behaviour is same as without a bang, except it fails unless all routes are ok.
36 37 38 39 40 41 42 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 36 def routes_for!(place_or_object_place_was_built_from) routes_for(place_or_object_place_was_built_from).tap do |routes| routes.each do |route| fail_unless_route_is_ok route end end end |
#shortest_route_by_distance_to(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by distance to a place.
place - The place, or object place was built from, you want the shortest route to
Returns shortest route, or nil if no routes had status ok
77 78 79 80 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 77 def shortest_route_by_distance_to(place_or_object_place_was_built_from) routes = routes_for place_or_object_place_was_built_from select_ok_routes(routes).min_by(&:distance_in_meters) end |
#shortest_route_by_distance_to!(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by distance to a place.
place - The place, or object place was built from, you want the shortest route to
Returns shortest route, fails if any of the routes are not ok
87 88 89 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 87 def shortest_route_by_distance_to!(place_or_object_place_was_built_from) routes_for!(place_or_object_place_was_built_from).min_by(&:distance_in_meters) end |
#shortest_route_by_duration_in_traffic_to(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by duration in traffic to a place.
NOTE The matrix must be loaded with mode driving and a departure_time set to
get the matrix loaded with duration in traffic.
place - The place, or object place was built from, you want the shortest route to
Returns shortest route, or nil if no routes had status ok
118 119 120 121 122 123 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 118 def shortest_route_by_duration_in_traffic_to(place_or_object_place_was_built_from) ensure_driving_and_departure_time_or_fail! routes = routes_for place_or_object_place_was_built_from select_ok_routes(routes).min_by(&:duration_in_traffic_in_seconds) end |
#shortest_route_by_duration_in_traffic_to!(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by duration in traffic to a place.
NOTE The matrix must be loaded with mode driving and a departure_time set to
get the matrix loaded with duration in traffic.
place - The place, or object place was built from, you want the shortest route to
Returns shortest route, fails if any of the routes are not ok
133 134 135 136 137 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 133 def shortest_route_by_duration_in_traffic_to!(place_or_object_place_was_built_from) ensure_driving_and_departure_time_or_fail! routes_for!(place_or_object_place_was_built_from).min_by(&:duration_in_traffic_in_seconds) end |
#shortest_route_by_duration_to(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by duration to a place.
place - The place, or object place was built from, you want the shortest route to
Returns shortest route, or nil if no routes had status ok
96 97 98 99 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 96 def shortest_route_by_duration_to(place_or_object_place_was_built_from) routes = routes_for place_or_object_place_was_built_from select_ok_routes(routes).min_by(&:duration_in_seconds) end |
#shortest_route_by_duration_to!(place_or_object_place_was_built_from) ⇒ Object
Public: Finds shortes route by duration to a place.
place - The place, or object place was built from, you want the shortest route to
Returns shortest route, fails if any of the routes are not ok
106 107 108 |
# File 'lib/google_distance_matrix/routes_finder.rb', line 106 def shortest_route_by_duration_to!(place_or_object_place_was_built_from) routes_for!(place_or_object_place_was_built_from).min_by(&:duration_in_seconds) end |