forked from UKHomeOffice/esapi-policeuk-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
421 lines (400 loc) · 19.4 KB
/
Copy pathindex.js
File metadata and controls
421 lines (400 loc) · 19.4 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
var falcor = require('falcor');
var falcorExpress = require('falcor-express');
var Model = falcor.Model;
var $error = Model.error;
var Router = require('falcor-router');
var request = require('request-promise');
var express = require('express');
var _ = require('lodash');
var app = express();
// Have Express request index.html
app.use(express.static('.'));
var $ref = falcor.Model.ref;
var pprint = function(o) { return JSON.stringify(o, null, 2);};
// Same data that was used in the view for our
// events, but this time on a simple object
// and not a Falcor model.
var eventsData = {
locationsById: {
1: {
city: "Salt Lake City",
state: "Utah"
},
2: {
city: "Las Vegas",
state: "Nevada"
},
3: {
city: "Minneapolis",
state: "Minnesota"
},
4: {
city: "Walker Creek Ranch",
state: "California"
}
},
events: [
{
name: "ng-conf",
description: "The world's best Angular Conference",
location: $ref('locationsById[1]')
},
{
name: "React Rally",
description: "Conference focusing on Facebook's React",
location: $ref('locationsById[1]')
},
{
name: "ng-Vegas",
description: "Two days jam-packed with Angular goodness with a focus on Angular 2",
location: $ref('locationsById[2]')
},
{
name: "Midwest JS",
description: "Midwest JS is a premier technology conference focused on the JavaScript ecosystem.",
location: $ref('locationsById[3]')
},
{
name: "NodeConf",
description: "NodeConf is the longest running community driven conference for the Node community.",
location: $ref('locationsById[4]')
}
]
}
// We setup a model.json endpoint and pass it a dataSourceRoute which
// allows us to serve a router. Various route requests can be sent to the
// router to request whatever data is required
app.use('/model.json', falcorExpress.dataSourceRoute(function(req, res) {
return new Router([
{
route: "forces[{integers:indices}]",
get: function(pathset) {
return request({uri: "http://data.police.uk/api/forces", json: true}).then(function(resp) {
return pathset.indices.map(function(index){
var id = resp[index].id;
var r = {
path: ['forces', index],
value: resp[index] ? $ref(["forcesById", id]): $error("Unknown force index" + index)
};
return r;
});
});
}
},
{
route: "forcesById[{keys:ids}]['id', 'name']",
get: function(pathset) {
var attributes = pathset[2];
return request({uri: "http://data.police.uk/api/forces", json: true}).then(function(resp) {
var forcesById = _.keyBy(resp, 'id');
var resultCollection = pathset.ids.map(function(id){
return attributes.map(function(attribute) {
var myid = id;
return {
path: ['forcesById', myid, attribute],
value: forcesById[myid] ? forcesById[myid][attribute] : null //$error("Unknown force id: " + myid)
};
});
});
return _.flatten(resultCollection);
});
}
},
{
route: "forcesById[{keys:ids}]['description']",
get: function(pathset) {
console.log("forcesById[{keys:ids}]['description']");
var attributes = [pathset[2]];
console.log("attributes requested: ", attributes);
var resultPromiseCollection = pathset.ids.map(function(id){
return request({uri: "http://data.police.uk/api/forces/" + id, json: true}).then(function(resp) {
console.log("forcesById detail:" + id, resp);
return attributes.map(function(attribute) {
return {
path: ['forcesById', id, attribute],
value: resp[attribute]
};});
});
});
return Promise.all(resultPromiseCollection).then(function(resultCollection) {
console.log("description return is: ", JSON.stringify(resultCollection, null, 2));
return _.flatten(resultCollection);
});
}
},
{
route: "forcesById[{keys:ids}]['engagement_methods']",
get: function(pathset){
console.log("forcesById[{keys:ids}]['engagement_methods']");
var r = pathset.ids.map(function(id) {
return {
path: ['forcesById', id, 'engagement_methods'],
value: $ref(['engagementMethods', id])
};
});
console.log("forcesById[{keys:ids}]['engagement_methods'] will ret" + JSON.stringify(r, null, 2));
return r;
}
},
{
route: "forcesById[{keys:ids}]['neighbourhoods']",
get: function(pathset){
console.log("forcesById[{keys:ids}]['neighbourhoods']", pathset);
var r = pathset.ids.map(function(id) {
return {
path: ['forcesById', id, 'neighbourhoods'],
value: $ref(['neighbourhoodByForceId', id])
};
});
console.log("forcesById[{keys:ids}]['neighbourhoods'] will ret" + JSON.stringify(r, null, 2));
return r;
}
},
{
route: "engagementMethods[{keys:forceIds}][{integers:idx}]['url', 'type', 'description', 'title']",
get: function(pathset) {
console.log("engagementMethods[{keys:forceIds}]['url', 'type', 'description', 'title']");
var attributes = pathset[2];
console.log("attributes requested: ", attributes);
var resultPromiseCollection = pathset.forceIds.map(function(id){
console.log("requesting engmethods: " + id);
return request({uri: "http://data.police.uk/api/forces/" + id, json: true}).then(function(resp) {
console.log("engagementMethods response detail:" + id, resp);
var eng_methods = resp['engagement_methods'];
return pathset.idx.map(function(methodIdx) {
return attributes.map(function(attribute) {
var eng_method = eng_methods[methodIdx];
console.log("bobo", eng_method);
return {
path: ['engagementMethods', id, methodIdx, attribute],
value: eng_method[attribute]
};});
});
});
});
return Promise.all(resultPromiseCollection).then(function(resultCollection) {
var flatResultCollection = _.flatten(resultCollection);
console.log("engagement methods return is: ", JSON.stringify(flatResultCollection, null, 2));
return flatResultCollection;
});
}
},
{
route: "neighbourhoodByForceId[{keys:forceIds}][{integers:neighbourhoodIndices}]",
get: function(pathset) {
console.log("req: neighbourhoodByForceId[{keys:forceIds}][{integers:neighbourhoodIndices}]", pathset);
var requestPromises = pathset.forceIds.map(function(forceId) {
return pathset.neighbourhoodIndices.map(function(nIdx) {
var result = request({uri: "http://data.police.uk/api/" + forceId + "/neighbourhoods", json: true}).then(function(resp){
console.log("resp:neighbourhoodByForceId[{keys:forceIds}][{integers:neighbourhoodIndices}]", forceId,
nIdx, JSON.stringify(resp, null, 2));
return {
path: ["neighbourhoodByForceId", forceId, nIdx],
value: resp[nIdx] ? $ref(['neighbourhoodByForceIdAndCode', forceId, resp[nIdx].id]) : null //$error("No neighbourhood for idx: " + nIdx)
};
});
return result;
});
});
var flatRequestPromises = _.flatten(requestPromises);
return Promise.all(flatRequestPromises).then(function(allResps) {
console.log("Neighborhood all", JSON.stringify(allResps, null, 2)) ;
return allResps;
});
}
},
{
route: "neighbourhoodByForceIdAndCode[{keys:forceIds}][{keys:nCodes}]['location']",
get: function(pathset) {
var rCollection = pathset.forceIds.map(function(forceId) {
return pathset.nCodes.map(function(nCode) {
return {
path: ["neighbourhoodByForceIdAndCode", forceId, nCode, 'location'],
value: $ref(['locationsByForceIdAndCode', forceId, nCode ])
};
});
});
return _.flatten(rCollection);
}
},
{
route: "neighbourhoodByForceIdAndCode[{keys:forceIds}][{keys:nCodes}]['people']",
get: function(pathset) {
var rCollection = pathset.forceIds.map(function(forceId) {
return pathset.nCodes.map(function(nCode) {
return {
path: ["neighbourhoodByForceIdAndCode", forceId, nCode, 'people'],
value: $ref(['peopleByForceIdAndNeighbourhoodCode', forceId, nCode ])
};
});
});
return _.flatten(rCollection);
}
},
{
route: "peopleByForceIdAndNeighbourhoodCode[{keys:forceIds}][{keys:nCodes}][{integers:personIndices}]['bio', 'rank', 'name']",
get: function(pathset) {
var attributes = pathset[4];
console.log("\n\npathset: ", pprint(pathset));
console.log("attributes", pprint(attributes));
var requestPromises = pathset.forceIds.map(function(forceId) {
return pathset.nCodes.map(function(nCode) {
var result = request({uri: "http://data.police.uk/api/" + forceId + "/" + nCode + "/people", json: true}).then(function(resp){
var peopleAttributePaths = pathset.personIndices.map(function(idx) {
console.log("respTo" + pathset[0], forceId, nCode, pprint(pathset), pprint(resp));
return attributes.map(function(attribute) {
console.log("evaluating " + idx + " " + attribute, resp);
var attrValue = resp[idx] && resp[idx][attribute] ? resp[idx][attribute] :null; // $error("No person." + attribute + " for " + pprint(pathset) + "for idx: " + idx);
console.log("will set", attrValue);
var individualAttr = {
path: ["peopleByForceIdAndNeighbourhoodCode", forceId, nCode, idx, attribute],
value: attrValue
};
console.log(pathset[0], "individualAttr", individualAttr);
return individualAttr;
});
});
return _.flatten(peopleAttributePaths);
});
return result;
});
});
var flatRequestPromises = _.flatten(requestPromises);
return Promise.all(flatRequestPromises).then(function(allResps) {
var flattened = _.flatten(allResps);
console.log("peopleByForceIdAndNeighbourhoodCode allResps: ", pprint(flattened)) ;
return flattened;
});
}
},
{
route: "neighbourhoodByForceIdAndCode[{keys:forceIds}][{keys:nCodes}]['url_force','name','population']",
get: function(pathset) {
var requestPromises = pathset.forceIds.map(function(forceId) {
return pathset.nCodes.map(function(nCode) {
var result = request({uri: "http://data.police.uk/api/" + forceId + "/" + nCode, json: true}).then(function(resp){
console.log("neighbourhoods by ForceId and code", pathset, resp);
var respByCode = _.keyBy('id');
return {
path: ["neighbourhoodByForceIdAndCode", forceId, nCode],
value: resp[nIdx] ? $ref(['neighbourhoodByForceIdAndCode', forceId, resp[nIdx].id]) : $error("No neighbourhood for idx: " + nIdx)
};
});
return result;
});
});
var flatRequestPromises = _.flatten(requestPromises);
return Promise.all(flatRequestPromises).then(function(allResps) {
console.log("Neighborhood all", allResps) ;
return allResps;
});
}
},
{
route: "locationsByForceIdAndCode[{keys:forceIds}][{keys:nCodes}][{integers:indices}]['name', 'longitude', 'postcode', 'address', 'latitude', 'type', 'description']",
get: function(pathset){
var attributes = pathset[4];
console.log("locAttrs", attributes);
var requestPromises = pathset.forceIds.map(function(forceId) {
return pathset.nCodes.map(function(nCode) {
var result = request({uri: "http://data.police.uk/api/" + forceId + "/" + nCode, json: true}).then(function(resp){
console.log("locationsByForceIdAndCode pathset:", pathset,"response:", resp, attributes);
var individualAttrs = pathset.indices.map(function(idx) {
return attributes.map(function(attribute) {
return {
path: ["locationsByForceIdAndCode", forceId, nCode, idx, attribute],
value: resp['locations'][idx][attribute]
};
});
});
var flattenedAttrs = _.flatten(individualAttrs);
console.log('flattenedAttrs', flattenedAttrs);
return flattenedAttrs;
});
console.log("tfw", result);
return result;
});
});
console.log("wtf, ", requestPromises);
var flatRequestPromises = _.flatten(requestPromises);
console.log("wtflat, ", flatRequestPromises);
return Promise.all(flatRequestPromises).then(function(allResps) {
console.log("Neighborhood all", allResps) ;
return _.flatten(allResps);
});
}
},
{
// Our route needs to match a pattern of integers that
// are used as eventIds
route: "events[{integers:eventIds}]['name', 'description', 'location']",
get: function(pathSet) {
var results = [];
// Above we specified an eventIds identifier that is an
// array of ids which we can loop over
pathSet.eventIds.forEach(function(eventId) {
// Next, an array of key names that map is held at pathSet[2]
pathSet[2].map(function(key) {
// We find the event the cooresponds to the current eventId
var eventRecord = eventsData.events[eventId];
// Finally we push a path/value object onto
// the results array
results.push({
path: ['events', eventId, key],
value: eventRecord[key]
});
});
});
return results;
}
},
{
// Our route needs to match a pattern of integers that
// are used as locationId
route: "locationsById[{integers:locationId}]['city', 'state']",
get: function(pathSet) {
var results = [];
// Above we specified an locationId identifier that is an
// array of ids which we can loop over
pathSet.locationId.forEach(function(locationId) {
// Next, an array of key names that map is held at pathSet[2]
pathSet[2].map(function(key) {
// We find the event the cooresponds to the current locationId
var location = eventsData.locationsById[locationId];
// Finally we push a path/value object onto
// the results array
results.push({
path: ['locationsById', locationId, key],
value: location[key]
});
});
});
return results;
}
},
{
// The search route will match keys that match the names
// of our conferences
route: "events.byName[{keys}]['description']",
get: function(pathSet) {
var results = [];
// We want to loop over each of the conference names provided
pathSet[2].forEach(function(name) {
// We also want to loop over all the events on the data object
// and check if conference name is there
eventsData.events.forEach(function(event) {
if(_.includes(event, name)) {
results.push({
path: ['events','byName', name, 'description'],
value: event.description
});
}
});
});
return results;
}
}
]);
}));
app.listen(3000);
console.log("Listening on http://localhost:3000");