-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
51 lines (45 loc) · 1.48 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
var NodeHelper = require("node_helper");
var axios = require('axios').default;
var moment = require("moment");
module.exports = NodeHelper.create({
start: function () {
console.log(this.name + " helper method started...");
},
sendRequest: function (urls) {
var self = this;
console.log( "[MMM-Stock] " + moment().format("D-MMM-YY HH:mm") + " making URL calls" );
//urls is an array of options, only using 1 for now
axios(urls[0])
.then(function (response) {
if (response.status == 200) {
self.sendSocketNotification("STOCK_RESULT", response.data);
}
})
.catch(function (error) {
// handle error
console.log( "[MMM-Stock] " + moment().format("D-MMM-YY HH:mm") + " Request " + error );
});
},
sendExchangeRate: function (url) {
var self = this;
axios.get(url)
.then(function (response) {
if (response.status == 200) {
self.sendSocketNotification("EXCHANGE_RATE", response.data);
}
})
.catch(function (error) {
// handle error
console.log( "[MMM-Stock] Exchange Rate " + moment().format("D-MMM-YY HH:mm") + " Exchange " + error );
});
},
//Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, urls) {
if (notification === "GET_STOCKS") {
// console.log( "[MMM-Stock] " + moment().format("D-MMM-YY HH:mm") + " sending requests: "+JSON.stringify(urls, null, 4) );
this.sendRequest(urls);
} else if(notification === "GET_EXCHANGE_RATE"){
this.sendExchangeRate(urls);
}
}
});