Created
December 2, 2012 17:45
-
-
Save booyaa/4190115 to your computer and use it in GitHub Desktop.
castlemaine - nodejs tool to convert foreign exchange currencies
This file contains hidden or 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
| module.exports = { | |
| apiKey : 'YOUR OPEN EXCHANGE RATES API KEY' | |
| , sourceCcy : 'USD' | |
| , targetCcy : 'GBP' | |
| } |
This file contains hidden or 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
| #!/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