-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtflDataGetter.js
More file actions
134 lines (119 loc) · 3.43 KB
/
Copy pathtflDataGetter.js
File metadata and controls
134 lines (119 loc) · 3.43 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var env = require('env2')('./config.env');
var https = require('https');
var querystring = require('querystring');
function handleArrivalDataRequests(callback) {
makeArrivalDataRequests(callback);
}
function sendResponse(arrivalDataResponse, arrivalData) {
var processedData = processData(arrivalData);
arrivalDataResponse.writeHead(200, {'Content-Type': 'text/plain'});
arrivalDataResponse.end(processedData);
}
function processData(rawArrivalData) {
var allStationArrivalInfo, clockwiseData;
allStationArrivalInfo = rawArrivalData.map(function(stationArrivalInfo) {
return {
stationNumber: getStationNumber(stationArrivalInfo.stationName),
direction: getDirectionFrom(stationArrivalInfo.towards),
time: stationArrivalInfo.timeToStation
};
});
allStationArrivalInfo = removeUnfoundStations(allStationArrivalInfo);
clockwiseData = filterByDirection('clockwise', allStationArrivalInfo);
return JSON.stringify(createArrivalOutputData(clockwiseData));
}
function makeArrivalDataRequests(callback) {
var options = createArrivalDataRequestOptions();
var apiKey = process.env.app_key;
var apiId = process.env.app_id;
var postData = querystring.stringify({
app_key: apiKey,
app_id: apiId
});
var body = '';
var request = https.request(options, function(response) {
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function(){
callback(processData(JSON.parse(body)));
});
});
request.end(postData);
}
function createArrivalDataRequestOptions(){
return {
hostname : 'api.tfl.gov.uk',
path : '/Line/Circle/arrivals',
method : 'GET'
};
}
function createArrivalOutputData(arrivalData) {
var out = stopOrder.map(function(stopName, stopIndex) {
return getShortestTimeForStationNumber(stopIndex, arrivalData);
});
return out;
}
function getShortestTimeForStationNumber(targetStationNumber, allStationArrivalInfo) {
var maxTime = 600;
var minTime = allStationArrivalInfo.reduce(function(minTime, stationArrivalInfo) {
if (stationArrivalInfo.stationNumber === targetStationNumber) {
var arrivalTime = stationArrivalInfo.time;
return minTime > arrivalTime ? arrivalTime : minTime;
} else {
return minTime;
}
}, maxTime);
return minTime/maxTime;
}
function removeUnfoundStations(allStationArrivalInfo) {
return allStationArrivalInfo.filter(function(stationArrivalInfo) {
return stationArrivalInfo.stationNumber !== -1;
});
}
var stopOrder = [
'Edgware',
'Baker',
'Great',
'Euston',
'King\'s',
'Farringdon',
'Barbican',
'Moorgate',
'Liverpool',
'Aldgate',
'Tower',
'Monument',
'Cannon',
'Mansion',
'Blackfriars',
'Temple',
'Embankment',
'Westminster',
'St.',
'Victoria',
'Sloane',
'South',
'Gloucester',
'High',
'Notting',
'Bayswater',
'Paddington'
];
function getStationNumber(stationName) {
var stationNameFirstWord = stationName.split(' ')[0];
var index = stopOrder.indexOf(stationNameFirstWord);
if (index === -1) console.log(stationName);
return index;
}
function filterByDirection(direction, allStationArrivalInfo) {
return allStationArrivalInfo.filter(function(stationArrivalInfo) {
return stationArrivalInfo.direction === direction;
});
}
function getDirectionFrom(towards) {
return towards === 'Edgware Road (Circle)' ? 'clockwise' : 'anti-clockwise';
}
module.exports = {
handleArrivalDataRequests: handleArrivalDataRequests
};