-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (38 loc) · 1.21 KB
/
index.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
42
43
44
45
46
var request = require('request');
var railStations = (function() {
var base = 'http://ojp.nationalrail.co.uk/find/stationsDLRLU/';
function lookupCode(name, callback) {
var url = base + encodeURIComponent(name);
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var stations = transposeResults(body);
fireCallback(callback, false, stations);
} else {
fireCallback(callback, error);
}
});
}
function transposeResults(response) {
var results = JSON.parse(response);
return results.map(function(result) {
return {
'code': result[0],
'name': result[1],
'longitude': result[8],
'latitude': result[7],
'postcode': result[9],
'operator': result[10],
'raw': result
}
});
}
function fireCallback(callback, error, results) {
if (typeof callback === 'function') {
callback(error, results);
}
}
return {
search: lookupCode
}
})();
module.exports = railStations;