-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviders.js
89 lines (71 loc) · 1.96 KB
/
providers.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var unirest = require('unirest');
function Provider(options){
options = options || {}
this.name = options.name || null;
this.description = options.description || null;
this.restUrl = options.restUrl || null;
this.refreshInterval = options.refreshInterval || 30000;
this.last = 0;
this.bid = 0;
this.ask = 0;
this.high = 0;
this.low = 0;
}
Provider.prototype.refreshPrice = function(){
var that = this;
var request = unirest.get(this.restUrl);
request.set('Accepts', 'application/json');
request.end(function(response){
var data = JSON.parse(response.body);
var keys = ['last', 'bid', 'ask', 'high', 'low'];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
that[key] = data[key];
}
console.log("Refreshed price for " + that.name + ", new price " + that.bid);
});
};
Provider.prototype.startListening = function(){
var that = this;
console.log("Started listening for " + this.name + " provider, querying " +
this.restUrl + " every " + this.refreshInterval+" ms...");
this.timerId = setInterval(
function() {
that.refreshPrice();
},
this.refreshInterval
);
this.refreshPrice();
};
Provider.prototype.stopListening = function(){
console.log("Stopped listening for " + this.name + " provider.");
clearInterval(this.timerId);
};
Provider.prototype.getName = function(){
return this.name;
};
Provider.prototype.getDescription = function(){
return this.description;
};
Provider.prototype.getRestUrl = function(){
return this.restUrl;
};
Provider.prototype.getRefreshInterval = function(){
return this.refreshInterval;
};
Provider.prototype.getBid = function(){
return this.bid;
};
Provider.prototype.getAsk = function(){
return this.ask;
};
Provider.prototype.getHigh = function(){
return this.high;
};
Provider.prototype.getLow = function(){
return this.low;
};
exports.createProvider = function(options){
return new Provider(options);
};
exports.Provider = Provider;