Class: CTA::TrainTracker
- Inherits:
-
Object
- Object
- CTA::TrainTracker
- Defined in:
- lib/cta_redux/train_tracker.rb,
lib/cta_redux/api/train_tracker.rb,
lib/cta_redux/faraday_middleware/train_tracker_parser.rb
Defined Under Namespace
Classes: ArrivalsResponse, FollowResponse, Parser, PositionsResponse
Class Method Summary collapse
-
.arrivals!(options = {}) ⇒ CTA::TrainTracker::ArrivalsResponse
Returns the arrivals for a route, or station.
-
.cache ⇒ Object
Returns the underlying cache object caching responses (if we’re actually caching responses).
-
.cache=(cache) ⇒ Object
Sets the underlying cache object caching responses.
-
.cache_responses ⇒ true, false
Returns whether or not cta_redux is caching responses.
-
.cache_responses=(should_cache) ⇒ Object
Sets whether or not cta_redux is caching responses.
-
.connection ⇒ Object
Returns the connection object we use to talk to the TrainTracker API.
-
.debug ⇒ Object
Returns the debug status of the API.
-
.debug=(debug) ⇒ Object
Sets the debug status of the API.
-
.follow!(options = {}) ⇒ CTA::TrainTracker::FollowResponse
Returns a set of upcoming positions for a train/run.
-
.key ⇒ String
Returns the current API key used to talk to TrainTracker.
-
.key=(key) ⇒ Object
Sets the API key used to talk to TrainTracker.
-
.locations!(options = {}) ⇒ CTA::TrainTracker::LocationsResponse
Returns the position and next station of all trains in service.
-
.positions!(options = {}) ⇒ CTA::TrainTracker::LocationsResponse
Returns the position and next station of all trains in service.
-
.predictions!(options = {}) ⇒ CTA::TrainTracker::ArrivalsResponse
Returns the arrivals for a route, or station.
Class Method Details
.arrivals!(options = {}) ⇒ CTA::TrainTracker::ArrivalsResponse
Returns the arrivals for a route, or station
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/cta_redux/train_tracker.rb', line 30 def self.arrivals!(={}) allowed_keys = [:route, :parent_station, :station, :limit] if .keys.any? { |k| !allowed_keys.include?(k) } raise "Illegal option!" end has_map = .has_key?(:parent_station) has_stop = .has_key?(:station) route = Array.wrap([:route]).flatten.compact.uniq map = Array.wrap([:parent_station]).flatten.compact.uniq stop = Array.wrap([:station]).flatten.compact.uniq limit = Array.wrap([:limit]).flatten.compact.uniq.first.to_i if [:limit] if route.size > 1 raise "No more than 1 route may be specified!" end if map.size > 1 || stop.size > 1 raise "No more than 1 station or parent_station may be specified!" end if !(has_map || has_stop) raise "You must specify a station or a parent_station! Try arrivals(:station => 30280..." end params = {} params.merge!({ :mapid => map.first }) if [:parent_station] params.merge!({ :stpid => stop.first }) if [:station] params.merge!({ :max => limit }) if [:limit] params.merge!({ :rt => route.first }) if route.any? connection.get('ttarrivals.aspx', params) end |
.cache ⇒ Object
Returns the underlying cache object caching responses (if we’re actually caching responses)
169 170 171 172 173 174 175 176 |
# File 'lib/cta_redux/train_tracker.rb', line 169 def self.cache if self.cache_responses # This is ugly @cache || self.connection.builder.handlers.find { |x| x == FaradayMiddleware::Caching }.instance_variable_get(:@args).first else nil end end |
.cache=(cache) ⇒ Object
Setting the cache object resets the connection. If you’re using the default SimpleCache strategy (built-in 60 second caching), then it will also clear the cache.
Sets the underlying cache object caching responses. Any object can be used that responds to #read, #write, and #fetch
182 183 184 185 |
# File 'lib/cta_redux/train_tracker.rb', line 182 def self.cache=(cache) @cache = cache @connection = nil end |
.cache_responses ⇒ true, false
Returns whether or not cta_redux is caching responses
156 157 158 |
# File 'lib/cta_redux/train_tracker.rb', line 156 def self.cache_responses @cache_responses end |
.cache_responses=(should_cache) ⇒ Object
Sets whether or not cta_redux is caching responses
162 163 164 165 |
# File 'lib/cta_redux/train_tracker.rb', line 162 def self.cache_responses=(should_cache) @cache_responses = should_cache @connection = nil end |
.connection ⇒ Object
Returns the connection object we use to talk to the TrainTracker API
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/cta_redux/train_tracker.rb', line 6 def self.connection raise "You need to set a developer key first. Try CTA::TrainTracker.key = 'foo'." unless @key @connection ||= Faraday.new do |faraday| faraday.url_prefix = 'http://lapi.transitchicago.com/api/1.0/' faraday.params = { :key => @key } faraday.use CTA::TrainTracker::Parser, !!@debug if @cache_responses faraday.response :caching, (@cache || SimpleCache.new(Hash.new)) end faraday.adapter Faraday.default_adapter end end |
.debug ⇒ Object
Returns the debug status of the API. When in debug mode, all API responses will additionally return the parsed XML tree, and the original XML for inspection
142 143 144 |
# File 'lib/cta_redux/train_tracker.rb', line 142 def self.debug !!@debug end |
.debug=(debug) ⇒ Object
Sets the debug status of the API. When in debug mode, all API responses will additionally return the parsed XML tree, and the original XML for inspection
149 150 151 152 |
# File 'lib/cta_redux/train_tracker.rb', line 149 def self.debug=(debug) @debug = debug @connection = nil end |
.follow!(options = {}) ⇒ CTA::TrainTracker::FollowResponse
Returns a set of upcoming positions for a train/run
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cta_redux/train_tracker.rb', line 84 def self.follow!(={}) raise "Must specify a run! Try follow(:run => 914)..." unless .has_key?(:run) runs = Array.wrap([:run]).flatten.compact.uniq if runs.size > 1 raise "Only one run may be specified!" end connection.get('ttfollow.aspx', { :runnumber => runs.first }) end |
.key ⇒ String
Returns the current API key used to talk to TrainTracker
128 129 130 |
# File 'lib/cta_redux/train_tracker.rb', line 128 def self.key @key end |
.key=(key) ⇒ Object
If using SimpleCache as a caching strategy, this resets the cache.
Sets the API key used to talk to TrainTracker
135 136 137 138 |
# File 'lib/cta_redux/train_tracker.rb', line 135 def self.key=(key) @key = key @connection = nil end |
.locations!(options = {}) ⇒ CTA::TrainTracker::LocationsResponse
Returns the position and next station of all trains in service.
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cta_redux/train_tracker.rb', line 102 def self.locations!(={}) unless .has_key?(:routes) raise "Must specify at least one route! (Try locations(:routes => [:red, :blue]) )" end rt = Array.wrap([:routes]).flatten.compact.map { |r| (CTA::Train::FRIENDLY_L_ROUTES[r] || r).to_s } if rt.size > 8 raise "No more than 8 routes may be specified!" end connection.get('ttpositions.aspx', { :rt => rt.join(',') }) end |
.positions!(options = {}) ⇒ CTA::TrainTracker::LocationsResponse
Returns the position and next station of all trains in service.
122 123 124 |
# File 'lib/cta_redux/train_tracker.rb', line 122 def self.positions!(={}) self.locations!() end |
.predictions!(options = {}) ⇒ CTA::TrainTracker::ArrivalsResponse
Returns the arrivals for a route, or station
74 75 76 |
# File 'lib/cta_redux/train_tracker.rb', line 74 def self.predictions!(={}) self.arrivals!() end |