Class: CTA::Bus

Inherits:
Trip
  • Object
show all
Defined in:
lib/cta_redux/models/bus.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, inherited from Trip This corresponds to trips.txt in the GTFS feed, though the CTA does not fully implement the standard.

Defined Under Namespace

Classes: Live, Prediction

Constant Summary

Constants inherited from Trip

Trip::BUS_ROUTES, Trip::L_ROUTES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Trip

#calendar, #route, #shapes, #stop_times, #stops

Instance Attribute Details

#liveLive

Note:

a CTA::Bus will only contain live data when augmented with an API::Response

Returns the Live data associated with this CTA::Bus, if available.

Returns:


9
10
11
# File 'lib/cta_redux/models/bus.rb', line 9

def live
  @live
end

Class Method Details

.find_active_run(run, timestamp, fuzz = false, direction = nil) ⇒ Object

Find a 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 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 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.


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cta_redux/models/bus.rb', line 51

def self.find_active_run(run, timestamp, fuzz = false, direction = nil)
  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
    WHERE t.route_id = '#{run}'
    #{direction ? "  AND t.direction = '#{direction.gsub("bound", "")}'" : '' }
      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)

29
# File 'lib/cta_redux/models/bus.rb', line 29

alias_method :id, :route_id

#directionString

Returns:

  • (String)

29
# File 'lib/cta_redux/models/bus.rb', line 29

alias_method :id, :route_id

#direction_idInteger

Returns:

  • (Integer)

29
# File 'lib/cta_redux/models/bus.rb', line 29

alias_method :id, :route_id

#predictions!(options = {}) ⇒ CTA::BusTracker::PredictionsResponse

Returns predictions for this CTA::Bus. Accepts all options for CTA::BusTracker.predictions!, and will merge in it's own vehicle_id or route_id if present.

Examples:

predictions!(:stops => 15895)
predictions!(:limit => 1)

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :vehicles (Array<String>, Array<Integer>, String, Integer)

    Vehicles to predict. Not available with :routes

  • :routes (Array<String>, Array<Integer>, String, Integer)

    Routes to predict. Not available with :vehicles

  • :stops (Array<String>, Array<Integer>, String, Integer)

    Stops along a route to predict. Required with :routes

  • :limit (String, Integer)

    Maximum number of predictions to return.

Returns:


81
82
83
84
# File 'lib/cta_redux/models/bus.rb', line 81

def predictions!(options = {})
  opts = (self.vehicle_id ? { :vehicles => self.vehicle_id } : { :routes => self.route_id })
  CTA::BusTracker.predictions!(options.merge(opts))
end

#route_idString Also known as: id

Returns:

  • (String)

29
# File 'lib/cta_redux/models/bus.rb', line 29

alias_method :id, :route_id

#schd_trip_idString Also known as: scheduled_trip_id, run

Returns:

  • (String)

29
# File 'lib/cta_redux/models/bus.rb', line 29

alias_method :id, :route_id

#service_idInteger

Returns:

  • (Integer)

29
# File 'lib/cta_redux/models/bus.rb', line 29

alias_method :id, :route_id

#shape_idInteger

Returns:

  • (Integer)

29
# File 'lib/cta_redux/models/bus.rb', line 29

alias_method :id, :route_id

#trip_idInteger

Returns:

  • (Integer)

29
# File 'lib/cta_redux/models/bus.rb', line 29

alias_method :id, :route_id

#wheelchair_accessibletrue, false

Returns:

  • (true, false)

29
# File 'lib/cta_redux/models/bus.rb', line 29

alias_method :id, :route_id