Class: CTA::Stop

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

Overview

Note:

Current columns: [:stop_id, :stop_code, :stop_name, :stop_desc, :stop_lat, :stop_lon, :location_type, :parent_station, :wheelchair_boarding]

A Sequel::Model. This corresponds to stops.txt in the GTFS feed, though the CTA does not fully implement the standard.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_from_api_response(s) ⇒ Object

Internal method. Some CTA routes are seasonal, and are missing from the GTFS feed. However, the API still returns that info. So, we create a dummy CTA::Stop to fill in the gaps. I've emailed CTA developer support for clarification.


86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cta_redux/models/stop.rb', line 86

def self.new_from_api_response(s)
  CTA::Stop.unrestrict_primary_key
  stop = CTA::Stop.new({
    :stop_id => s["stpid"].to_i,
    :stop_name => s["stpnm"],
    :stop_lat => s["lat"].to_f,
    :stop_lon => s["lon"].to_f,
    :location_type => 3, # Bus in GTFS-land
    :stop_desc => "#{s["stpnm"]} (seasonal, generated from API results - missing from GTFS feed)"
  })
  CTA::Stop.restrict_primary_key

  stop
end

Instance Method Details

#child_stopsArray<CTA::Stop>

Stops can have a hierarchical relationship. The CTA designates certain stops as “parent stops” which contain “child stops” A great example of this is an 'L' station - the station itself will be a parent stop, but each platform will be a child stop.


13
# File 'lib/cta_redux/models/stop.rb', line 13

one_to_many :child_stops, :class => self, :key => :parent_station

#location_typeInteger


56
# File 'lib/cta_redux/models/stop.rb', line 56

alias_method :id, :stop_id

#parent_stationInteger


56
# File 'lib/cta_redux/models/stop.rb', line 56

alias_method :id, :stop_id

#parent_stopCTA::Stop

Stops can have a hierarchical relationship. The CTA designates certain stops as “parent stops” which contain “child stops” A great example of this is an 'L' station - the station itself will be a parent stop, but each platform will be a child stop.


18
# File 'lib/cta_redux/models/stop.rb', line 18

many_to_one :parent_stop, :class => self, :key => :parent_station

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

Returns predictions for this CTA::Stop. Accepts all options for BusTracker.predictions! or TrainTracker.predictions!, and will merge in # it's own stop/station/parent_station ID as needed.


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cta_redux/models/stop.rb', line 122

def predictions!(options = {})
  if self.stop_type == :bus
    CTA::BusTracker.predictions!(options.merge({:stops => self.stop_id}))
  else
    if self.stop_type == :rail
      CTA::TrainTracker.predictions!(options.merge({:station => self.stop_id}))
    else
      CTA::TrainTracker.predictions!(options.merge({:parent_station => self.stop_id}))
    end
  end
end

#routesSequel::Dataset

Returns a Sequel::Dataset that corresponds to all routes associated with this CTA::Stop at some point

Examples:

CTA::Stop.first.routes.first #=> #<CTA::Route...
CTA::Stop.first.routes.all   #=> [#<CTA::Route..., #<CTA::Route...]

69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cta_redux/models/stop.rb', line 69

def routes
  CTA::Route.with_sql("SELECT r.*\nFROM routes r\nWHERE r.route_id IN (\nSELECT DISTINCT t.route_id\nFROM stop_times st\nJOIN trips t ON st.trip_id = t.trip_id\nWHERE st.stop_id = '\#{self.stop_id}'\n)\n")
end

#stop_codeInteger Also known as: code


56
# File 'lib/cta_redux/models/stop.rb', line 56

alias_method :id, :stop_id

#stop_descString Also known as: description


56
# File 'lib/cta_redux/models/stop.rb', line 56

alias_method :id, :stop_id

#stop_idInteger Also known as: id


56
# File 'lib/cta_redux/models/stop.rb', line 56

alias_method :id, :stop_id

#stop_latFloat Also known as: lat


56
# File 'lib/cta_redux/models/stop.rb', line 56

alias_method :id, :stop_id

#stop_lonFloat Also known as: lon


56
# File 'lib/cta_redux/models/stop.rb', line 56

alias_method :id, :stop_id

#stop_nameString Also known as: name


56
# File 'lib/cta_redux/models/stop.rb', line 56

alias_method :id, :stop_id

#stop_type:bus, ...

The type of this stop. CTA partitions all stops on the numeric stop_id. It's unclear why the CTA does this, but the TrainTracker API seems to separate out parent stations and stations in their requests, which complicates following the GTFS spec to the letter. Additionally, because GTFS has no concept of differentiating stops based on the type of vehicle expected to service that stop, the numerical ID can also differentiate a bus stop from a rail stop. Bottom line? The CTA's APIs and GTFS itself is really confusing, and the actual reasons why this method exists are unclear. You should just be able to rely on it.


108
109
110
111
112
113
114
115
116
# File 'lib/cta_redux/models/stop.rb', line 108

def stop_type
  if self.stop_id < 30000
    :bus
  elsif self.stop_id < 40000
    :rail
  else
    :parent_station
  end
end

#transfers_fromArray<CTA::Stop>

GTFS defines a Transfer object, that describes where customers may transfer lines. This method defines which CTA::Stop customers may transfer from at this CTA::Stop. By then introspecting on Routes attached to these stops, you can find the transfer points between routes at this stop.


25
# File 'lib/cta_redux/models/stop.rb', line 25

one_to_many :transfers_from, :class => 'CTA::Transfer', :key => :from_stop_id

#transfers_toArray<CTA::Stop>

GTFS defines a Transfer object, that describes where customers may transfer lines. This method defines which CTA::Stop customers may transfer to at CTA::Stop. By then introspecting on Routes attached to these stops, you can find the transfer points between routes at this stop.


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

one_to_many :transfers_to, :class => 'CTA::Transfer', :key => :to_stop_id

#tripsArray<CTA::Trip>

A Route may be related to many trips, through CTA::StopTime objects


36
# File 'lib/cta_redux/models/stop.rb', line 36

many_to_many :trips, :left_key => :stop_id, :right_key => :trip_id, :join_table => :stop_times

#wheelchair_boardingtrue, false


56
# File 'lib/cta_redux/models/stop.rb', line 56

alias_method :id, :stop_id