Class: CTA::TrainTracker

Inherits:
Object
  • Object
show all
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

Class Method Details

.arrivals!(options = {}) ⇒ CTA::TrainTracker::ArrivalsResponse

Returns the arrivals for a route, or station

Examples:

CTA::TrainTracker.arrivals!(:route => :red)

Parameters:

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

Options Hash (options):

  • :route (String, Integer)

    The route to query

  • :station (String, Integer)

    The station to query for arrivals

  • :parent_station (String, Integer)

    The parent station to query for arrivals.

  • :limit (String, Integer)

    Maximum number of results to return

Returns:



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!(options={})
  allowed_keys = [:route, :parent_station, :station, :limit]
  if options.keys.any? { |k| !allowed_keys.include?(k) }
    raise "Illegal option!"
  end

  has_map  = options.has_key?(:parent_station)
  has_stop = options.has_key?(:station)

  route = Array.wrap(options[:route]).flatten.compact.uniq
  map   = Array.wrap(options[:parent_station]).flatten.compact.uniq
  stop  = Array.wrap(options[:station]).flatten.compact.uniq
  limit = Array.wrap(options[:limit]).flatten.compact.uniq.first.to_i if options[: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 options[:parent_station]
  params.merge!({ :stpid => stop.first }) if options[:station]
  params.merge!({ :max => limit }) if options[:limit]
  params.merge!({ :rt => route.first }) if route.any?

  connection.get('ttarrivals.aspx', params)
end

.cacheObject

Returns the underlying cache object caching responses (if we’re actually caching responses)

Returns:

  • (Object)


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

Note:

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

Parameters:

  • cache (Object)


182
183
184
185
# File 'lib/cta_redux/train_tracker.rb', line 182

def self.cache=(cache)
  @cache = cache
  @connection = nil
end

.cache_responsestrue, false

Returns whether or not cta_redux is caching responses

Returns:

  • (true, false)


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

Parameters:

  • should_cache (true, false)


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

.connectionObject

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

.debugObject

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

Parameters:

  • debug (true, false)


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

Examples:

CTA::TrainTracker.follow!(:run => 914)

Parameters:

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

Options Hash (options):

  • :run (String, Integer)

    The run number of the train to follow

Returns:



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cta_redux/train_tracker.rb', line 84

def self.follow!(options={})
  raise "Must specify a run! Try follow(:run => 914)..." unless options.has_key?(:run)

  runs = Array.wrap(options[:run]).flatten.compact.uniq

  if runs.size > 1
    raise "Only one run may be specified!"
  end

  connection.get('ttfollow.aspx', { :runnumber => runs.first })
end

.keyString

Returns the current API key used to talk to TrainTracker

Returns:

  • (String)

    the api key



128
129
130
# File 'lib/cta_redux/train_tracker.rb', line 128

def self.key
  @key
end

.key=(key) ⇒ Object

Note:

If using SimpleCache as a caching strategy, this resets the cache.

Sets the API key used to talk to TrainTracker

Parameters:

  • key (String)

    The key to use



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.

Examples:

CTA::TrainTracker.locations!(:route => [:red, :blue])

Parameters:

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

Options Hash (options):

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

    Routes for which to return positions

Returns:

  • (CTA::TrainTracker::LocationsResponse)


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!(options={})
  unless options.has_key?(:routes)
    raise "Must specify at least one route! (Try locations(:routes => [:red, :blue]) )"
  end

  rt = Array.wrap(options[: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.

Examples:

CTA::TrainTracker.positions!(:route => [:red, :blue])

Parameters:

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

Options Hash (options):

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

    Routes for which to return positions

Returns:

  • (CTA::TrainTracker::LocationsResponse)


122
123
124
# File 'lib/cta_redux/train_tracker.rb', line 122

def self.positions!(options={})
  self.locations!(options)
end

.predictions!(options = {}) ⇒ CTA::TrainTracker::ArrivalsResponse

Returns the arrivals for a route, or station

Examples:

CTA::TrainTracker.predicitons!(:route => :red)

Parameters:

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

Options Hash (options):

  • :route (String, Integer)

    The route to query

  • :station (String, Integer)

    The station to query for arrivals

  • :parent_station (String, Integer)

    The parent station to query for arrivals.

  • :limit (String, Integer)

    Maximum number of results to return

Returns:



74
75
76
# File 'lib/cta_redux/train_tracker.rb', line 74

def self.predictions!(options={})
  self.arrivals!(options)
end