Class: CUMTD
- Inherits:
-
Object
show all
- Includes:
- HTTParty
- Defined in:
- lib/cumtd.rb
Class Method Summary
collapse
Instance Method Summary
collapse
-
#api_key ⇒ Object
-
#get_departures_by_stop(stop) ⇒ Object
-
#get_reroutes ⇒ Object
-
#get_routes ⇒ Object
-
#get_routes_by_stop(stop) ⇒ Object
-
#get_shape_between_stops(begin_stop_id, end_stop_id, shape_id) ⇒ Object
-
#get_shape_by_id(shape_id) ⇒ Object
-
#get_stop_by_id(stop_id) ⇒ Object
-
#get_stop_times_by_stop(stop_id, route_id = nil, date = Time.now.to_s) ⇒ Object
-
#get_stop_times_by_trip(trip_id) ⇒ Object
-
#get_stops ⇒ Object
-
#get_stops_by_lat_lon(lat, lon, count = 20) ⇒ Object
-
#get_stops_by_search(query, count = 10) ⇒ Object
-
#get_trip_by_id(trip_id) ⇒ Object
-
#get_trips_by_block(block_id) ⇒ Object
-
#get_vehicle_by_id(vehicle_id) ⇒ Object
-
#get_vehicles ⇒ Object
-
#get_vehicles_by_route_id(route_id) ⇒ Object
-
#initialize(api_key, stops_file = nil, routes_file = nil, serialize_path = File.expand_path(File.dirname(__FILE__))) ⇒ CUMTD
constructor
-
#nearest_departures(stops) ⇒ Object
Returns an array of hashes.
-
#print_all_departures(stops) ⇒ Object
-
#serialize_routes(file_location) ⇒ Object
-
#serialize_stops(file_location) ⇒ Object
-
#serialize_vehicles(vehicles, file_location) ⇒ Object
Constructor Details
#initialize(api_key, stops_file = nil, routes_file = nil, serialize_path = File.expand_path(File.dirname(__FILE__))) ⇒ CUMTD
Returns a new instance of CUMTD.
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
|
# File 'lib/cumtd.rb', line 14
def initialize(api_key, stops_file=nil, routes_file=nil, serialize_path=File.expand_path(File.dirname(__FILE__)))
@api_key = api_key
@@reroutes = Array.new
@@all_stops = Array.new
if stops_file
object = nil
File.open(stops_file,"rb") {|f| @@all_stops = YAML.load(f)}
else
self.get_stops
end
@@all_routes = Array.new
if routes_file
object = nil
File.open(routes_file,"rb") {|f| @@all_routes = YAML.load(f)}
else
self.get_routes
end
unless serialize_path == :no_serialize
serialize_stops(File.join(serialize_path, 'stops.yaml'))
serialize_routes(File.join(serialize_path, 'routes.yaml'))
end
end
|
Class Method Details
.all_routes ⇒ Object
50
51
52
|
# File 'lib/cumtd.rb', line 50
def self.all_routes
@@all_routes
end
|
.all_stops ⇒ Object
46
47
48
|
# File 'lib/cumtd.rb', line 46
def self.all_stops
@@all_stops
end
|
.reroutes ⇒ Object
54
55
56
|
# File 'lib/cumtd.rb', line 54
def self.reroutes
@@reroutes
end
|
Instance Method Details
#api_key ⇒ Object
42
43
44
|
# File 'lib/cumtd.rb', line 42
def api_key
@api_key
end
|
#get_departures_by_stop(stop) ⇒ Object
135
136
137
138
139
140
141
142
|
# File 'lib/cumtd.rb', line 135
def get_departures_by_stop(stop)
response = self.class.get("/GetDeparturesByStop?key=#{@api_key}&stop_id=#{stop.stop_id}")
departures = Array.new
response["departures"].each do |departure|
departures << Departure.new(departure)
end
return departures
end
|
#get_reroutes ⇒ Object
94
95
96
97
98
99
100
101
|
# File 'lib/cumtd.rb', line 94
def get_reroutes
response = self.class.get("/GetReroutes?key=#{@api_key}")
@@reroutes.clear
response["reroutes"].each do |reroute|
@@reroutes << Reroute.new(reroute)
end unless response["reroutes"].nil?
return @@reroutes
end
|
#get_routes ⇒ Object
85
86
87
88
89
90
91
92
|
# File 'lib/cumtd.rb', line 85
def get_routes
response = self.class.get("/GetRoutes?key=#{@api_key}")
@@all_routes.clear
response["routes"].each do |route|
@@all_routes << Route.new(route)
end
return @@all_routes
end
|
#get_routes_by_stop(stop) ⇒ Object
120
121
122
123
124
125
126
127
|
# File 'lib/cumtd.rb', line 120
def get_routes_by_stop(stop)
response = self.class.get("/GetRoutesByStop?key=#{@api_key}&stop_id=#{stop.stop_id}")
routes = Array.new
response["routes"].each do |route|
routes << Route.new(route)
end
return routes
end
|
#get_shape_between_stops(begin_stop_id, end_stop_id, shape_id) ⇒ Object
231
232
233
234
235
236
237
238
|
# File 'lib/cumtd.rb', line 231
def get_shape_between_stops(begin_stop_id, end_stop_id, shape_id)
response = self.class.get("/GetShape?key=#{@api_key}&begin_stop_id=#{begin_stop_id}&end_stop_id=#{end_stop_id}&shape_id=#{shape_id}")
shapes = Array.new
response["shapes"].each do |shape|
shapes << Shape.new(shape)
end
return shapes
end
|
#get_shape_by_id(shape_id) ⇒ Object
222
223
224
225
226
227
228
229
|
# File 'lib/cumtd.rb', line 222
def get_shape_by_id(shape_id)
response = self.class.get("/GetShape?key=#{@api_key}&shape_id=#{shape_id}")
shapes = Array.new
response["shapes"].each do |shape|
shapes << Shape.new(shape)
end
return shapes
end
|
#get_stop_by_id(stop_id) ⇒ Object
129
130
131
132
133
|
# File 'lib/cumtd.rb', line 129
def get_stop_by_id(stop_id)
response = self.class.get("/GetStop?key=#{@api_key}&stop_id=#{stop_id}")
stop = Stop.new(response["stops"])
return stop
end
|
#get_stop_times_by_stop(stop_id, route_id = nil, date = Time.now.to_s) ⇒ Object
240
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/cumtd.rb', line 240
def get_stop_times_by_stop(stop_id, route_id=nil, date=Time.now.to_s)
if !route_id
response = self.class.get("/GetStopTimesByStop?key=#{@api_key}&stop_id=#{stop_id}")
else
response = self.class.get("/GetStopTimesByStop?key=#{@api_key}&stop_id=#{stop_id}&route_id=#{route_id}")
end
stop_times = Array.new
response["stop_times"].each do |stop_time|
stop_times << StopTime.new(stop_time)
end
return stop_times
end
|
#get_stop_times_by_trip(trip_id) ⇒ Object
176
177
178
179
180
181
182
183
|
# File 'lib/cumtd.rb', line 176
def get_stop_times_by_trip(trip_id)
response = self.class.get("/GetStopTimesByTrip?key=#{@api_key}&trip_id=#{trip_id}")
stop_times = Array.new
response["stop_times"].each do |stop_time|
stop_times << StopTime.new(stop_time)
end
return stop_times
end
|
#get_stops ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/cumtd.rb', line 76
def get_stops
response = self.class.get("/GetStops?key=#{@api_key}")
@@all_stops.clear
response["stops"].each do |stop|
@@all_stops << Stop.new(stop)
end
return @@all_stops
end
|
#get_stops_by_lat_lon(lat, lon, count = 20) ⇒ Object
103
104
105
106
107
108
109
110
|
# File 'lib/cumtd.rb', line 103
def get_stops_by_lat_lon(lat, lon, count=20)
response = self.class.get("/GetStopsByLatLon?key=#{@api_key}&lat=#{lat}&lon=#{lon}&count=#{count}")
stops = Array.new
response["stops"].each do |stop|
stops << Stop.new(stop)
end
return stops
end
|
#get_stops_by_search(query, count = 10) ⇒ Object
167
168
169
170
171
172
173
174
|
# File 'lib/cumtd.rb', line 167
def get_stops_by_search(query, count=10)
response = self.class.get("/GetStopsBySearch?key=#{@api_key}&query=#{query}&count=#{count}")
stops = Array.new
response["stops"].each do |stop|
stops << Stop.new(stop)
end
return stops
end
|
#get_trip_by_id(trip_id) ⇒ Object
208
209
210
211
|
# File 'lib/cumtd.rb', line 208
def get_trip_by_id(trip_id)
response = self.class.get("/GetTrip?key=#{@api_key}&trip_id=#{trip_id}")
return Trip.new(response["trips"].first)
end
|
#get_trips_by_block(block_id) ⇒ Object
213
214
215
216
217
218
219
220
|
# File 'lib/cumtd.rb', line 213
def get_trips_by_block(block_id)
response = self.class.get("/GetTripsByBlock?key=#{@api_key}&block_id=#{block_id}")
trips = Array.new
response["trips"].each do |trip|
trips << Trip.new(trip)
end
return trips
end
|
#get_vehicle_by_id(vehicle_id) ⇒ Object
194
195
196
197
|
# File 'lib/cumtd.rb', line 194
def get_vehicle_by_id(vehicle_id)
response = self.class.get("/GetVehicle?key=#{@api_key}&vehicle_id=#{vehicle_id}")
return Vehicle.new(response["vehicles"].first)
end
|
#get_vehicles ⇒ Object
185
186
187
188
189
190
191
192
|
# File 'lib/cumtd.rb', line 185
def get_vehicles
response = self.class.get("/GetVehicles?key=#{@api_key}")
vehicles = Array.new
response["vehicles"].each do |vehicle|
vehicles << Vehicle.new(vehicle)
end
return vehicles
end
|
#get_vehicles_by_route_id(route_id) ⇒ Object
199
200
201
202
203
204
205
206
|
# File 'lib/cumtd.rb', line 199
def get_vehicles_by_route_id(route_id)
response = self.class.get("/GetVehiclesByRoute?key=#{@api_key}&route_id=#{route_id}")
vehicles = Array.new
response["vehicles"].each do |vehicle|
vehicles << Vehicle.new(vehicle)
end
return vehicles
end
|
#nearest_departures(stops) ⇒ Object
Returns an array of hashes. Each hash contains a “stop” key denoting the stop and the “departures” key contains one departure. These hashes are sorted based on length of time until they depart, given stops to get departures for.
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/cumtd.rb', line 155
def nearest_departures(stops)
master_deps = Array.new
stops.each do |stop|
stops_from_deps = get_departures_by_stop(stop)
stops_from_deps.each do |stop_dep|
master_deps << Hash["stop", stop, "departures", stop_dep]
end
end
return master_deps.sort_by! { |stop| stop["departures"].expected_mins }
end
|
#print_all_departures(stops) ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/cumtd.rb', line 112
def print_all_departures(stops)
stops.each do |stop|
deps = get_departures_by_stop(stop["stop_id"])
stop_t = stop
print_departures(deps, stop_t)
end
end
|
#serialize_routes(file_location) ⇒ Object
64
65
66
67
68
|
# File 'lib/cumtd.rb', line 64
def serialize_routes(file_location)
File.open(file_location, "wb") do |file|
YAML.dump(@@all_routes,file)
end
end
|
#serialize_stops(file_location) ⇒ Object
58
59
60
61
62
|
# File 'lib/cumtd.rb', line 58
def serialize_stops(file_location)
File.open(file_location, "wb") do |file|
YAML.dump(@@all_stops,file)
end
end
|
#serialize_vehicles(vehicles, file_location) ⇒ Object
70
71
72
73
74
|
# File 'lib/cumtd.rb', line 70
def serialize_vehicles(vehicles, file_location)
File.open(file_location, "wb") do |file|
YAML.dump(vehicles,file)
end
end
|