Skip to content

Instantly share code, notes, and snippets.

@chepe263
Created April 17, 2016 01:57
Show Gist options
  • Save chepe263/c967cce993d137e6c778554f2c767493 to your computer and use it in GitHub Desktop.
Save chepe263/c967cce993d137e6c778554f2c767493 to your computer and use it in GitHub Desktop.
Get exchange rates from https://openexchangerates.org/
rExchange = function(symbol, apikey){
if (symbol === undefined || symbol.length !==3 ){
return null;
}
if (apikey === undefined && apikey.length !==32){
return null;
}
var xhr = new XMLHttpRequest();
xhr.open('GET', encodeURI("https://openexchangerates.org/api/latest.json?app_id="+apikey));
rate =
xhr.onload = function() {
if (xhr.status === 200) {
rates = JSON.parse(xhr.responseText).rates;
localStorage.setItem("__rates", JSON.stringify(rates) );
localStorage.setItem("__rateDate", (new Date()).toString());
if (symbol.length == 3 && rates.hasOwnProperty(symbol))
return ( rates[symbol] );
}
else {
return null;
//console.log( xhr.status);
}
};
xhr.send();
return rate
}
//rExchange();
console.log( rExchange("GTQ") );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment