Class: CTA::TrainTracker::PositionsResponse

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) ⇒ PositionsResponse

Returns a new instance of PositionsResponse.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cta_redux/api/train_tracker.rb', line 76

def initialize(parsed_body, raw_body, debug)
  super(parsed_body, raw_body, debug)
  @routes = Array.wrap(parsed_body["ctatt"]["route"]).map do |route|
    rt = Route.where(:route_id => route["name"].capitalize).first

    trains = Array.wrap(route["train"]).map do |train|
      t = CTA::Train.find_active_run(train["rn"], self.timestamp, (train["isDly"] == "1"), train['destNm']).first
      if !t # Sometimes the CTA doesn't report things as delayed even when they ARE
        t = CTA::Train.find_active_run(train["rn"], self.timestamp, true, train['destNm']).first
      end

      if !t
        puts "Couldn't find train #{train["rn"]} - this is likely a bug."
        next
      end

      position = train.select { |k,v| ["lat", "lon", "heading"].include?(k) }
      t.live = CTA::Train::Live.new(position, train)

      t
    end

    rt.live = CTA::Route::Live.new(trains)
    rt
  end.compact

  @trains = @routes.compact.map { |r| r.live.vehicles }.flatten
  @predictions = @trains.compact.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 positions requested.

Returns:



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

def predictions
  @predictions
end

#routesArray<CTA::Route> (readonly)

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

Returns:



68
69
70
# File 'lib/cta_redux/api/train_tracker.rb', line 68

def routes
  @routes
end

#trainsArray<CTA::Train> (readonly)

Note:

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

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

Returns:



71
72
73
# File 'lib/cta_redux/api/train_tracker.rb', line 71

def trains
  @trains
end