Module: Orochi::ActsAsRouteable::InstanceMethods

Defined in:
lib/orochi/acts_as_routeable.rb

Instance Method Summary collapse

Instance Method Details

#directionsObject



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

def directions
  # TODO abstract into helper
  directions = []
  routes.first.each_step do |step|
    directions.push(step.directions_json.to_s)
  end
  directions
end

#includes?(point) ⇒ Boolean

PRE point needs to be a tuple [lat, lng] (for now) TODO point of inclusion

Returns:

  • (Boolean)


99
100
101
102
103
104
# File 'lib/orochi/acts_as_routeable.rb', line 99

def includes?(point)
  self.polyline.any? do |step_json|
    polyline_array = eval step_json
    polyline_array.include?(point)
  end
end

#polylineObject



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

def polyline
  # TODO abstract into helper
  polyline = []
  routes.first.each_step do |step|
    polyline.push(step.polyline_json.to_a)
  end
  polyline.flatten
end

#request_routes(options) ⇒ Object



22
23
24
# File 'lib/orochi/acts_as_routeable.rb', line 22

def request_routes(options)
  Orochi::GoogleClient.request(options)
end

#reverseObject



87
88
89
90
91
92
93
94
95
# File 'lib/orochi/acts_as_routeable.rb', line 87

def reverse
  # TODO find_or_create_by
  reversed_router = Router.where({:start => self.router.stop, :stop => self.router.start})
  if reversed_router.nil? || reversed_router.empty?
    reversed_router = Router.create({:start => self.router.stop, :stop => self.router.start})
    reversed_router.route!
  end
  reversed_router
end

#route!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/orochi/acts_as_routeable.rb', line 27

def route!
  json = self.request_routes({:origin => self.router.start, :destination => self.router.stop})
  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)
        
        polyline = polyline_data.inject([]) do |acc, point|
          acc << ([point[0], point[1]])
        end
        
        s.polyline_json = polyline.inspect
        s.directions_json = leg_step["html_instructions"]
        s.save!
      end
    end
  end
end

#routesObject



55
56
57
# File 'lib/orochi/acts_as_routeable.rb', line 55

def routes
  self.router.routes
end

#set_endpoints!(start, stop) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/orochi/acts_as_routeable.rb', line 59

def set_endpoints!(start, stop)
  # TODO find_or_create_by
  if self.router.nil?
    self.router = Router.create!
  end
  self.router.start = start
  self.router.stop = stop
  self.router.save!
end