-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusHelper.js
41 lines (34 loc) · 1017 Bytes
/
busHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const moment = require('moment');
const rp = require('request-promise-native');
function getArrivalTime(obj)
{
const unixTime = moment(obj.expectedArrival,"YYYY-MM-DDTHH:mm:ssZ",'en').valueOf();
return unixTime;
}
function sortByDistance(stopPoints)
{
return stopPoints.sort(function(a,b)
{
return a.distance - b.distance;
})
}
function sortByArrivalTime(busDataArray)
{
return busDataArray.sort(function(a,b)
{
return getArrivalTime(a) - getArrivalTime(b)
})
}
function makeStopDetailsRequestString(stopId)
{
return 'https://api.tfl.gov.uk/StopPoint/' + stopId
+ '/Arrivals?app_id=72ae2528&app_key=e51178ced390783e1bc24c32fe85b8d1';
}
function getBusData(stopId)
{
return rp(makeStopDetailsRequestString(stopId))
.then(JSON.parse)
.then(busDataArray => sortByArrivalTime(busDataArray))
.then(busDataArray => busDataArray.slice(0,5))
}
module.exports = {sortByDistance, getBusData};