class RoutificApi::Route

This class represents the resulting route returned by the Routific API

Constants

FIELDS

Attributes

vehicle_routes[R]

Public Class Methods

new(data) click to toggle source

Constructor

# File lib/routific/route.rb, line 19
def initialize(data)
  FIELDS.each do |field|
    instance_variable_set "@#{field}", data[field]
  end
  add_solution(data[:solution] || {})
end
parse(data) click to toggle source

Parse the JSON representation of a route, and return it as a Route object

# File lib/routific/route.rb, line 51
def parse(data)
  data = process_data(data)
  RoutificApi::Route.new(data)
end
process_data(hash) click to toggle source
# File lib/routific/route.rb, line 56
def process_data(hash)
  hash.keys.each do |key|
    hash[(key.to_sym rescue key) || key] = hash.delete(key)
  end
  hash
end

Public Instance Methods

add_solution(solution) click to toggle source
# File lib/routific/route.rb, line 26
def add_solution(solution)
  @vehicle_routes = {}

  solution.each do |vehicle_name, way_points|
    # Get all way points for this vehicle
    way_points.each do |waypoint_info|
      # Get all information for this way point
      way_point = RoutificApi::WayPoint.new(waypoint_info)
      add_way_point(vehicle_name, way_point)
    end
  end
end
add_way_point(vehicle_name, way_point) click to toggle source

Adds a new way point for the specified vehicle

# File lib/routific/route.rb, line 40
def add_way_point(vehicle_name, way_point)
  if @vehicle_routes[vehicle_name].nil?
    # No previous way point was added for the specified vehicle, so create a new array
    @vehicle_routes[vehicle_name] = []
  end
  # Adds the provided way point for the specified vehicle
  @vehicle_routes[vehicle_name] << way_point
end