Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 08eb249

Browse files
Fixed issue #2
1 parent a340c32 commit 08eb249

File tree

3 files changed

+14
-15
lines changed

3 files changed

+14
-15
lines changed

lib/model/currencypair.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ CurrencyPair.prototype.getQuoteCurrency = function () {
5555
* Returns a string representation of the pair.
5656
* @returns string
5757
*/
58-
CurrencyPair.prototype.toString = function () {
59-
return this.baseCurrency + '/' + this.quoteCurrency;
58+
CurrencyPair.prototype.toString = function (seprator) {
59+
seprator = _.isUndefined(seprator) ? '/' : seprator;
60+
return this.baseCurrency + seprator + this.quoteCurrency;
6061
};

lib/provider/currencylayer.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var _ = require('lodash');
1313
var Rate = require('../model/rate');
1414
var UnsupportedCurrencyPairException = require('../error/UnsupportedCurrencyPairException');
1515

16-
const URL = 'http://apilayer.net/api/convert?access_key=%s&from=%s&to=%s&amount=1&format=1';
16+
const URL = 'http://apilayer.net/api/live?access_key=%s&currencies=%s&source=%s&format=1';
1717
const Provider = 'Currency Layer';
1818

1919
/**
@@ -60,7 +60,7 @@ util.inherits(CurrencyLayerProvider, AbstractProvider);
6060
CurrencyLayerProvider.prototype.fetchRate = function (currencyPair, callback) {
6161
var self = this;
6262

63-
var url = sprintf(URL, self.accessKey, currencyPair.getBaseCurrency() + currencyPair.getQuoteCurrency());
63+
var url = sprintf(URL, self.accessKey, currencyPair.getQuoteCurrency(), currencyPair.getBaseCurrency());
6464

6565
self.fetchContent(url, function (err, content) {
6666
if (err) {
@@ -75,12 +75,12 @@ CurrencyLayerProvider.prototype.fetchRate = function (currencyPair, callback) {
7575
return callback(new Error(data.error.info), null);
7676
}
7777

78-
if (_.isUndefined(data) || _.isUndefined(data.result)) {
78+
if (_.isUndefined(data) || _.isUndefined(data.quotes || _.isUndefined(data.quotes[currencyPair.toString('')]))) {
7979
return callback(new UnsupportedCurrencyPairException(currencyPair, Provider), null);
8080
}
8181

82-
var date = data.info && data.info.timestamp ? new Date(data.info.timestamp * 1000) : new Date();
83-
return callback(null, new Rate(data.result.toString(), date, Provider));
82+
var date = data.timestamp ? new Date(data.timestamp * 1000) : new Date();
83+
return callback(null, new Rate(data.quotes[currencyPair.toString('')], date, Provider));
8484
}
8585
catch (err) {
8686
return callback(err, null);
@@ -95,7 +95,7 @@ CurrencyLayerProvider.prototype.fetchRate = function (currencyPair, callback) {
9595
CurrencyLayerProvider.prototype.fetchRateSync = function (currencyPair) {
9696
var self = this;
9797

98-
var url = sprintf(URL, self.accessKey, currencyPair.getBaseCurrency() + currencyPair.getQuoteCurrency());
98+
var url = sprintf(URL, self.accessKey, currencyPair.getQuoteCurrency(), currencyPair.getBaseCurrency());
9999
var content = this.fetchContentSync(url);
100100

101101
// Parse the content fetch from currency layer
@@ -105,10 +105,10 @@ CurrencyLayerProvider.prototype.fetchRateSync = function (currencyPair) {
105105
throw new Error(data.error.info);
106106
}
107107

108-
if (_.isUndefined(data) || _.isUndefined(data.result)) {
108+
if (_.isUndefined(data) || _.isUndefined(data.quotes) || _.isUndefined(data.quotes[currencyPair.toString('')])) {
109109
throw new UnsupportedCurrencyPairException(currencyPair, Provider);
110110
}
111111

112-
var date = data.info && data.info.timestamp ? new Date(data.info.timestamp * 1000) : new Date();
113-
return new Rate(data.result.toString(), date, Provider);
112+
var date = data.timestamp ? new Date(data.timestamp * 1000) : new Date();
113+
return new Rate(data.quotes[currencyPair.toString('')], date, Provider);
114114
};

test/provider/currencylayer.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ describe('currency layer provider', function () {
3737

3838
assert.throw(function () {
3939
provider.fetchRateSync(currencyPair);
40-
}, UnsupportedCurrencyPairException,
41-
'The currency pair "USD/XXL" is not supported or "Currency Layer" changed the response format')
40+
}, Error)
4241
});
4342

4443
it('it_fetches_a_rate_sync', function () {
@@ -55,8 +54,7 @@ describe('currency layer provider', function () {
5554
provider.fetchRate(currencyPair, function (err, rate) {
5655
assert.throw(function () {
5756
throw err;
58-
}, UnsupportedCurrencyPairException,
59-
'The currency pair "USD/XXL" is not supported or "Currency Layer" changed the response format');
57+
}, Error);
6058
done();
6159
});
6260
});

0 commit comments

Comments
 (0)