Skip to content

Commit

Permalink
Merge pull request #76 from linkedconnections/development
Browse files Browse the repository at this point in the history
v0.8.6
  • Loading branch information
pietercolpaert authored Dec 5, 2018
2 parents 764083f + b03d97b commit 269f03d
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 25 deletions.
14 changes: 10 additions & 4 deletions baseUris-example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"stop" : "http://data.gtfs.org/example/stops/{stop_id}",
"route" : "http://data.gtfs.org/example/routes/{routes.route_id}",
"trip" : "http://data.gtfs.org/example/trips/{trips.trip_id}/{trips.startTime(YYYYMMDDTHHmm)}",
"connection" : "http://example/linkedconnections.org/connections/{trips.startTime(YYYYMMDDTHHmm)}/{connection.departureStop}/{trips.trip_id}"
"stop": "http://data.gtfs.org/example/stops/{stop_id}",
"route": "http://data.gtfs.org/example/routes/{routes.route_short_name}",
"trip": "http://data.gtfs.org/example/trips/{trip_id}/{routes.route_short_name}{trips.startTime}",
"connection": "http://example/linkedconnections.org/connections/{trips.startTime}/{connection.departureStop}/{trips.trip_id}",
"resolve": {
"route_short_id": "connection.trip.route.route_id.substring(0,5)",
"trip_id": "connection.trip.trip_id",
"trip_startTime": "format(connection.trip.startTime, 'YYYYMMDDTHHmm');",
"departureStop": "connection.departureStop"
}
}
9 changes: 5 additions & 4 deletions lib/ConnectionsBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,24 @@ ConnectionsBuilder.prototype.processConnectionRule = function (connectionRule, d
let arrivalTime = addDuration(serviceDay, arrivalDFM);
let startTime = addDuration(serviceDay, this._firstDepartureDFM);
// Set startTime of the trip
trip['startTime'] = startTime;
let tripOfThisConnection = Object.assign({}, trip);
tripOfThisConnection['startTime'] = startTime;

let connection = {
departureTime: departureTime,
arrivalTime: arrivalTime,
arrivalStop: connectionRule['arrival_stop'],
departureStop: connectionRule['departure_stop'],
trip: trip
trip: tripOfThisConnection
};

//The direction or headsign of a vehicle depends on several levels
if (connectionRule['departure_stop_headsign'])
connection['headsign'] = connectionRule['departure_stop_headsign'];
else if (trip['trip_headsign'])
connection['headsign'] = trip['trip_headsign'];
connection['headsign'] = tripOfThisConnection['trip_headsign'];
else if (trip.route['route_long_name'])
connection['headsign'] = trip.route['route_long_name'];
connection['headsign'] = tripOfThisConnection.route['route_long_name'];

if (connectionRule['arrival_stop_headsign'])
connection['previous_headsign'] = connectionRule['arrival_stop_headsign'];
Expand Down
43 changes: 26 additions & 17 deletions lib/URIStrategy.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/**
* Pieter Colpaert © Ghent University - iMinds
* Pieter Colpaert © Ghent University - iMinds
* Combines connection rules, trips and services to an unsorted stream of connections
*/

const { format } = require('date-fns');
const uri_templates = require('uri-templates');

var URIStrategy = function (baseUris) {
var URIStrategy = function(baseUris) {
var defaultBaseUris = {
stop: 'http://example.org/stops/{stop_id}',
route: 'http://example.org/routes/{routes.route_id}',
trip: 'http://example.org/trips/{trips.trip_id}/{trips.startTime(YYYYMMDD)}',
connection: 'http://example.org/connections/{trips.startTime(YYYYMMDD)}/{connection.departureStop}/{trips.trip_id}'
trip:
'http://example.org/trips/{trips.trip_id}/{trips.startTime(YYYYMMDD)}',
connection:
'http://example.org/connections/{trips.startTime(YYYYMMDD)}/{connection.departureStop}/{trips.trip_id}',
};
if (!baseUris) {
baseUris = defaultBaseUris;
Expand All @@ -34,40 +36,47 @@ var URIStrategy = function (baseUris) {
this._routeTemplate = uri_templates(baseUris.route);
this._tripTemplate = uri_templates(baseUris.trip);
this._connectionTemplate = uri_templates(baseUris.connection);

this._resolve = baseUris.resolve || {};
};

/**
* Returns a persistent identifier for a connection
*/
URIStrategy.prototype.getId = function (connection) {
return resolveURI(this._connectionTemplate, connection);
URIStrategy.prototype.getId = function(connection) {
return resolveURI(this._connectionTemplate, connection, this._resolve);
};

URIStrategy.prototype.getStopId = function (id) {
URIStrategy.prototype.getStopId = function(id) {
return this._stopTemplate.fill({ [this._stopTemplate.varNames[0]]: id });
};

URIStrategy.prototype.getTripId = function (connection) {
return resolveURI(this._tripTemplate, connection);
}
URIStrategy.prototype.getTripId = function(connection) {
return resolveURI(this._tripTemplate, connection, this._resolve);
};

URIStrategy.prototype.getRouteId = function (connection) {
return resolveURI(this._routeTemplate, connection);
}
URIStrategy.prototype.getRouteId = function(connection) {
return resolveURI(this._routeTemplate, connection, this._resolve);
};

function resolveURI(template, connection) {
function resolveURI(template, connection, resolve) {
let varNames = template.varNames;
let fillerObj = {};

for (let i in varNames) {
fillerObj[varNames[i]] = resolveValue(varNames[i], connection);
fillerObj[varNames[i]] = resolveValue(varNames[i], connection, resolve);
}

return template.fill(fillerObj);
}

function resolveValue(param, connection) {
function resolveValue(param, connection, resolve) {
// try first to resolve using keys in 'resolve' object
if (resolve[param]) {
return eval(resolve[param]);
}

// otherwise, keep behaviour for backward compatibility

// GTFS source file and attribute name
let source = param.split('.')[0];
let attr = param.split('.')[1];
Expand Down
93 changes: 93 additions & 0 deletions test/testURIStrategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const assert = require('assert');
const URIStrategy = require('../lib/URIStrategy');

describe('URIStrategy', () => {
describe('getRouteId', () => {
it('should replace {routes.route_id} by connection.trip.route.route_id', () => {
const strategy = new URIStrategy({
route: 'http://example.org/routes/{routes.route_id}',
});

const connection = {
trip: {
route: {
route_id: 'B1234-56789',
},
},
};

assert.equal(
strategy.getRouteId(connection),
'http://example.org/routes/B1234-56789'
);
});

it('should replace spaces by %20', () => {
const strategy = new URIStrategy({
route: 'http://example.org/routes/{routes.route_id}',
});

const connection = {
trip: {
route: {
route_id: 'a b c',
},
},
};

assert.equal(
strategy.getRouteId(connection),
'http://example.org/routes/a%20b%20c'
);
});

it('should resolve expression by evaluating matching key in resolve object', () => {
const strategy = new URIStrategy({
route: 'http://example.org/routes/{route_short_id}',
resolve: {
route_short_id: 'connection.trip.route.route_id.substring(0,5)',
},
});

const connection = {
trip: {
route: {
route_id: 'B1234-56789',
},
},
};

assert.equal(
strategy.getRouteId(connection),
'http://example.org/routes/B1234'
);
});
});

describe('getId', () => {
it('should resolve expression using date-fns.format function', () => {
const strategy = new URIStrategy({
connection:
'http://example.org/connections/{trip_startTime}/{departureStop}/{trip_id}',
resolve: {
trip_id: 'connection.trip.trip_id',
trip_startTime: "format(connection.trip.startTime, 'YYYYMMDDTHHmm');",
departureStop: 'connection.departureStop',
},
});

const connection = {
departureStop: '1234',
trip: {
trip_id: '5678',
startTime: new Date('2018-09-21T10:25:12'),
},
};

assert.equal(
strategy.getId(connection),
'http://example.org/connections/20180921T1025/1234/5678'
);
});
});
});

0 comments on commit 269f03d

Please sign in to comment.