-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrouter.js
142 lines (108 loc) · 4.17 KB
/
router.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
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
135
136
137
138
139
140
141
142
'use strict'
let router = require('koa-router')();
let globalKeys = require('./keys.js');
let Q = require('Q');
let https = require('https');
let http = require('http');
function getJsonBody(callback) {
return function(response) {
let body = '';
response.on('data', function(data) {
body += data;
});
response.on('end', function() {
let payload = JSON.parse(body);
callback(payload);
});
}
}
// Walk Score api *requires* a latitude and longitude. We'll use Google's Geocoding
// Api (https://developers.google.com/maps/documentation/geocoding/intro).
function getLocationFromGoogle(address) {
let deferred = Q.defer();
let url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address + '&key=' + globalKeys.googleApiKey;
https.get(url, getJsonBody(function(payload) {
let location = payload.results[0].geometry.location;
deferred.resolve(location);
}));
return deferred.promise;
}
function getWalkScore(address, location) {
let deferred = Q.defer();
let url = 'http://api.walkscore.com/score?format=json&address=' + address + '&lat=' +
location.lat + '&lon=' + location.lng + '&wsapikey=' + globalKeys.walkScoreApiKey;
http.get(url, deferred.resolve);
return deferred.promise;
}
function getStops(location) {
let deferred = Q.defer();
let url = 'http://transit.walkscore.com/transit/search/stops/?format=json&lat=' +
location.lat + '&lon=' + location.lng + '&wsapikey=' + globalKeys.walkScoreApiKey;
http.get(url, deferred.resolve);
return deferred.promise;
}
function getStop(id) {
let deferred = Q.defer();
let url = 'http://transit.walkscore.com/transit/stop/' + id + '?format=json&wsapikey=' + globalKeys.walkScoreApiKey;
http.get(url, deferred.resolve);
return deferred.promise;
}
function getRoute(id) {
let deferred = Q.defer();
let url = 'http://transit.walkscore.com/transit/route/' + id + '?format=json&wsapikey=' + globalKeys.walkScoreApiKey;
http.get(url, deferred.resolve);
return deferred.promise;
}
function getTravelTimePolygon(origin) {
let deferred = Q.defer();
let url = 'http://api2.walkscore.com/api/v1/traveltime_polygon/json?wsapikey=' + globalKeys.walkScoreApiKey
+ '&origin=' + origin.lat + '%2C' + origin.lng + '&mode=bike&time=20';
http.get(url, deferred.resolve);
return deferred.promise;
}
// Example usage:
// curl localhost:3001/api/walk/2025%201st%20Avenue%20Suite%20500,%20Seattle,%20WA%2098121 | json-prettify | less
router.get('/api/walk/:address', function *(next) {
// For example, an address might be '2025 1st Avenue Suite 500, Seattle, WA 98121'
let address = this.params.address;
let location = yield getLocationFromGoogle(address);
let walkScore = yield getWalkScore(address, location);
this.body = walkScore;
this.set({ "Content-Type": "application/json" });
});
// Example usage:
// curl localhost:3001/api/stops/2025%201st%20Avenue%20Suite%20500,%20Seattle,%20WA%2098121 > stops.out
router.get('/api/stops/:address', function *(next) {
let address = this.params.address;
let location = yield getLocationFromGoogle(address);
let stops = yield getStops(location);
this.body = stops;
this.set({ "Content-Type": "application/json" });
});
// Example usage:
// curl localhost:3001/api/route/6fffc9e7af7a9a444b12fd2ae2685281ab42bc1e
router.get('/api/route/:id', function *(next) {
let id = this.params.id;
let route = yield getRoute(id);
this.body = route;
this.set({ "Content-Type": "application/json" });
});
// Example usage:
// curl localhost:3001/api/stop/fb1522a4c2ba6265619b2e7054a9f4c74ea11479
router.get('/api/stop/:id', function *(next) {
let id = this.params.id;
let stop = yield getStop(id);
this.body = stop;
this.set({ "Content-Type": "application/json" });
});
// Example usage:
// curl localhost:3001/api/travel/2025%201st%20Avenue%20Suite%20500,%20Seattle,%20WA%2098121
router.get('/api/travel/:address', function *(next) {
let address = this.params.address;
let origin = yield getLocationFromGoogle(address);
let polygon = yield getTravelTimePolygon(origin);
this.body = polygon;
this.set({ "Content-Type": "application/json" });
})
router.redirect('/', 'index.html');
module.exports = router;