-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (59 loc) · 1.51 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var _ = require('underscore');
var async = require('async');
var Promise = require('promise');
var tapfinder = require('./lib/tapfinder');
exports.handler = function (event, context) {
console.log(event);
getTapfinderSearchResults(event, context).then(
function(results) {
context.succeed(results);
}
).catch(
function(error) {
console.error(error);
context.fail();
}
);
};
function getTapfinderSearchResults(event, context) {
return tapfinder.search(event.term).then(
function(matches) {
var resultsPromises = [];
event.types.forEach(
function(type) {
resultsPromises.push(loadResults(type, matches));
}
);
return Promise.all(resultsPromises).then(
function (resultsPerType) {
return resultsPerType.reduce(
function(object, value) {
object[value[0]] = value[1];
return object;
}, {}
);
}
);
}
);
}
function loadResults(type, matches) {
var matchesForType = matches[type];
var matchLoader = tapfinderLoaderForType(type);
var tasks = _.map(matchesForType, matchLoader);
return(Promise.all(tasks)).then(
function (results) {
return [type, results];
}
);
}
function tapfinderLoaderForType(type) {
switch(type) {
case "beers":
return tapfinder.loadBeer.bind(tapfinder);
case "bars":
return tapfinder.loadBar.bind(tapfinder);
default:
throw "Could not determine loader for type " + type;
}
}