-
Notifications
You must be signed in to change notification settings - Fork 16
/
node_helper.js
65 lines (53 loc) · 1.53 KB
/
node_helper.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
/* Magic Mirror
* Module: stocks
*
* By Alex Yakhnin https://github.com/alexyak
* MIT Licensed.
*/
var NodeHelper = require('node_helper');
var request = require('request');
module.exports = NodeHelper.create({
start: function () {
console.log('stocks helper started ...');
},
getStocks: function (url) {
var self = this;
console.log('requesting:' + url);
request({ url: url, method: 'GET' }, function (error, response, body) {
if (!error && response.statusCode == 200) {
var result = JSON.parse(body);
self.sendSocketNotification('STOCKS_RESULT', result);
}
});
},
getStocksMulti: function (urls) {
var self = this;
var count = urls.length;
var counter = 0;
var stockResults = [];
urls.forEach(url => {
console.log('url:' + url);
request({ url: url, method: 'GET' }, function (error, response, body) {
if (!error && response.statusCode == 200) {
stockResults.push(JSON.parse(body));
counter++;
if (counter == count - 1) {
self.sendSocketNotification('STOCKS_RESULT', stockResults);
}
}
else {
var err = error;
}
});
});
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload) {
if (notification === 'GET_STOCKS') {
this.getStocks(payload);
}
if (notification === "GET_STOCKS_MULTI") {
this.getStocksMulti(payload);
}
}
});