@@ -13,7 +13,7 @@ var _ = require('lodash');
13
13
var Rate = require ( '../model/rate' ) ;
14
14
var UnsupportedCurrencyPairException = require ( '../error/UnsupportedCurrencyPairException' ) ;
15
15
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¤cies =%s&source =%s&format=1' ;
17
17
const Provider = 'Currency Layer' ;
18
18
19
19
/**
@@ -60,7 +60,7 @@ util.inherits(CurrencyLayerProvider, AbstractProvider);
60
60
CurrencyLayerProvider . prototype . fetchRate = function ( currencyPair , callback ) {
61
61
var self = this ;
62
62
63
- var url = sprintf ( URL , self . accessKey , currencyPair . getBaseCurrency ( ) + currencyPair . getQuoteCurrency ( ) ) ;
63
+ var url = sprintf ( URL , self . accessKey , currencyPair . getQuoteCurrency ( ) , currencyPair . getBaseCurrency ( ) ) ;
64
64
65
65
self . fetchContent ( url , function ( err , content ) {
66
66
if ( err ) {
@@ -75,12 +75,12 @@ CurrencyLayerProvider.prototype.fetchRate = function (currencyPair, callback) {
75
75
return callback ( new Error ( data . error . info ) , null ) ;
76
76
}
77
77
78
- if ( _ . isUndefined ( data ) || _ . isUndefined ( data . result ) ) {
78
+ if ( _ . isUndefined ( data ) || _ . isUndefined ( data . quotes || _ . isUndefined ( data . quotes [ currencyPair . toString ( '' ) ] ) ) ) {
79
79
return callback ( new UnsupportedCurrencyPairException ( currencyPair , Provider ) , null ) ;
80
80
}
81
81
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 ) ) ;
84
84
}
85
85
catch ( err ) {
86
86
return callback ( err , null ) ;
@@ -95,7 +95,7 @@ CurrencyLayerProvider.prototype.fetchRate = function (currencyPair, callback) {
95
95
CurrencyLayerProvider . prototype . fetchRateSync = function ( currencyPair ) {
96
96
var self = this ;
97
97
98
- var url = sprintf ( URL , self . accessKey , currencyPair . getBaseCurrency ( ) + currencyPair . getQuoteCurrency ( ) ) ;
98
+ var url = sprintf ( URL , self . accessKey , currencyPair . getQuoteCurrency ( ) , currencyPair . getBaseCurrency ( ) ) ;
99
99
var content = this . fetchContentSync ( url ) ;
100
100
101
101
// Parse the content fetch from currency layer
@@ -105,10 +105,10 @@ CurrencyLayerProvider.prototype.fetchRateSync = function (currencyPair) {
105
105
throw new Error ( data . error . info ) ;
106
106
}
107
107
108
- if ( _ . isUndefined ( data ) || _ . isUndefined ( data . result ) ) {
108
+ if ( _ . isUndefined ( data ) || _ . isUndefined ( data . quotes ) || _ . isUndefined ( data . quotes [ currencyPair . toString ( '' ) ] ) ) {
109
109
throw new UnsupportedCurrencyPairException ( currencyPair , Provider ) ;
110
110
}
111
111
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 ) ;
114
114
} ;
0 commit comments