Skip to content

Instantly share code, notes, and snippets.

@booyaa
Created December 2, 2012 17:45
Show Gist options
  • Select an option

  • Save booyaa/4190115 to your computer and use it in GitHub Desktop.

Select an option

Save booyaa/4190115 to your computer and use it in GitHub Desktop.
castlemaine - nodejs tool to convert foreign exchange currencies
module.exports = {
apiKey : 'YOUR OPEN EXCHANGE RATES API KEY'
, sourceCcy : 'USD'
, targetCcy : 'GBP'
}
#!/usr/bin/env node
// castlemaine - nodejs tool to convert foreign exchange currencies
// requires npm modules open-exchange-rates and money
// largely inspired by the demo code provided by the open-exchange-rates module
/*jshint laxcomma : true */
var oxr = require('open-exchange-rates')
,fx = require('money')
,conf = require('./castlemaine-conf');
oxr.set({ app_id: conf.apiKey });
oxr.latest(function() {
// Apply exchange rates and base rate to `fx` library object:
fx.rates = oxr.rates; fx.base = oxr.base;
var amount, sourceCcy, targetCcy;
if (process.argv.length < 3 || process.argv.length > 5) {
usage();
process.exit(1);
}
amount = process.argv[2];
if (process.argv.length === 3) {
sourceCcy = conf.sourceCcy;
targetCcy = conf.targetCcy;
}
if (process.argv.length === 4) {
sourceCcy = conf.sourceCcy;
targetCcy = process.argv[3].toUpperCase();
}
if (process.argv.length === 5) {
sourceCcy = process.argv[3].toUpperCase();
targetCcy = process.argv[4].toUpperCase();
}
console.log('%d %s = %d %s', amount, sourceCcy, fx(amount).from(sourceCcy).to(targetCcy), targetCcy);
});
function usage() {
var appName = process.argv[1].split('/')[7];
console.log("error! insufficient arguments supplied!");
console.log("examples");
console.log('convert 12.50 %s to %s - %s 12.5', conf.sourceCcy, conf.targetCcy, appName);
console.log('convert 12.50 %s to AED - %s 12.5 AED', conf.sourceCcy, appName);
console.log('convert 12.50 USD to EUR - %s 12.5 USD EUR', appName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment