Class: CTA::TrainTracker::ArrivalsResponse

Inherits:
API::Response show all
Defined in:
lib/cta_redux/api/train_tracker.rb

Instance Attribute Summary collapse

Attributes inherited from API::Response

#error, #parsed_body, #raw_body, #timestamp

Instance Method Summary collapse

Constructor Details

#initialize(parsed_body, raw_body, debug) ⇒ ArrivalsResponse

Returns a new instance of ArrivalsResponse.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cta_redux/api/train_tracker.rb', line 13

def initialize(parsed_body, raw_body, debug)
  super(parsed_body, raw_body, debug)

  eta_map = Array.wrap(parsed_body["ctatt"]["eta"]).inject({}) do |h, eta|
    h[eta["rt"]] ||= []
    h[eta["rt"]] << eta

    h
  end

  @routes = eta_map.map do |rt, etas|
    trains = etas.map do |t|
      train = CTA::Train.find_active_run(t["rn"], self.timestamp, (t["isDly"] == "1")).first
      if !train
        train = CTA::Train.find_active_run(t["rn"], self.timestamp, true).first
      end
      position = t.select { |k,v| ["lat", "lon", "heading"].include?(k) }
      train.live = CTA::Train::Live.new(position, t)

      train
    end

    route = CTA::Route.where(:route_id => rt.capitalize).first
    route.live = CTA::Route::Live.new(trains)

    route
  end

  @trains = @routes.map { |r| r.live.vehicles }.flatten
  @predictions = @trains.map { |t| t.live.predictions }.flatten
end

Instance Attribute Details

#predictionsArray<CTA::Train::Prediction> (readonly)

Note:

Convenience method, equivalent to calling trains.map { |t| t.live.predictions }.flatten

Returns An array of CTA::Train::Prediction objects that correspond to the predictions requested.

Returns:



11
12
13
# File 'lib/cta_redux/api/train_tracker.rb', line 11

def predictions
  @predictions
end

#routesArray<CTA::Route> (readonly)

Returns An array of Route objects that correspond to the predictions requested.

Returns:



5
6
7
# File 'lib/cta_redux/api/train_tracker.rb', line 5

def routes
  @routes
end

#trainsArray<CTA::Train> (readonly)

Note:

Convenience method, equivalent to calling routes.map { |r| r.vehicles }.flatten

Returns An array of CTA::Train objects that correspond to the predictions requested.

Returns:



8
9
10
# File 'lib/cta_redux/api/train_tracker.rb', line 8

def trains
  @trains
end