class GoogleDistanceMatrix::RoutesFinder

Public: Has logic for doing finder operations on a matrix.

rubocop:disable Metrics/ClassLength

Attributes

matrix[R]

Public Class Methods

new(matrix) click to toggle source
# File lib/google_distance_matrix/routes_finder.rb, line 11
def initialize(matrix)
  @matrix = matrix
end

Public Instance Methods

route_for(options = {}) click to toggle source

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

# File lib/google_distance_matrix/routes_finder.rb, line 51
def route_for(options = {})
  options = options.with_indifferent_access

  origin = ensure_place options[:origin]
  destination = ensure_place options[: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 = {}) click to toggle source

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.

# File lib/google_distance_matrix/routes_finder.rb, line 66
def route_for!(options = {})
  route_for(options).tap do |route|
    fail_unless_route_is_ok route
  end
end
routes_for(place_or_object_place_was_built_from) click to toggle source

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

# 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) click to toggle source

Public: Finds routes for given place.

Behaviour is same as without a bang, except it fails unless all routes are ok.

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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

Private Instance Methods

ensure_driving_and_departure_time_or_fail!() click to toggle source
# File lib/google_distance_matrix/routes_finder.rb, line 194
def ensure_driving_and_departure_time_or_fail!
  return if configuration.mode == 'driving' && configuration.departure_time.present?

  raise InvalidQuery, 'Matrix must be in mode driving and a departure_time must be set'
end
ensure_place(object) click to toggle source
# File lib/google_distance_matrix/routes_finder.rb, line 141
def ensure_place(object)
  if object.is_a? Place
    object
  else
    object = object.with_indifferent_access if object.is_a? Hash

    find_place_for_object(origins, object) ||
      find_place_for_object(destinations, object)
  end
end
fail_unless_route_is_ok(route) click to toggle source
# File lib/google_distance_matrix/routes_finder.rb, line 186
def fail_unless_route_is_ok(route)
  raise InvalidRoute, route unless route.ok?
end
find_place_for_object(collection, object) click to toggle source
# File lib/google_distance_matrix/routes_finder.rb, line 152
def find_place_for_object(collection, object)
  collection.detect do |place|
    place.extracted_attributes_from.present? &&
      place.extracted_attributes_from == object
  end
end
find_route_for_origin_and_destination(origin, destination) click to toggle source
# File lib/google_distance_matrix/routes_finder.rb, line 175
def find_route_for_origin_and_destination(origin, destination)
  origin_index = origins.index origin
  destination_index = destinations.index destination

  if origin_index.nil? || destination_index.nil?
    raise ArgumentError, 'Given origin or destination is not i matrix.'
  end

  [data[origin_index][destination_index]]
end
routes_for_destination(destination) click to toggle source
# File lib/google_distance_matrix/routes_finder.rb, line 166
def routes_for_destination(destination)
  index = destinations.index destination
  raise ArgumentError, 'Given destination is not i matrix.' if index.nil?

  [].tap do |routes|
    data.each { |row| routes << row[index] }
  end
end
routes_for_origin(origin) click to toggle source
# File lib/google_distance_matrix/routes_finder.rb, line 159
def routes_for_origin(origin)
  index = origins.index origin
  raise ArgumentError, 'Given origin is not i matrix.' if index.nil?

  data[index]
end
select_ok_routes(routes) click to toggle source
# File lib/google_distance_matrix/routes_finder.rb, line 190
def select_ok_routes(routes)
  routes.select(&:ok?)
end