Skip to content

Instantly share code, notes, and snippets.

@deversmann
Last active April 29, 2022 14:52
Show Gist options
  • Save deversmann/6c5ccb54433bf13fc64273f4706d771b to your computer and use it in GitHub Desktop.
Save deversmann/6c5ccb54433bf13fc64273f4706d771b to your computer and use it in GitHub Desktop.
cta_train_widget

cta_train_widget: Smashing Widget for CTA Train Routes

Description

A widget for the Smashing Dashboard system to display bus arrival times at one or more stops along Chicago's CTA bus routes.

Screenshot

cta_train_widget screenshot

Installation

smashing install 6c5ccb54433bf13fc64273f4706d771b

or

  • Copy cta_train_widget.coffee,cta_train_widget.html,cta_train_widget.scss into widget/cta_train_widget/
  • Copy cta_train_widget.rb into jobs/

Usage

In cta_train_widget.rb replace the following:

Add the widget to your dashboard layout like this:

<li data-row="1" data-col="1" data-sizex="3" data-sizey="1">
    <div data-id="cta_train_widget" data-view="CTATrainWidget"></div>
</li>

A width of either 2 or 3 usually looks good. If a height of 2 or more is used, the max variable in cta_train_widget.rb can be changed to increase the number of results to fill the space.


Contents of this gist licensed as follows:

MIT License

Copyright (c) 2021 Damien Eversmann

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class Dashing.CTATrainWidget extends Dashing.Widget
ready: ->
onData: (data) ->
<h3 class="title">CTA Train Times</h3>
<table class="list-nostyle trains">
<tr><th style="width: 30%;">Line/Dest</th><th style="width:60%;">Station</th><th style="width: 10%;">Arrival</th></tr>
<tr data-id="eta" data-foreach-item="eta">
<td class="rt">
<span data-bind="item.destNm" data-bind-class="item.line"></span>
</td>
<td>
<p class="staNm" data-bind="item.staNm"></p>
<p class="stpDe" data-bind="item.stpDe"></p>
</td>
<td class="est" data-bind="item.est"></td>
</tr>
</table>
<span class="updated-at" data-bind="updatedAtMessage"></span>
<i class="fa fa-subway icon-background" style="margin-top: 50px;"></i>
# encoding: UTF-8
require 'net/http'
require 'uri'
require 'json'
require 'time'
cta_api_key = ENV["CTA_TRAIN_API_KEY"] # https://www.transitchicago.com/developers/traintracker/
train_stop_list = ENV["CTA_TRAIN_STOPS"] # comma separated list
max = 6
SCHEDULER.every '15s', :first_in => 0 do |job|
http = Net::HTTP.new("lapi.transitchicago.com", 80)
response = http.request(Net::HTTP::Get.new("/api/1.0/ttarrivals.aspx?key=#{cta_api_key}&stpid=#{train_stop_list}&outputType=json"))
json = JSON.parse(response.body)
etas = json["ctatt"]["eta"]
etas.each do |eta|
est = (Time.parse(eta["arrT"]) - Time.parse(eta["prdt"])) / 60
eta["est_int"] = est.to_i
eta["est"] = (est >= 2 ? est.to_i.to_s : "DUE")
if (eta["rt"]) == "Red"
eta["line"] = "red-line"
elsif (eta["rt"]) == "Blue"
eta["line"] = "blue-line"
elsif (eta["rt"]) == "Brn"
eta["line"] = "brown-line"
elsif (eta["rt"]) == "G"
eta["line"] = "green-line"
elsif (eta["rt"]) == "Org"
eta["line"] = "orange-line"
elsif (eta["rt"]) == "Pink"
eta["line"] = "pink-line"
elsif (eta["rt"]) == "P"
eta["line"] = "purple-line"
elsif (eta["rt"]) == "Y"
eta["line"] = "yellow-line"
else
eta["line"] = "unknown-line"
end
end
etas.sort_by! { |eta| eta["est_int"] }
send_event('cta_train_widget', eta: etas[0,max])
end
$background-color: #565a5c;
/* using css specificity to override the .widget class which sets to middle */
div.cta_train_widget {
vertical-align: top;
}
.cta_train_widget {
background-color: $background-color;
padding-left: 10px;
padding-right: 10px;
.updated-at {
top: 0px;
right: 2px;
left: unset;
color: rgba(255,255,255,0.5);
}
table.trains {
line-height: 22px;
}
table.trains tr:nth-child(even) {
background-color: rgba(0,0,0,0.3);
}
table.trains td {
border-top: white solid 2px;
}
table.trains td.rt {
text-align: middle;
height: 44px;
}
table.trains td span {
border: white solid 1px;
display: inline-block;
width: 100%;
padding: 2px;
text-transform: uppercase;
font-weight: bold;
}
table.trains td p.staNm {
font-size: 22px;
}
table.trains td p.stpDe {
font-size: 15px;
}
table.trains td.est {
font-size: 30px;
}
.red-line {
background-color: #c60c30;
color: #ffffff;
}
.blue-line{
background-color: #00a1de;
color: #ffffff;
}
.brown-line{
background-color: #62361b;
color: #ffffff;
}
.green-line{
background-color: #009b3a;
color: #ffffff;
}
.orange-line{
background-color: #f9461c;
color: #ffffff;
}
.pink-line{
background-color: #e27ea6;
color: #ffffff;
}
.purple-line{
background-color: #522398;
color: #ffffff;
}
.yellow-line{
background-color: #f9e300;
color: #000000;
}
.unknown-line{
background-color: #cccccc;
color: #000000;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment