Skip to content

Commit

Permalink
Added mapping to routes
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesdemey committed Dec 15, 2014
1 parent 2ea3fee commit a371e18
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 3 deletions.
161 changes: 159 additions & 2 deletions maps/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,163 @@

'use strict';

exports.route = function(data) {
return data;
var _ = require('lodash-node'),
async = require('async'),
datetime = require('../utils/datetime'),
stations = require('../db/stations');

exports.route = function(directions, locale, callback) {

locale = locale || null;

var routes = _.map(directions.Routes, function(route) {

return {

'arrival' : route.ArrivalDateTime
? {
'time' : datetime.toCET(route.ArrivalDateTime).toISOString(),
'relative' : datetime.toCET(route.ArrivalDateTime).locale(locale).fromNow(),
'delay' : route.ArrivalDelay,
'arrived' : route.ArrivalDetected,
}
: undefined,

'departure' : route.DepartureDateTime
? {
'time' : datetime.toCET(route.DepartureDateTime).toISOString(),
'relative' : datetime.toCET(route.DepartureDateTime).locale(locale).fromNow(),
'delay' : route.DepartureDelay,
'departed' : route.DepartureDetected,
}
: undefined,

'connections' : route.Connections,
'travelTime' : route.TravelTime,

'transports' : _.map(route.Transports, function(transport) {
return {

'arrival' : transport.ArrivalDateTime
? {
'station' : {
'id': transport.ArrivalStationId
},
'time' : datetime.toCET(transport.ArrivalDateTime).toISOString(),
'relative' : datetime.toCET(transport.ArrivalDateTime).locale(locale).fromNow(),
'track' : transport.ArrivalTrack,
'delay' : transport.ArrivalDelay,
'arrived' : transport.ArrivalDetected,
}
: undefined,

'departure' : transport.DepartureDateTime
? {
'station' : {
'id': transport.DepartureStationId
},
'time' : datetime.toCET(transport.DepartureDateTime).toISOString(),
'relative' : datetime.toCET(transport.DepartureDateTime).locale(locale).fromNow(),
'track' : transport.DepartureTrack,
'delay' : transport.DepartureDelay,
'departed' : transport.DepartureDetected,
}
: undefined,

};

}),

'modified' : {
'route' : route.RouteModified
}
};
});

async.parallel({

departure: function(callback) {
stations.byId(directions.DepartureStationId, function(err, station) {
if (err)
callback(err);
else
callback(null, station);
});
},

arrival: function(callback) {
stations.byId(directions.ArrivalStationId, function(err, station) {
if (err)
callback(err);
else
callback(null, station);
});
},

transports: function(callback) {

async.each(routes, function(route, callback) {
async.each(route.transports, function(transport, callback) {

async.parallel({

arrival: function(callback) {
stations.byId(transport.arrival.station.id, function(err, station) {
if (err)
callback(err);
else
callback(null, station);
});
},

departure: function(callback) {
stations.byId(transport.departure.station.id, function(err, station) {
if (err)
callback(err);
else
callback(null, station);
});
},

}, function(err, results) {

transport.arrival.station = results.arrival;
transport.departure.station = results.departure;

if (err)
callback(err);
else
callback(null, transport);
});

}, function(err, transports) {
if (err)
callback(err);
else
callback(null);
});

}, function(err) {
if (err)
callback(err);
else
callback(null);
});

}

}, function(err, results) {

if (err)
callback(err);
else
callback(null, {
'departure' : results.departure,
'arrival' : results.arrival,

'route' : routes
});

});

};
9 changes: 8 additions & 1 deletion routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ router.get('/', function(req, res, next) {
return res.json(500, new ex.RailtimeException('Could not resolve route'));
}

res.json(maps.routes.route(body));
maps.routes.route(body, req.locale, function(err, directions) {
if (err)
res.send(resp.statusCode || 500, { error: err });
else
res.json(directions);
});


});

});
Expand Down

0 comments on commit a371e18

Please sign in to comment.