Class: CTA::Trip
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- CTA::Trip
- Defined in:
- lib/cta_redux/models/trip.rb
Overview
Current columns: [:route_id, :service_id, :trip_id, :direction_id, :block_id, :shape_id, :direction, :wheelchair_accessible, :schd_trip_id]
A Sequel::Model. This corresponds to trips.txt in the GTFS feed, though the CTA does not fully implement the standard.
Constant Summary collapse
- L_ROUTES =
["Brn", "G", "Pink", "P", "Org", "Red", "Blue", "Y"]
- BUS_ROUTES =
CTA::Trip.exclude(:route_id => L_ROUTES).select_map(:route_id).uniq
Class Method Summary collapse
-
.find_active_run(run, timestamp, fuzz = false) ⇒ Object
Find a Trip that should be happening, given a timestamp and a route or run.
Instance Method Summary collapse
- #block_id ⇒ Integer
- #calendar ⇒ CTA::Calendar
- #direction ⇒ String
- #direction_id ⇒ Integer
- #route ⇒ CTA::Route
- #route_id ⇒ String (also: #id)
- #schd_trip_id ⇒ String (also: #scheduled_trip_id, #run)
- #service_id ⇒ Integer
- #shape_id ⇒ Integer
-
#stop_times ⇒ Array<CTA::StopTime>
The StopTimes that are serviced on this Trip.
- #stops ⇒ Array<CTA::Stop>
- #trip_id ⇒ Integer
- #wheelchair_accessible ⇒ true, false
Class Method Details
.find_active_run(run, timestamp, fuzz = false) ⇒ Object
Find a CTA::Trip that should be happening, given a timestamp and a route or run. The CTA does not return GTFS trip_id information in either the BusTracker or TrainTracker API, so it is actually somewhat difficult to associate an API response to a CTA::Trip. However, we know what should be happening at any given time. This method attempts a fuzzy find - internally, we often first try to find the exact Trip that should be happening according to the schedule, and then failing that we assume that the CTA is running late and look for trips that should have ended within the past 90 minutes. This almost always finds something. That said, however, it means we may sometimes find a CTA::Trip that’s incorrect. In practical usage however, that doesn’t matter too much - most Trips for a run service the same stops. However, to be safe, your program may wish to compare certain other bits of the API responses to ensure we found something valid. For example, almost all Brown line trains service the same stops, so finding the wrong Trip doesn’t matter too much. However, a handful of Brown line runs throughout the dat actually change to Orange line trains at Midway - so, you may wish to verify that the destination of the Trip matches the reported destination of the API. Suggestions on how to approach this problem are most welcome (as are patches for better behavior).
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cta_redux/models/trip.rb', line 85 def self.find_active_run(run, , fuzz = false) if self.to_s == "CTA::Train" # This is admittedly hacky. join_str = "WHERE t.schd_trip_id = 'R#{run}'" else join_str = "WHERE t.route_id = '#{run}'" end d = .is_a?(DateTime) ? : DateTime.parse() wday = d.strftime("%A").downcase end_ts = (fuzz ? (d.to_time + (60 * 60 * 6) - (90 * 60)) : d).strftime("%H:%M:%S") Trip.with_sql(<<-SQL) SELECT t.* FROM trips t JOIN stop_times st ON t.trip_id = st.trip_id JOIN calendar c ON t.service_id = c.service_id #{join_str} AND c.start_date <= '#{d.to_s}' AND c.end_date >= '#{d.to_s}' AND c.#{wday} GROUP BY t.trip_id, st.departure_time HAVING MAX(st.departure_time) >= '#{end_ts}' SQL end |
Instance Method Details
#block_id ⇒ Integer
63 |
# File 'lib/cta_redux/models/trip.rb', line 63 alias_method :id, :route_id |
#calendar ⇒ CTA::Calendar
31 |
# File 'lib/cta_redux/models/trip.rb', line 31 many_to_one :calendar, :key => :service_id |
#direction ⇒ String
63 |
# File 'lib/cta_redux/models/trip.rb', line 63 alias_method :id, :route_id |
#direction_id ⇒ Integer
63 |
# File 'lib/cta_redux/models/trip.rb', line 63 alias_method :id, :route_id |
#route ⇒ CTA::Route
39 |
# File 'lib/cta_redux/models/trip.rb', line 39 many_to_one :route, :key => :route_id |
#route_id ⇒ String Also known as: id
63 |
# File 'lib/cta_redux/models/trip.rb', line 63 alias_method :id, :route_id |
#schd_trip_id ⇒ String Also known as: scheduled_trip_id, run
63 |
# File 'lib/cta_redux/models/trip.rb', line 63 alias_method :id, :route_id |
#service_id ⇒ Integer
63 |
# File 'lib/cta_redux/models/trip.rb', line 63 alias_method :id, :route_id |
#shape_id ⇒ Integer
63 |
# File 'lib/cta_redux/models/trip.rb', line 63 alias_method :id, :route_id |
#stop_times ⇒ Array<CTA::StopTime>
Returns The StopTimes that are serviced on this CTA::Trip.
35 |
# File 'lib/cta_redux/models/trip.rb', line 35 one_to_many :stop_times, :key => :trip_id |
#stops ⇒ Array<CTA::Stop>
43 |
# File 'lib/cta_redux/models/trip.rb', line 43 many_to_many :stops, :left_key => :trip_id, :right_key => :stop_id, :join_table => :stop_times |
#trip_id ⇒ Integer
63 |
# File 'lib/cta_redux/models/trip.rb', line 63 alias_method :id, :route_id |
#wheelchair_accessible ⇒ true, false
63 |
# File 'lib/cta_redux/models/trip.rb', line 63 alias_method :id, :route_id |