-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
63 lines (57 loc) · 1.8 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
var Amazon = require('./lib/amazon');
var humanizer = require('./lib/humanizer');
function ABS(settings){
if(!settings || !settings.awsKey || !settings.awsSecret){
this.configured = false;
}else{
this.configured = true;
this.amazon = new Amazon(settings);
}
}
/**
* Main search function
* @param {[type]} searchArguments [The Search query, in a string form, example 'how to become a programmer']
* @param {Function} callback [in the form function(error, results){}]
*/
ABS.prototype.search = function search(searchArguments, page, callback) {
if(typeof page === 'function'){
var callback = page;
}
if(!this.configured){callback({message:'Not Properly Configured'},null);return;}
if(!searchArguments){searchArguments='';}
this.resultsHandler = function(error, results){
if(error){callback(error, null);return};
humanizer.transformResults(results, callback);
};
// TODO
// -> End the request and force error response if it exceedes some timeout
// -> Cache this request to answer faster or with cached results in case of timeout
if(typeof page !== 'function'){
this.amazon.query(searchArguments, page, this.resultsHandler );
}else{
this.amazon.query(searchArguments, this.resultsHandler);
}
};
/**
* Use this book search as a connect like middleware
* @return {[type]} [description]
*/
ABS.prototype.middleware = function searchMiddleware(){
var self = this;
return function(req, res, next){
outputCallback = function(error, response){
if(error){
res.send(400, error);
return false;
};
res.send(200, response);
res.end();
};
if(req.query.page){
self.search(req.query.q, req.query.page, outputCallback);
}else{
self.search(req.query.q, outputCallback);
}
};
}
module.exports = ABS;