Skip to content

Instantly share code, notes, and snippets.

@Jylanthas
Last active August 29, 2015 14:20
Show Gist options
  • Save Jylanthas/6f3d6b4b840da703c4d3 to your computer and use it in GitHub Desktop.
Save Jylanthas/6f3d6b4b840da703c4d3 to your computer and use it in GitHub Desktop.
routific test
require 'routific'
require 'json'
# Set the Routific API token
Routific.setToken('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NTIwN2Q2MTRiMzYyMjBhMDAwYzhlNjEiLCJpYXQiOjE0MjgxOTI2NTl9.gzapZ2hLNwGxGBNUaXJX6xv4osfz4nPUu0KzdhwMm0s')
def main
test1
# test2
end
def test2
trip_props = {
id: t.id,
arrive_by: t.arrive_by,
pickup: {
latlng: t.pickup.latlng.csv_point_str
},
dropoff: {
latlng: t.dropoff.latlng.csv_point_str
},
user: {
name: t.user.full_name
}
}
avg_by_user = Trip.fulfilled.
where { arrive_by > 2.weeks.ago }.
reduce(Hash.new(0)) { |h, t|
h[t.user.full_name] += t.arrive_by.unix
}
# group_by { |t|
# t.user.full_name
# }
avg_by_user.each { |name,t_unix_sum|
#puts name, t_unix_sum /
}
puts JSON.pretty_generate avg_by_user
end
# Fetch the demo data and parse it to JSON
def test1
network = {
"stop_1" => {
"name" => "Bus Stop @ Clay St & Van Ness (SW Corner)",
"lat" => 37.78497950946977,
"lng" => -122.42141685576958
},
"stop_2" => {
"name" => "Bus Stop @ 24th St & Valencia St (Valencia Farmers Market)",
"lat" => 37.74943967125433,
"lng" => -122.4194953139052
},
"stop_3" => {
"name" => "3215 Cesar Chavez St",
"lat" => 37.747813,
"lng" => -122.42141685576958
},
"stop_4" => {
"name" => "Hillsdale & Glendora",
"lat" => 37.53297200000001,
"lng" => -122.32666300000001
},
"stop_5" => {
"name" => "3000 Sand Hill Rd",
"lat" => 37.42837828801787,
"lng" => -122.20021340019768
},
"stop_6" => {
"name" => "3333 Coyote Hill Rd",
"lat" => 37.39789930850007,
"lng" => -122.1550005276009
},
"depot" => {
"name" => "Elizar's Pad, Oakland, CA",
"lat" => 37.806396,
"lng" => -122.278225
}
}
visits = {
"stop_2" => { },
"stop_5" => { },
"stop_4" => { },
"stop_3" => { },
"stop_1" => {
# "start" => "9:00",
# "end" => "12:00",
# "duration" => 10
},
"stop_6" => { },
}
fleet = {
"vehicle_1" => {
"start-location" => "depot"#,
# "end-location" => "stop_6",
# "shift-start" => "6:00",
# "shift-end" => "9:00"
}
}
data = {
network: network,
visits: visits,
fleet: fleet
}
# Get the route
route = Routific.getRoute(data)
puts route
puts JSON.pretty_generate route.vehicleRoutes
puts JSON.pretty_generate route
end
main()
@raywu
Copy link

raywu commented Apr 29, 2015

require 'routific'
require 'json'

# Set the Routific API token
Routific.setToken('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NTIwN2Q2MTRiMzYyMjBhMDAwYzhlNjEiLCJpYXQiOjE0MjgxOTI2NTl9.gzapZ2hLNwGxGBNUaXJX6xv4osfz4nPUu0KzdhwMm0s')

def main
    test1
    # test2
end

def test2
    trip_props = {
        id: t.id,
        arrive_by: t.arrive_by,
        pickup: {
            latlng: t.pickup.latlng.csv_point_str
        },
        dropoff: {
            latlng: t.dropoff.latlng.csv_point_str
        },
        user: {
            name: t.user.full_name
        }
    }
    avg_by_user = Trip.fulfilled.
        where { arrive_by > 2.weeks.ago }.
        reduce(Hash.new(0)) { |h, t|
            h[t.user.full_name] += t.arrive_by.unix
        }
        # group_by { |t|
        #     t.user.full_name
        # }
    avg_by_user.each { |name,t_unix_sum|
        #puts name, t_unix_sum /
    }
    puts JSON.pretty_generate avg_by_user
end

# stops = ttt.map {|t| [t.pickup, t.dropoff] }
# Hash[ # network/stops
#   *(ttt.map { |t|
#     s1 = t.pickup
#     s2 = t.dropoff
#     [
#       [ # pick
#         s1.formatted_address, {
#           "name" => s1.formatted_address,
#           "lat" => s1.lat,
#           "lng" => s1.lng,
#         }
#       ],
#       [ # drop
#         s2.formatted_address, {
#           "name" => s2.formatted_address,
#           "lat" => s2.lat,
#           "lng" => s2.lng,
#         }
#       ]
#     ]
#   }.flatten)
# ]
#
# Hash[ # visits/pickup, dropoff time
#   *(ttt.map { |t|
#     s1 = t.pickup
#     s2 = t.dropoff
#     [
#       [ # pick
#         s1.formatted_address, {
#           "start" => (t.pickup_time.in_time_zone(-8) - 5.minutes).strftime("%H:%M"),
#           "end" => (t.pickup_time.in_time_zone(-8) + 5.minutes).strftime("%H:%M"),
#           "duration" => 10
#         }
#       ],
#       [ # drop
#         s2.formatted_address, {
#           "start" => (t.dropoff_time.in_time_zone(-8) - 5.minutes).strftime("%H:%M"),
#           "end" => (t.dropoff_time.in_time_zone(-8) + 5.minutes).strftime("%H:%M"),
#           "duration" => 10
#         }
#       ]
#     ]
#   }.flatten)
# ]
#
# Hash[ #fleet/bus
#   ttt.map { |t|
#     [ t.bus.name, {
#       "start-location" => (t.pickup.formatted_address),
#     } ]
#   }
# ]


# Fetch the demo data and parse it to JSON
def test1
    network = {
      "Bus Stop @ 1783 Hayes" => {
        "name" => "Bus Stop @ 1783 Hayes",
        "lat" => 37.773937,
        "lng" => -122.445681
      },
      "Bus Stop @ 400 High St" => {
        "name" => "Bus Stop @ 400 High St",
        "lat" => 37.44471,
        "lng" => -122.164487
      },
      "Bus Stop @ Divisadero & McAllister (SW Corner)" => {
        "name" => "Bus Stop @ Divisadero & McAllister (SW Corner)",
        "lat" => 37.777624,
        "lng" => -122.438413
      },
      "Bus Stop @ 425 Broadway" => {
        "name" => "Bus Stop @ 425 Broadway",
        "lat" => 37.485367,
        "lng" => -122.204058
      },
      "Bus Stop @ Mission & 29th" => {
        "name" => "Bus Stop @ Mission & 29th",
        "lat" => 37.74381,
        "lng" => -122.421135
      },
      "Bus Stop on E Hillsdale Blvd, South of Foster City Blvd" => {
        "name" => "Bus Stop on E Hillsdale Blvd, South of Foster City Blvd",
        "lat" => 37.560793,
        "lng" => -122.269734
      },
      "Bus Stop @ 6th St and Harrison St (Chevron)" => {
        "name" => "Bus Stop @ 6th St and Harrison St (Chevron)",
        "lat" => 37.777327,
        "lng" => -122.404239
      },
      "Bus Stop @ Gilead" => {
        "name" => "Bus Stop @ Gilead",
        "lat" => 37.56957,
        "lng" => -122.280402
      },
      "Bus Stop on Cesar Chavez East of Valencia" => {
        "name" => "Bus Stop on Cesar Chavez East of Valencia",
        "lat" => 37.748043,
        "lng" => -122.419847
      },
      "Bus Stop @ Marine Pkwy & Twin Dolphin Dr" => {
        "name" => "Bus Stop @ Marine Pkwy & Twin Dolphin Dr",
        "lat" => 37.52849,
        "lng" => -122.263208
      },
      "Bus Stop @ Mission & 22nd" => {
        "name" => "Bus Stop @ Mission & 22nd",
        "lat" => 37.755169,
        "lng" => -122.418808
      },
      "Atrium Business Park" => {
        "name" => "Atrium Business Park",
        "lat" => 37.552719,
        "lng" => -122.291561
      },
      "Guerrero & 14th (SW Corner)" => {
        "name" => "Guerrero & 14th (SW Corner)",
        "lat" => 37.767915,
        "lng" => -122.424552
      },
      "2670 Middlefield Rd" => {
        "name" => "2670 Middlefield Rd",
        "lat" => 37.476875,
        "lng" => -122.212291
      },
      "Bus Stop @ 469 Fillmore" => {
        "name" => "Bus Stop @ 469 Fillmore",
        "lat" => 37.773759,
        "lng" => -122.430874
      },
      "Bus Stop @ Delaware St & 3rd Ave (Shell Station)" => {
        "name" => "Bus Stop @ Delaware St & 3rd Ave (Shell Station)",
        "lat" => 37.56725,
        "lng" => -122.319994
      },
      "Lafayette Park, Sacramento & Gough (NW corner)" => {
        "name" => "Lafayette Park, Sacramento & Gough (NW corner)",
        "lat" => 37.790986,
        "lng" => -122.425911
      },
      "Bus Stop @ 1730 S El Camino Real" => {
        "name" => "Bus Stop @ 1730 S El Camino Real",
        "lat" => 37.551827,
        "lng" => -122.313903
      },
      "Bus Stop @ Sutter & Hyde (NW corner)" => {
        "name" => "Bus Stop @ Sutter & Hyde (NW corner)",
        "lat" => 37.788221,
        "lng" => -122.41691
      },
      "Bus Stop @ 383 Oyster Point Blvd" => {
        "name" => "Bus Stop @ 383 Oyster Point Blvd",
        "lat" => 37.666529,
        "lng" => -122.383354
      },
      "Bus Stop @ 2635 Fillmore" => {
        "name" => "Bus Stop @ 2635 Fillmore",
        "lat" => 37.793809,
        "lng" => -122.434871
      },
      "Bus Stop @ 261 Hamilton" => {
        "name" => "Bus Stop @ 261 Hamilton",
        "lat" => 37.444711,
        "lng" => -122.16086
      },
      "Bus Stop @ Pacific Ave & Leavenworth St (NE corner)" => {
        "name" => "Bus Stop @ Pacific Ave & Leavenworth St (NE corner)",
        "lat" => 37.795764,
        "lng" => -122.416552
      },
      "Bus Stop @ 4005 Miranda Ave" => {
        "name" => "Bus Stop @ 4005 Miranda Ave",
        "lat" => 37.400071,
        "lng" => -122.137128
      },
      "165 9th Street (Chevron)" => {
        "name" => "165 9th Street (Chevron)",
        "lat" => 37.775263,
        "lng" => -122.413296
      },
      "Drive Way @ 275 Shoreline Dr" => {
        "name" => "Drive Way @ 275 Shoreline Dr",
        "lat" => 37.524952,
        "lng" => -122.257701
      },
      "Bus Stop @ Mission St & 19th St (Mission Thrift)" => {
        "name" => "Bus Stop @ Mission St & 19th St (Mission Thrift)",
        "lat" => 37.759802,
        "lng" => -122.419248
      },
      "Entrance to Bay Park Plaza" => {
        "name" => "Entrance to Bay Park Plaza",
        "lat" => 37.589454,
        "lng" => -122.341736
      },
      "Bus Stop @ Hermann & Fillmore" => {
        "name" => "Bus Stop @ Hermann & Fillmore",
        "lat" => 37.770249,
        "lng" => -122.429708
      },
      "Bus Stop @ 790 Quarry Rd" => {
        "name" => "Bus Stop @ 790 Quarry Rd",
        "lat" => 37.438335,
        "lng" => -122.171467
      },
      "Bus Stop @ 1875 S Grant St" => {
        "name" => "Bus Stop @ 1875 S Grant St",
        "lat" => 37.554035,
        "lng" => -122.302024
      },
      "Page Mill Road & Ramos Way (Palo Alto Square)" => {
        "name" => "Page Mill Road & Ramos Way (Palo Alto Square)",
        "lat" => 37.421131,
        "lng" => -122.143469
      }
    }

    visits = {
      "Bus Stop @ 1783 Hayes" => {
        "start" => "08:31",
        "end" => "08:41",
        "duration" => 10
      },
      "Bus Stop @ 400 High St" => {
        "start" => "07:31",
        "end" => "07:41",
        "duration" => 10
      },
      "Bus Stop @ Divisadero & McAllister (SW Corner)" => {
        "start" => "08:37",
        "end" => "08:47",
        "duration" => 10
      },
      "Bus Stop @ 425 Broadway" => {
        "start" => "09:26",
        "end" => "09:36",
        "duration" => 10
      },
      "Bus Stop @ Mission & 29th" => {
        "start" => "08:38",
        "end" => "08:48",
        "duration" => 10
      },
      "Bus Stop on E Hillsdale Blvd, South of Foster City Blvd" => {
        "start" => "09:25",
        "end" => "09:35",
        "duration" => 10
      },
      "Bus Stop @ 6th St and Harrison St (Chevron)" => {
        "start" => "08:00",
        "end" => "08:10",
        "duration" => 10
      },
      "Bus Stop @ Gilead" => {
        "start" => "08:41",
        "end" => "08:51",
        "duration" => 10
      },
      "Bus Stop on Cesar Chavez East of Valencia" => {
        "start" => "08:00",
        "end" => "08:10",
        "duration" => 10
      },
      "Bus Stop @ Marine Pkwy & Twin Dolphin Dr" => {
        "start" => "09:40",
        "end" => "09:50",
        "duration" => 10
      },
      "Bus Stop @ Mission & 22nd" => {
        "start" => "08:32",
        "end" => "08:42",
        "duration" => 10
      },
      "Atrium Business Park" => {
        "start" => "09:19",
        "end" => "09:29",
        "duration" => 10
      },
      "Guerrero & 14th (SW Corner)" => {
        "start" => "07:48",
        "end" => "07:58",
        "duration" => 10
      },
      "2670 Middlefield Rd" => {
        "start" => "08:47",
        "end" => "08:57",
        "duration" => 10
      },
      "Bus Stop @ 469 Fillmore" => {
        "start" => "07:44",
        "end" => "07:54",
        "duration" => 10
      },
      "Bus Stop @ Delaware St & 3rd Ave (Shell Station)" => {
        "start" => "08:20",
        "end" => "08:30",
        "duration" => 10
      },
      "Lafayette Park, Sacramento & Gough (NW corner)" => {
        "start" => "08:15",
        "end" => "08:25",
        "duration" => 10
      },
      "Bus Stop @ 1730 S El Camino Real" => {
        "start" => "09:17",
        "end" => "09:27",
        "duration" => 10
      },
      "Bus Stop @ Sutter & Hyde (NW corner)" => {
        "start" => "08:21",
        "end" => "08:31",
        "duration" => 10
      },
      "Bus Stop @ 383 Oyster Point Blvd" => {
        "start" => "08:52",
        "end" => "09:02",
        "duration" => 10
      },
      "Bus Stop @ 2635 Fillmore" => {
        "start" => "08:20",
        "end" => "08:30",
        "duration" => 10
      },
      "Bus Stop @ 261 Hamilton" => {
        "start" => "09:32",
        "end" => "09:42",
        "duration" => 10
      },
      "Bus Stop @ Pacific Ave & Leavenworth St (NE corner)" => {
        "start" => "09:10",
        "end" => "09:20",
        "duration" => 10
      },
      "Bus Stop @ 4005 Miranda Ave" => {
        "start" => "10:41",
        "end" => "10:51",
        "duration" => 10
      },
      "165 9th Street (Chevron)" => {
        "start" => "08:25",
        "end" => "08:35",
        "duration" => 10
      },
      "Drive Way @ 275 Shoreline Dr" => {
        "start" => "09:10",
        "end" => "09:20",
        "duration" => 10
      },
      "Bus Stop @ Mission St & 19th St (Mission Thrift)" => {
        "start" => "06:34",
        "end" => "06:44",
        "duration" => 10
      },
      "Entrance to Bay Park Plaza" => {
        "start" => "07:23",
        "end" => "07:33",
        "duration" => 10
      },
      "Bus Stop @ Hermann & Fillmore" => {
        "start" => "08:32",
        "end" => "08:42",
        "duration" => 10
      },
      "Bus Stop @ 790 Quarry Rd" => {
        "start" => "09:29",
        "end" => "09:39",
        "duration" => 10
      },
      "Bus Stop @ 1875 S Grant St" => {
        "start" => "09:26",
        "end" => "09:36",
        "duration" => 10
      },
      "Page Mill Road & Ramos Way (Palo Alto Square)" => {
        "start" => "07:25",
        "end" => "07:35",
        "duration" => 10
      }
    }

    fleet = {
      "Elizar Chevy Suburban" => {
        "start-location" => "Bus Stop @ Mission St & 19th St (Mission Thrift)"
      },
      "Aleks Ford Explorer" => {
        "start-location" => "165 9th Street (Chevron)"
      },
      "Elizar Sprinter Black" => {
        "start-location" => "Bus Stop on Cesar Chavez East of Valencia"
      },
      "Ilkhom Ford Explorer" => {
        "start-location" => "Bus Stop @ 469 Fillmore"
      },
      "Bus Ali" => {
        "start-location" => "Bus Stop @ 1783 Hayes"
      },
      "Nurettin Ford Explorer" => {
        "start-location" => "Bus Stop @ Hermann & Fillmore"
      }
    }

    data = {
      network: network,
      visits: visits,
      fleet: fleet
    }

    # Get the route
    route = Routific.getRoute(data)
    puts route
    puts JSON.pretty_generate Hash[route.vehicleRoutes.map {
      |key, waypoints|
      [
        key,
        waypoints.map { |w|
          {
            location_id: w.location_id,
            location_name: w.location_name,
            arrival_time: w.arrival_time,
            finish_time: w.finish_time,
          }
        }
      ]
    }]
end

main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment