added rate services to wallet

This commit is contained in:
Ivan Socolsky 2014-11-05 22:05:09 -03:00
commit 015af22638
4 changed files with 25 additions and 11 deletions

View file

@ -20,15 +20,26 @@ var RateService = function(opts) {
self._isAvailable = false;
self._rates = {};
self._alternatives = {};
self.queued = [];
self._alternatives = [];
self._queued = [];
self._fetchCurrencies();
}
};
var _instance;
RateService.singleton = function(opts) {
if (!_instance) {
_instance = new RateService(opts);
}
return _instance;
};
RateService.prototype._fetchCurrencies = function() {
var self = this;
log.info('Fetching exchange rates');
var backoffSeconds = 5;
var updateFrequencySeconds = 3600;
var rateServiceUrl = 'https://bitpay.com/api/rates';
@ -43,15 +54,15 @@ RateService.prototype._fetchCurrencies = function() {
return;
}
_.each(body, function(currency) {
self.rates[currency.code] = currency.rate;
self.alternatives.push({
self._rates[currency.code] = currency.rate;
self._alternatives.push({
name: currency.name,
isoCode: currency.code,
rate: currency.rate
});
});
self._isAvailable = true;
_.each(self.queued, function(callback) {
_.each(self._queued, function(callback) {
setTimeout(callback, 1);
});
setTimeout(function() {
@ -81,7 +92,7 @@ RateService.prototype.whenAvailable = function(callback) {
if (!this.isAvailable()) {
setTimeout(callback, 1);
} else {
this.queued.push(callback);
this._queued.push(callback);
}
};