Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nicolas Sandller
5,643 PointsIdea for a node.js application using bitreserve's API.
Check out my code guys, let me know if there are any improvements or cool stuff to do with this information.
app.js file
var profile = require("./profile.js")
/*
var pairs = ['CHFUSD','EURUSD']
var currencies = ['USD','AUD']
currencies.forEach(function(key){
profile.get(key);
})
*/
var pairs =process.argv.slice(2);
pairs.forEach(function(key){
profile.get(0,key);
})
profile.js file
var https = require("https");
//print out message
function printMessage(username, badgeCount, points) {
var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript";
return console.log(message);
}
//print out error message
function printError(error) {
return console.error(error.message);
}
function lookCurrency(profile,currency){
var result = profile.map(function(key){
if(key.currency === currency){
console.log(key);
}
});
}
function lookPair(profile,pair){
var result = profile.map(function(key){
if(key.pair === pair){
console.log(key);
}
});
}
function get(currency,pair){
var request = https.get("https://api.bitreserve.org/v0/ticker", function(response) {
//Read the data
var body = "";
response.on("data", function(chunk) {
body += chunk;
});
response.on("end", function(){
if(response.statusCode === 200){
//Parse the data
var profile = JSON.parse(body);
if(currency === 0){lookPair(profile,pair);}
else{lookCurrency(profile,currency);}
} else{
printError({message:"There was an error retrieving the information. Error: " + response.statusCode});
}
})
});
request.on('error', function(error) {
printError(error);
});
}
module.exports.get = get;
now run this command: node bitreserve-rates.js EURUSD CHFUSD