Created
April 17, 2016 01:57
-
-
Save chepe263/c967cce993d137e6c778554f2c767493 to your computer and use it in GitHub Desktop.
Get exchange rates from https://openexchangerates.org/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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