Module: Orochi::ActsAsRouteable::InstanceMethods

Defined in:
lib/orochi/acts_as_routeable.rb

Instance Method Summary collapse

Instance Method Details

#directions_fromObject



88
89
# File 'lib/orochi/acts_as_routeable.rb', line 88

def directions_from
end

#directions_toObject



85
86
# File 'lib/orochi/acts_as_routeable.rb', line 85

def directions_to
end

#polylineObject



78
79
80
81
82
83
# File 'lib/orochi/acts_as_routeable.rb', line 78

def polyline
  routes.first.each_step do |step|
    puts step.polyline_json
    puts
  end
end

#request_routesObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/orochi/acts_as_routeable.rb', line 24

def request_routes
  request_str = "http://maps.googleapis.com/maps/api/directions/json?sensor=false&alternatives=true&"

  start = self.router.start
  request_str += "origin=#{CGI::escape(start)}&"

  stop = self.router.stop
  request_str += "destination=#{CGI::escape(stop)}"

  response = open(request_str)
  return JSON.parse(response.read)
end

#route!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/orochi/acts_as_routeable.rb', line 38

def route!
  json = self.request_routes
  json_routes = json["routes"]
  json_routes.each do |route|
    r = self.router.routes.create!

    route["legs"].each do |route_leg|
      l = r.legs.create!

      route_leg["steps"].each do |leg_step|
        s = l.steps.create!

        leg_points = leg_step["polyline"]["points"]
        leg_levels = leg_step["polyline"]["levels"]
        polyline_data = GoogleMapsPolyline.decode_polyline(leg_points, leg_levels)
        
        # TODO use inject
        polyline = []
        polyline_data.collect do |point|
          polyline.push([point[0], point[1]])
        end
        
        s.polyline_json = polyline
        s.directions_json = leg_step["html_instructions"]
        s.save!
      end
    end
  end
end

#routesObject



68
69
70
# File 'lib/orochi/acts_as_routeable.rb', line 68

def routes
  self.router.routes
end

#set_endpoints!(start, stop) ⇒ Object



72
73
74
75
76
# File 'lib/orochi/acts_as_routeable.rb', line 72

def set_endpoints!(start, stop)
  self.router.start = start
  self.router.stop = stop
  self.router.save!
end