-
Notifications
You must be signed in to change notification settings - Fork 0
/
guestful-persistent.js
executable file
·49 lines (43 loc) · 1.9 KB
/
guestful-persistent.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
var request = require('request'),
async = require('async');
module.exports = {
load: function (config, callback) {
var self = this,
calls = {};
for (var v in config) {
calls[v] = (function (varname) {
var fn = function (asyncCallback) {
if (asyncCallback) {
console.log('Loading "persistent.' + varname + '" from ' + config[varname].url);
} else {
console.log('Reloading "persistent.' + varname + '" from ' + config[varname].url);
}
request(config[varname].url, function (error, response, body) {
if (error || response.statusCode != 200) {
if (asyncCallback) {
asyncCallback(new Error("Could not load static file for '" + varname + "' from: " + config[varname].url + (error ? error : " (status code: " + response.statusCode + ")")));
}
} else {
self[varname] = JSON.parse(body);
if (config[varname].transform && typeof config[varname].transform == "function") {
self[varname] = config[varname].transform(self[varname]);
}
if (asyncCallback) {
asyncCallback();
}
}
});
};
if (config[varname].maxAge && config[varname].maxAge > 0) {
setInterval(function () {
fn();
}, config[varname].maxAge * 1000);
}
return fn;
})(v);
}
async.parallel(calls, function (err) {
callback(err);
});
}
};