Class: CTA::Trip

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/cta_redux/models/trip.rb

Overview

Note:

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.

Direct Known Subclasses

Bus, Train

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

Instance Method Summary collapse

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).

Parameters:

  • run (String)

    The run or route to search for

  • timestamp (DateTime, String)

    The timestamp to search against.

  • fuzz (true, false) (defaults to: false)

    Whether or not to do an exact schedule search or a fuzzy search.



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, timestamp, 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 = timestamp.is_a?(DateTime) ? timestamp : DateTime.parse(timestamp)
  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_idInteger

Returns:

  • (Integer)


63
# File 'lib/cta_redux/models/trip.rb', line 63

alias_method :id, :route_id

#calendarCTA::Calendar

Returns The Calendar entry for this CTA::Trip. Can be used to determine if a given CTA::Trip is valid for a given date/time.

Returns:



31
# File 'lib/cta_redux/models/trip.rb', line 31

many_to_one :calendar, :key => :service_id

#directionString

Returns:

  • (String)


63
# File 'lib/cta_redux/models/trip.rb', line 63

alias_method :id, :route_id

#direction_idInteger

Returns:

  • (Integer)


63
# File 'lib/cta_redux/models/trip.rb', line 63

alias_method :id, :route_id

#routeCTA::Route

Returns The Route associated with this CTA::Trip.

Returns:



39
# File 'lib/cta_redux/models/trip.rb', line 39

many_to_one :route, :key => :route_id

#route_idString Also known as: id

Returns:

  • (String)


63
# File 'lib/cta_redux/models/trip.rb', line 63

alias_method :id, :route_id

#schd_trip_idString Also known as: scheduled_trip_id, run

Returns:

  • (String)


63
# File 'lib/cta_redux/models/trip.rb', line 63

alias_method :id, :route_id

#service_idInteger

Returns:

  • (Integer)


63
# File 'lib/cta_redux/models/trip.rb', line 63

alias_method :id, :route_id

#shape_idInteger

Returns:

  • (Integer)


63
# File 'lib/cta_redux/models/trip.rb', line 63

alias_method :id, :route_id

#stop_timesArray<CTA::StopTime>

Returns The StopTimes that are serviced on this CTA::Trip.

Returns:



35
# File 'lib/cta_redux/models/trip.rb', line 35

one_to_many :stop_times, :key => :trip_id

#stopsArray<CTA::Stop>

Returns All Stops serviced on this CTA::Trip.

Returns:



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_idInteger

Returns:

  • (Integer)


63
# File 'lib/cta_redux/models/trip.rb', line 63

alias_method :id, :route_id

#wheelchair_accessibletrue, false

Returns:

  • (true, false)


63
# File 'lib/cta_redux/models/trip.rb', line 63

alias_method :id, :route_id