-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternational-ppc-currency-converter.js
More file actions
59 lines (47 loc) · 1.93 KB
/
international-ppc-currency-converter.js
File metadata and controls
59 lines (47 loc) · 1.93 KB
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
54
55
56
57
58
59
// ID: 6ebc498a9290007f5e794c8f73fe7009
/**
* A simple script to provide you with conversion rates.
* You need an an API Key from https://fixer.io/product.
*/
function main() {
var originalCurrency = 'GBP'; // 3 letter currency code for starting currency
var targetCurrency = 'USD'; // 3 letter currency code for target currency
var customDate = '2019-05-01'; // Date of exchange rate in form 'yyyy-MM-dd'. Leave as blank string for current exchange rates. Dates for fixer.io are valid from 2000 onwards only
var apiKey = ''; // Get an API Key from https://fixer.io/product
var exchangeRate = GetExchangeRate(originalCurrency, targetCurrency, customDate, apiKey);
Logger.log(exchangeRate);
}
function GetExchangeRate(originalCurrency, targetCurrency, customDate, apiKey) {
if (originalCurrency == targetCurrency) {
return 1;
}
var url = ConstructUrl(customDate, apiKey);
try {
var jsonResponseString = UrlFetchApp.fetch(url).getContentText();
} catch (err) {
var errorMessage = 'Error has occurred. Unable to Access URL. Please check input parameters';
Logger.log(errorMessage);
return errorMessage;
}
var jsonResponseObject = JSON.parse(jsonResponseString);
var exchangeRates = jsonResponseObject.rates;
if (!(originalCurrency in exchangeRates)) {
throw originalCurrency + ' not a valid currency';
}
if (!(targetCurrency in exchangeRates)) {
throw targetCurrency + ' not a valid currency';
}
var exchangeRateToOriginalCurrency = exchangeRates[originalCurrency];
var exchangeRateToTargetCurrency = exchangeRates[targetCurrency];
var exchangeRate = exchangeRateToTargetCurrency / exchangeRateToOriginalCurrency;
return exchangeRate;
}
function ConstructUrl(customDate, apiKey) {
var baseUrl = 'http://data.fixer.io/';
if (customDate === '') {
customDate = 'latest';
}
var finalUrl = baseUrl + customDate;
finalUrl = finalUrl + '?access_key=' + apiKey;
return finalUrl;
}