Skip to content

Commit 1798e35

Browse files
The entire liveboard structure now uses application/ld+json (JSON-LD-based data) and the API is functional as well!
1 parent 8063e2c commit 1798e35

File tree

14 files changed

+212
-116
lines changed

14 files changed

+212
-116
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ iRail doesn't use a classic API on another URL. There's no 'irail.be/api', inste
2525

2626
You can also ask the application (iRail.be) for other kinds of resources. For example, you can ask for a JSON version of the data that the routeplanner serves.
2727

28-
In order to ask the API to serve you JSON (and not just the plain HTML that you get when you visit irail.be in your browser), send a request to `irail.be/route` but specify the requested content-type in your accept headers, e.g. `application/ld+json` or `application/json`.
28+
In order to ask the API to serve you JSON (and not just the plain HTML that you get when you visit irail.be in your browser), send a request to `irail.be/route` but specify the requested content-type in your accept headers, e.g. `application/ld+json`.
2929

3030
### Hypermedia interface
3131

app/config/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
|
4040
*/
4141

42-
'timezone' => 'UTC',
42+
'timezone' => 'Europe/Brussels',
4343

4444
/*
4545
|--------------------------------------------------------------------------

app/controllers/StationController.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,17 @@ public function liveboard($id){
2222

2323
switch ($val){
2424
case "text/html":
25-
return Response::view('stations.liveboard')->header('Content-Type', "text/html")->header('Vary', 'accept');
25+
$station = \hyperRail\StationString::convertToString($id);
26+
$data = array('station' => $station);
27+
return Response::view('stations.liveboard', $data)->header('Content-Type', "text/html")->header('Vary', 'accept');
2628
break;
2729
case "application/ld+json":
2830
$stationStringName = \hyperRail\StationString::convertToString($id);
2931
$URL = "http://api.irail.be/liveboard/?station=" . $stationStringName->name . "&fast=true&lang=nl&format=json";
3032
$data = file_get_contents($URL);
31-
\hyperRail\iRailFormatConverter::convertLiveboardData($data, $id);
32-
$response = Response::make($data, 200);
33-
$response->header('Content-Type', 'application/ld+json');
34-
$response->header('Vary', 'accept');
35-
break;
36-
case "application/json":
37-
$stationStringName = \hyperRail\StationString::convertToString($id);
38-
$URL = "http://api.irail.be/liveboard/?station=" . $stationStringName->name . "&fast=true&lang=nl&format=json";
39-
$data = file_get_contents($URL);
40-
$response = Response::make($data, 200);
41-
$response->header('Content-Type', 'application/json');
42-
$response->header('Vary', 'accept');
43-
return $response;
33+
$newData = \hyperRail\iRailFormatConverter::convertLiveboardData($data, $id);
34+
$jsonLD = (string)json_encode($newData);
35+
return Response::make($jsonLD, 200)->header('Content-Type', 'application/ld+json')->header('Vary', 'accept');
4436
break;
4537
default:
4638
return Response::view('stations.liveboard')->header('Content-Type', "text/html")->header('Vary', 'accept');

app/controllers/TestController.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
class TestController extends \BaseController {
4+
5+
/**
6+
* Display a listing of the resource.
7+
*
8+
* @return Response
9+
*/
10+
public function index()
11+
{
12+
//
13+
}
14+
15+
/**
16+
* Show the form for creating a new resource.
17+
*
18+
* @return Response
19+
*/
20+
public function create()
21+
{
22+
//
23+
}
24+
25+
/**
26+
* Store a newly created resource in storage.
27+
*
28+
* @return Response
29+
*/
30+
public function store()
31+
{
32+
//
33+
}
34+
35+
/**
36+
* Display the specified resource.
37+
*
38+
* @param int $id
39+
* @return Response
40+
*/
41+
public function show($id)
42+
{
43+
//
44+
}
45+
46+
/**
47+
* Show the form for editing the specified resource.
48+
*
49+
* @param int $id
50+
* @return Response
51+
*/
52+
public function edit($id)
53+
{
54+
//
55+
}
56+
57+
/**
58+
* Update the specified resource in storage.
59+
*
60+
* @param int $id
61+
* @return Response
62+
*/
63+
public function update($id)
64+
{
65+
//
66+
}
67+
68+
/**
69+
* Remove the specified resource from storage.
70+
*
71+
* @param int $id
72+
* @return Response
73+
*/
74+
public function destroy($id)
75+
{
76+
//
77+
}
78+
79+
}

app/hyperRail/Models/HypermediaLiveboardCollection.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

app/hyperRail/Models/HypermediaLiveboardItem.php

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace hyperRail\Models;
4+
use stdClass;
5+
6+
class LiveboardItem {
7+
// DEPARTURE DATA
8+
public $stationURL; // Station URL, to be built from: date + time + md5(trainID + destination)
9+
public $platform; // Platform to take at departure station
10+
public $delay; // Delay before the train departs, in minutes
11+
public $scheduledDepartureTime; // Scheduled departure time
12+
// DESTINATION DATA
13+
public $destinationURL; // The final destination of this train (URL)
14+
public $headsign; // The final destination of this train (headsign, string)
15+
public $routeLabel; // Label assigned to the train
16+
17+
public function fill($stationId, $requestDate, $requestTime, $routeLabel, $headSign,
18+
$delay, $time, $platform){
19+
$md5hash = md5($routeLabel . $headSign);
20+
$this->stationURL = "http://" . _DOMAIN_ . "/NMBS/stations/" . $stationId . "/" . $requestDate . $requestTime . $md5hash;
21+
$this->delay = $delay;
22+
$this->scheduledDepartureTime = $time;
23+
$this->platform = $platform;
24+
$this->destinationURL = "http://" . _DOMAIN_ . "/NMBS/stations/" . $stationId;
25+
$this->headsign = $headSign;
26+
$this->routeLabel = $routeLabel;
27+
}
28+
29+
public function toArray(){
30+
$dataArray = array(
31+
"@id" => $this->stationURL,
32+
"delay" => $this->delay,
33+
"platform" => $this->platform,
34+
"scheduledDepartureTime" => $this->scheduledDepartureTime,
35+
"stop" => $this->destinationURL,
36+
"headsign" => $this->headsign,
37+
"routeLabel" => $this->routeLabel
38+
);
39+
return $dataArray;
40+
}
41+
}
Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,44 @@
11
<?php
22

33
namespace hyperRail;
4+
use hyperRail\Models\LiveboardItem;
45

56
/**
67
* Class iRailFormatConverter
7-
* Converts the old JSON to new JSON.
8+
* Converts the old JSON to objects.
89
* @package hyperRail
910
*/
1011
class iRailFormatConverter {
11-
public static function convertRoutePlanningData($json, $id, $format = "jsonld"){
12-
}
13-
public static function convertLiveboardData($json, $id, $format = "jsonld"){
14-
// First, json_decode the data
12+
/**
13+
* Converts a list of Liveboard items to an array.
14+
* @param $json
15+
* @param $id
16+
* @param string $format
17+
*/
18+
public static function convertLiveboardData($json, $station_id, $format = "jsonld"){
19+
$liveboardCollection = array();
1520
$initialData = json_decode($json);
16-
// $departures = array();
17-
var_dump(json_decode($json));
18-
exit;
21+
// Set globals
22+
// (which data applies to all possible LiveboardItems we are going to create?)
23+
$stationName = $initialData->stationinfo->name;
24+
$timestamp = $initialData->timestamp;
25+
// Convert timestamp to date
26+
$date = date('Ymd', $timestamp);
27+
$time = date('His', $timestamp);
28+
foreach ($initialData->departures->departure as $departure){
29+
$liveboardItem = new LiveboardItem();
30+
$vehicleShort = explode("BE.NMBS.", $departure->vehicle);
31+
$liveboardItem->fill($station_id, $date, $time, $vehicleShort[1], $departure->station, $departure->delay, date('c', $departure->time), $departure->platform);
32+
array_push($liveboardCollection, $liveboardItem->toArray());
33+
}
34+
$context = array(
35+
"delay" => "http://semweb.mmlab.be/ns/rplod/delay",
36+
"platform" => "http://semweb.mmlab.be/ns/rplod/platform",
37+
"scheduledDepartureTime" => "http://semweb.mmlab.be/ns/rplod/scheduledDepartureTime",
38+
"stop" => "http://semweb.mmlab.be/ns/rplod/stop",
39+
"headsign" => "http://vocab.org/transit/terms/headsign",
40+
"routeLabel" => "http://semweb.mmlab.be/ns/rplod/routeLabel",
41+
);
42+
return array("@context" => $context, "@graph" => $liveboardCollection);
1943
}
2044
}

app/lang/en/client.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
return array(
4-
// New translation strings
4+
// START OF NEW TRANSLATION STRINGS
55
"planNewRoute" => "Plan new route",
66
"searchStations" => "Search stations",
77
"typeToStation" => "Type destination station name",
@@ -21,7 +21,6 @@
2121
"routesFoundDescription" => "routes found. The optimal route has been expanded. Tap or click the headers to expand them.",
2222
"reverse" => "Reverse trip",
2323
"planAnother" => "Plan another trip",
24-
//
2524
"errorCheckInput" => "We could not translate your text to a station. Please check your input. We automatically suggest possible stations! :)",
2625
"error" => "Something seems to have gone wrong.",
2726
"errorNoRoutes" => "We could not find any routes.",
@@ -33,20 +32,17 @@
3332
"stationsIdentical" => "The departure and destination station are the same. We can't calculate a route where there is none :)",
3433
"quickFilter" => "Type here to quickly filter liveboard results",
3534
"noResultsFoundLiveboard" => "We're sorry. According to our data, there are no trains departing right now in this station.",
36-
//
3735
"language" => "Language",
3836
"isPartOf" => "iRail is a part of",
39-
//
4037
"stationName" => "Station name",
4138
"stationSearchPlaceholder" => "Type to search for a station",
4239
"viewLiveboard" => "View Liveboard",
43-
//
4440
"platform" => "Platform",
4541
"station" => "Station",
4642
"liveboardDescription" => "Below you'll find a list of all trains departing for a certain station and their possible delay.",
4743
"waitBetween" => "Wait",
4844
"mins" => "minutes before departure",
49-
//
45+
// END OF NEW TRANSLATION STRINGS
5046
"search"=>"Search",
5147
"departure_at" => "Departure",
5248
"arrival_at" => "Arrival",

app/lang/fr/client.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
11
<?php
22

33
return array(
4+
// START OF NEW TRANSLATION STRINGS: PLEASE TRANSLATE
5+
"planNewRoute" => "Plan new route",
6+
"searchStations" => "Search stations",
7+
"typeToStation" => "Type destination station name",
8+
"typeFromStation" => "Type departure station name",
9+
"chooseDate" => "Choose your preferred date",
10+
"chooseTime" => "Pick your preferred time",
11+
"confirmSearch" => "Find trains",
12+
"departureAtHour" => "Departure at chosen hour",
13+
"arrivalAtHour" => "Arrival at chosen hour",
14+
"loadingHeader" => "Loading your results. Sit tight.",
15+
"loadingSub" => "Your results will be available within a few seconds.",
16+
"depart" => "depart",
17+
"arrive" => "arrive",
18+
"on" => "on",
19+
"at" => "at",
20+
"youWantTo" => "You want to",
21+
"routesFoundDescription" => "routes found. The optimal route has been expanded. Tap or click the headers to expand them.",
22+
"reverse" => "Reverse trip",
23+
"planAnother" => "Plan another trip",
24+
"errorCheckInput" => "We could not translate your text to a station. Please check your input. We automatically suggest possible stations! :)",
25+
"error" => "Something seems to have gone wrong.",
26+
"errorNoRoutes" => "We could not find any routes.",
27+
"errorNoLiveboard" => "We could not find any liveboards.",
28+
"errorExplanation" => "This sometimes happens when data is unavailable (e.g. date far in the future). Please try again. If this problem persists,",
29+
"errorReturn" => "Return to the planner",
30+
"errorReturnLiveboard" => "Return to station search",
31+
"errorMail" => "mail us",
32+
"stationsIdentical" => "The departure and destination station are the same. We can't calculate a route where there is none :)",
33+
"quickFilter" => "Type here to quickly filter liveboard results",
34+
"noResultsFoundLiveboard" => "We're sorry. According to our data, there are no trains departing right now in this station.",
35+
"language" => "Language",
36+
"isPartOf" => "iRail is a part of",
37+
"stationName" => "Station name",
38+
"stationSearchPlaceholder" => "Type to search for a station",
39+
"viewLiveboard" => "View Liveboard",
40+
"platform" => "Platform",
41+
"station" => "Station",
42+
"liveboardDescription" => "Below you'll find a list of all trains departing for a certain station and their possible delay.",
43+
"waitBetween" => "Wait",
44+
"mins" => "minutes before departure",
45+
// END OF NEW TRANSLATION STRINGS
446
"search"=>"Chercher",
547
"departure_at" => "Départ",
648
"arrival_at" => "Arrivée",

0 commit comments

Comments
 (0)