Class: GoogleDistanceMatrix::RoutesFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/google_distance_matrix/routes_finder.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrix) ⇒ RoutesFinder

Returns a new instance of RoutesFinder.



9
10
11
# File 'lib/google_distance_matrix/routes_finder.rb', line 9

def initialize(matrix)
  @matrix = matrix
end

Instance Attribute Details

#matrixObject (readonly)

Returns the value of attribute matrix.



5
6
7
# File 'lib/google_distance_matrix/routes_finder.rb', line 5

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
61
62
# 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]

  if origin.nil? || destination.nil?
    fail ArgumentError, "Must provide origin and destination"
  end

  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.



68
69
70
71
72
# File 'lib/google_distance_matrix/routes_finder.rb', line 68

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) ⇒ 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
    fail 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



80
81
82
83
# File 'lib/google_distance_matrix/routes_finder.rb', line 80

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



90
91
92
# File 'lib/google_distance_matrix/routes_finder.rb', line 90

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_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



99
100
101
102
# File 'lib/google_distance_matrix/routes_finder.rb', line 99

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



109
110
111
# File 'lib/google_distance_matrix/routes_finder.rb', line 109

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