From 1c480d21a6685cbb4bbd6a034cdd3853667a4e1e Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Fri, 29 Aug 2014 12:09:39 -0300 Subject: [PATCH] Address @matiu's comments --- config.js | 5 +++++ js/services/rate.js | 21 ++++++++++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/config.js b/config.js index 959cb3707..d71dd7624 100644 --- a/config.js +++ b/config.js @@ -56,6 +56,11 @@ var defaultConfig = { storageSalt: 'mjuBtGybi/4=', }, + rate: { + url: 'https://bitpay.com/api/rates', + updateFrequencySeconds: 60 * 60 + }, + disableVideo: true, verbose: 1, }; diff --git a/js/services/rate.js b/js/services/rate.js index 764e5c914..0c28ae046 100644 --- a/js/services/rate.js +++ b/js/services/rate.js @@ -2,19 +2,25 @@ var RateService = function(request) { this.isAvailable = false; + this.UNAVAILABLE_ERROR = 'Service is not available - check for service.isAvailable or use service.whenAvailable'; this.SAT_TO_BTC = 1 / 1e8; + var MINS_IN_HOUR = 60; + var MILLIS_IN_SECOND = 1000; + var rateServiceConfig = config.rate; + var updateFrequencySeconds = rateServiceConfig.updateFrequencySeconds || 60 * MINS_IN_HOUR; + var rateServiceUrl = rateServiceConfig.url || 'https://bitpay.com/api/rates'; this.queued = []; this.alternatives = []; var that = this; - var backoff = 5; + var backoffSeconds = 5; var retrieve = function() { request.get({ - url:'https://bitpay.com/api/rates', + url: rateServiceUrl, json: true }, function(err, response, listOfCurrencies) { if (err) { - backoff *= 1.5; - setTimeout(retrieve, backoff * 1000); + backoffSeconds *= 1.5; + setTimeout(retrieve, backoffSeconds * MILLIS_IN_SECOND); return; } var rates = {}; @@ -31,6 +37,7 @@ var RateService = function(request) { that.queued.forEach(function(callback) { setTimeout(callback, 1); }); + setTimeout(retrieve, updateFrequencySeconds * MILLIS_IN_SECOND); }); }; retrieve(); @@ -46,21 +53,21 @@ RateService.prototype.whenAvailable = function(callback) { RateService.prototype.toFiat = function(satoshis, code) { if (!this.isAvailable) { - return 0; + throw new Error(this.UNAVAILABLE_ERROR); } return satoshis * this.SAT_TO_BTC * this.rates[code]; }; RateService.prototype.fromFiat = function(amount, code) { if (!this.isAvailable) { - return 0; + throw new Error(this.UNAVAILABLE_ERROR); } return amount / this.rates[code] / this.SAT_TO_BTC; }; RateService.prototype.listAlternatives = function() { if (!this.isAvailable) { - return []; + throw new Error(this.UNAVAILABLE_ERROR); } var alts = [];