Skip to content

Instantly share code, notes, and snippets.

@madebyaleks
Created February 2, 2019 20:29
Show Gist options
  • Save madebyaleks/19609e277d140734837830640561e975 to your computer and use it in GitHub Desktop.
Save madebyaleks/19609e277d140734837830640561e975 to your computer and use it in GitHub Desktop.
Entur (Norwegian public transport) for Smashing/Dashing Dashboards.
require "net/http"
require "graphql/client"
require "graphql/client/http"
module ETAPI
api_endpoint = "https://api.entur.org/journeyplanner/2.0/index/graphql"
# stopPlace_id = "NSR:StopPlace:58366" # Jernbanetorget
# stopPlace_id = "NSR:StopPlace:58404" # Nationaltheatret
# Configure GraphQL endpoint using the basic HTTP network adapter.
HTTP = GraphQL::Client::HTTP.new("https://api.entur.org/journeyplanner/2.0/index/graphql") do
def headers(context)
# Optionally set any HTTP headers
{ "ET-Client-Name" => "Smashing-Dashboard"} #Set this to something understandable (Required by Entur API)
end
end
Schema = GraphQL::Client.load_schema(HTTP)
Client = GraphQL::Client.new(schema: Schema, execute: HTTP)
module Fetch
Query = ETAPI::Client.parse <<~'GRAPHQL'
{
stopPlace(id:"NSR:StopPlace:58366") { # Currently set to Jernbanetorget
name
estimatedCalls(numberOfDepartures: 4) {
destinationDisplay {
frontText
}
expectedDepartureTime
situations {
summary {
value
language
}
}
serviceJourney {
journeyPattern {
line {
publicCode
}
}
}
}
}
}
GRAPHQL
end
SCHEDULER.every '1m', :first_in => 0, allow_overlapping: false do |job|
response = ETAPI::Client.query(Fetch::Query)
if response.errors.any?
raise QueryError.new(response.errors[:data].join(", "))
else
response = response.data.stop_place
end
avvik_array = []
line_hash = []
response.estimated_calls.each do |i|
case Time.parse(i.expected_departure_time).to_i
when Time.now.to_i...Time.now.to_i+45
human_time = "Nå"
when Time.now.to_i+45...Time.now.to_i+90
human_time = "1 min"
when Time.now.to_i+90...Time.now.to_i+120
human_time = "2 min"
when Time.now.to_i+120...Time.now.to_i+180
human_time = "3 min"
when Time.now.to_i+180...Time.now.to_i+240
human_time = "4 min"
when Time.now.to_i+240...Time.now.to_i+300
human_time = "5 min"
when Time.now.to_i+300...Time.now.to_i+360
human_time = "6 min"
when Time.now.to_i+360...Time.now.to_i+420
human_time = "7 min"
when Time.now.to_i+480...Time.now.to_i+540
human_time = "8 min"
when Time.now.to_i+540...Time.now.to_i+600
human_time = "9 min"
when Time.now.to_i+600...Time.now.to_i+660
human_time = "10 min"
else
human_time = DateTime.parse("#{i.expected_departure_time}").strftime("%H:%M")
end
line_hash << {line_type: "#{i.service_journey.journey_pattern.line.transport_mode}", line_num: "#{i.service_journey.journey_pattern.line.public_code}", line_dest: "#{i.destination_display.front_text}", line_departure: "#{human_time}" }
i.situations.each do |o|
o.summary.each do |t|
incident = t.value
if incident != nil
avvik_array.push("Linje #{i.service_journey.journey_pattern.line.public_code}: #{incident}")
end
end
end
end
send_event('et_ticker', { stop_place: "#{response.name}", soon: line_hash, ticker: avvik_array})
end
end
<h1 class="title"><span data-bind="stop_place"></span></h1>
<div class="big_departure">
<table>
<tr class="trip" data-foreach-item="soon">
<td class="line_num"> <span data-bind="item.line_num"> </span></td>
<td class="line_dest"> <span data-bind="item.line_dest"> </span></td>
<td class="line_departure"> <span data-bind="item.line_departure"> </span></td>
</tr>
</table>
</div>
<div class="avvik">
<marquee class="ticker-trip">
<span data-foreach-item="ticker">
<span data-bind="item"></span>.&nbsp;
</span>
</marquee>
</div>
<p class="updated-at"> <span data-bind="updatedAtMessage"></span></p>
$ruter-gra-40: rgb(38,50,56);
$ruter-gra-30: rgb(46,51,58);
$ruter-gra-10: rgb(36,40,45);
$ruter-gra-lys: rgb(170,170,180);
$ruter-gul: rgb(251,192,45);
$bright: rgb(250, 250, 250);
$muted: rgba(255, 255, 255, 0.3);
// ----------------------------------------------------------------------------
// Widget-list styles
// ----------------------------------------------------------------------------
.widget-entur-ticker {
background-color: $ruter-gra-30;
padding: 0px 20px;
color: $bright;
.title {
color: $ruter-gra-lys;
font-size: 30px;
}
.big_departure {
font-size: 1.6em;
text-align: left;
}
tr.trip {
height: 1.3em;
}
td.line_type {
width: 73px;
}
td.line_num {
width: 55px;
}
td.line_dest {
width: 400px;;
}
td.line_departure {
text-align: right;
}
.ticker {
color: $ruter-gul;
}
//-------------------------------//
// Fargekodede transportmidler //
//-------------------------------//
.bane {
color: rgb(236,112,12);
}
.bybuss {
// color: rgb(230,0,0);
color: rgb( 235, 51, 51 );
}
.regionbuss {
color: rgb(118,163,0);
}
.trikk {
color: rgb(1,145,239);
}
.bat {
color: rgb(104,44,136);
}
.tog {
color: rgb(0,48,235);
}
.ufo {
color: rgb(144,239,93);
}
.avvik {
margin-top: 15px;
font-size: 1.3em;
opacity: 0.5;
color: $ruter-gul;
font-size: 0.8em;
}
.updated-at {
color: $muted;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment