Class: CTA::TrainTracker::PositionsResponse
- Inherits:
-
API::Response
- Object
- API::Response
- CTA::TrainTracker::PositionsResponse
- Defined in:
- lib/cta_redux/api/train_tracker.rb
Instance Attribute Summary collapse
-
#predictions ⇒ Array<CTA::Train::Prediction>
readonly
An array of CTA::Train::Prediction objects that correspond to the positions requested.
-
#routes ⇒ Array<CTA::Route>
readonly
An array of Route objects that correspond to the positions requested.
-
#trains ⇒ Array<CTA::Train>
readonly
An array of CTA::Train objects that correspond to the positions requested.
Attributes inherited from API::Response
#error, #parsed_body, #raw_body, #timestamp
Instance Method Summary collapse
-
#initialize(parsed_body, raw_body, debug) ⇒ PositionsResponse
constructor
A new instance of PositionsResponse.
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., (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., 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
#predictions ⇒ Array<CTA::Train::Prediction> (readonly)
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.
74 75 76 |
# File 'lib/cta_redux/api/train_tracker.rb', line 74 def predictions @predictions end |
#routes ⇒ Array<CTA::Route> (readonly)
Returns An array of Route objects that correspond to the positions requested.
68 69 70 |
# File 'lib/cta_redux/api/train_tracker.rb', line 68 def routes @routes end |
#trains ⇒ Array<CTA::Train> (readonly)
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.
71 72 73 |
# File 'lib/cta_redux/api/train_tracker.rb', line 71 def trains @trains end |