forked from ethereum/meteor-package-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker.js
53 lines (47 loc) · 1.4 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
53
// 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 ticker = options.ticker || "ETH";
var url =
`https://min-api.cryptocompare.com/data/price?fsym=${ticker}&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);
};