module Orochi::ActsAsRouteable::InstanceMethods

Public Instance Methods

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

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

# 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
polyline() click to toggle source
# 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) click to toggle source
# File lib/orochi/acts_as_routeable.rb, line 22
def request_routes(options)
  Orochi::GoogleClient.request(options)
end
reverse() click to toggle source
# 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!() click to toggle source
# 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
routes() click to toggle source
# File lib/orochi/acts_as_routeable.rb, line 55
def routes
  self.router.routes
end
set_endpoints!(start, stop) click to toggle source
# 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