Module: Pointy

Includes:
HTTParty
Defined in:
lib/pointy.rb,
lib/pointy/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.route(from, to, opts = {}) ⇒ Object



71
72
73
# File 'lib/pointy.rb', line 71

def self.route(from, to, opts={})
  Pointy.route_coordinates(from, to, opts)
end

.route_coordinates(from, to, opts = {}) ⇒ Object

from and to should be of format:

lat: float
long: float



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pointy.rb', line 56

def self.route_coordinates(from, to, opts={})
  polylines = Pointy.route_polylines(from, to, opts)
  return if polylines.nil?

  polylines.inject([]) do |all_points, polyline|
    decoded_poly = Polylines::Decoder.decode_polyline(polyline)
    all_points += decoded_poly.map do |coord|
      {
        lat: coord.first,
        long: coord.last
      }
    end
  end
end

.route_polylines(from, to, opts = {}) ⇒ Object

from and to should be of format:

lat: float
long: float



18
19
20
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
# File 'lib/pointy.rb', line 18

def self.route_polylines(from, to, opts={})
  return if default_params[:key].nil? and opts[:key].nil?

  # Format points for Google API
  opts.merge!({
    origin: "#{from[:lat]},#{from[:long]}",
    destination: "#{to[:lat]},#{to[:long]}"
  })

  response = get('/directions/json', query: opts)

  # Check there are routes
  routes = response['routes']
  return if routes.nil? or routes.empty?

  # Gather all route points
  route = routes.first
  legs = route['legs']
  return if legs.nil? or legs.empty?

  # Accumulate all points
  legs.inject([]) do |polylines, leg|
    steps = leg['steps']
    if !steps.nil? and !steps.empty?
      steps.each_with_index do |step, index|
        polylines << step['polyline']['points']
      end
    end

    polylines
  end
end