-
Notifications
You must be signed in to change notification settings - Fork 260
/
ticker.js
52 lines (46 loc) · 1.36 KB
/
ticker.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
// Price ticker
EthTools.ticker = new Mongo.Collection("ethereum_price_ticker", {
connection: null
});
if (Meteor.isClient) new PersistentMinimongo(EthTools.ticker);
EthTools.ticker.start = function(options) {
options = options || {};
if (!options.currencies) {
options.currencies = ["BTC", "USD", "EUR"];
}
var url =
"https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=" +
options.currencies.join(",");
if (options.extraParams) {
url += "&extraParams=" + options.extraParams;
}
var updatePrice = function(e, res) {
if (!e && res && res.statusCode === 200) {
var content = JSON.parse(res.content);
if (content) {
_.each(content, function(price, key) {
var name = key.toLowerCase();
// make sure its a number and nothing else!
if (_.isFinite(price)) {
EthTools.ticker.upsert(name, {
$set: {
price: String(price),
timestamp: null
}
});
}
});
}
} else {
console.warn(
"Can not connect to https://mini-api.cryptocompare.com to get price ticker data, please check your internet connection."
);
}
};
// update right away
HTTP.get(url, updatePrice);
// update prices
Meteor.setInterval(function() {
HTTP.get(url, updatePrice);
}, 1000 * 30);
};