-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsentinel.rb
46 lines (38 loc) · 1 KB
/
sentinel.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Sentinel < Source
def parse
routes = []
get_paths.each do |route_id, nodes|
# sort nodes by index to maintain nodes order
nodes.sort_by! { |node| node[:index] }
# iterate over every two consecutive nodes
nodes.each_cons(2) do |node1, node2|
route = {}
route[:start_node] = node1[:node]
route[:start_time] = node1[:time]
route[:end_node] = node2[:node]
route[:end_time] = node2[:time]
routes << route
end
end
puts routes
puts "-"*50
routes
end
private
def get_paths
paths = {}
custom_csv(@path).each do |row|
sentinel = {}
sentinel[:route_id] = clean(row[0])
sentinel[:node] = clean(row[1])
sentinel[:index] = clean(row[2])
sentinel[:time] = parse_time((clean(row[3])))
if paths[sentinel[:route_id]]
paths[sentinel[:route_id]] << sentinel
else
paths[sentinel[:route_id]] = [sentinel]
end
end
paths
end
end