Browse Source

v1.0.0

master
Sven Slootweg 6 years ago
parent
commit
f4336ba777
  1. 98
      README.md
  2. 25
      gulpfile.js
  3. 1
      index.coffee
  4. 1
      index.js
  5. 33
      lib/index.coffee
  6. 43
      lib/index.js
  7. 35
      package.json

98
README.md

@ -0,0 +1,98 @@
# get-exchange-rates
A simple way to get up-to-date exchange rates for Bitcoin and major national currencies. Does *not* require an API key.
# Data format
The base is currently in Euro - this means that the rates will indicate how much `currency` is equivalent to 1 Euro.
Rates are automatically cached for 5 minutes (but this is configurable), and there's currently no historical rate search.
Currently supported currencies (and their sources):
* BTC - Bitcoin (Blockchain.info)
* USD - United States Dollar (ECB, via fixer.io)
* CAD - Canadian Dollar (ECB, via fixer.io)
* GBP - British Pound (ECB, via fixer.io)
* CNY - Chinese Yuan (ECB, via fixer.io)
* RUB - Russian Ruble (ECB, via fixer.io)
* JPY - Japanese Yen (ECB, via fixer.io)
* AUD - Australian Dollar (ECB, via fixer.io)
* NZD - New Zealand Dollar (ECB, via fixer.io)
* BGN - Bulgarian Lev (ECB, via fixer.io)
* BRL - Brazilian Real (ECB, via fixer.io)
* CHF - Swiss Franc (ECB, via fixer.io)
* CZK - Czech Koruna (ECB, via fixer.io)
* DKK - Danish Krone (ECB, via fixer.io)
* HKD - Hong Kong Dollar (ECB, via fixer.io)
* HRK - Croation Kuna (ECB, via fixer.io)
* HUF - Hungarian Forint (ECB, via fixer.io)
* IDR - Indonesian Rupiah (ECB, via fixer.io)
* ILS - Israeli Shekel (ECB, via fixer.io)
* INR - Indian Rupee (ECB, via fixer.io)
* KRW - South Korean Won (ECB, via fixer.io)
* MXN - Mexican Peso (ECB, via fixer.io)
* MYR - Malaysian Ringgit (ECB, via fixer.io)
* NOK - Norwegian Krone (ECB, via fixer.io)
* PHP - Philippine Peso (ECB, via fixer.io)
* PLN - Polish Zloty (ECB, via fixer.io)
* RON - Romanian New Leu (ECB, via fixer.io)
* SEK - Swedish Krona (ECB, via fixer.io)
* SGD - Singapore Dollar (ECB, via fixer.io)
* THB - Thai Baht (ECB, via fixer.io)
* TRY - Turkish Lira (ECB, via fixer.io)
* ZAR - South African Rand (ECB, via fixer.io)
Each currency can be accessed from the result by its 3-letter abbreviation.
Note that fixer.io currencies are subject to change; the module does not currently do any validation, so availability of rates may change if fixer.io or the ECB decides to change them. This list of currencies was up-to-date as of September 20, 2015.
## License
[WTFPL](http://www.wtfpl.net/txt/copying/) or [CC0](https://creativecommons.org/publicdomain/zero/1.0/), whichever you prefer. A donation and/or attribution are appreciated, but not required.
## Donate
My income consists largely of donations for my projects. If this module is useful to you, consider [making a donation](http://cryto.net/~joepie91/donate.html)!
You can donate using Bitcoin, PayPal, Flattr, cash-in-mail, SEPA transfers, and pretty much anything else.
## Contributing
Pull requests welcome. Please make sure your modifications are in line with the overall code style, and ensure that you're editing the `.coffee` files, not the `.js` files.
Build tool of choice is `gulp`; simply run `gulp` while developing, and it will watch for changes.
Be aware that by making a pull request, you agree to release your modifications under the licenses stated above.
## Usage
A simple example:
```javascript
var Promise = require("bluebird");
var getExchangeRates = require("get-exchange-rates");
Promise.try(function() {
return getExchangeRates();
}).then(function(rates) {
var amountInEuro = 2;
var amountInUSD = amountInEuro * rates.USD;
console.log(amountInEuro + " EUR = " + amountInUSD + " USD");
// eg. "2 EUR = 2.2838 USD"
})
```
## API
### getExchangeRates([cacheExpiration])
Retrieves the exchange rates - either from the cache, or remotely. If retrieved remotely, the results are automatically cached. The rates are *not* rounded - you are responsible for doing this yourself.
Returns a Promise, that resolves with an object containing the rates.
The function will never reject - if either of the APIs is unavailable, the function will simply return stale rates. This may be changed at a later point (and will, in line with semantic versioning, result in a major version bump).
* __cacheExpiration__: *Optional, defaults to 5 minutes.* The duration in seconds, after which the cache should expire. The first call after this duration will result in the cache being repopulated. This option will only take effect if the result is retrieved remotely. __Be respectful to the API operators; please do not set this lower than 5 minutes (300 seconds).__

25
gulpfile.js

@ -0,0 +1,25 @@
var gulp = require('gulp');
/* CoffeeScript compile deps */
var gutil = require('gulp-util');
var coffee = require('gulp-coffee');
var cache = require('gulp-cached');
var remember = require('gulp-remember');
var plumber = require('gulp-plumber');
var source = ["lib/**/*.coffee", "index.coffee"]
gulp.task('coffee', function() {
return gulp.src(source, {base: "."})
.pipe(plumber())
.pipe(cache("coffee"))
.pipe(coffee({bare: true}).on('error', gutil.log)).on('data', gutil.log)
.pipe(remember("coffee"))
.pipe(gulp.dest("."));
});
gulp.task('watch', function () {
gulp.watch(source, ['coffee']);
});
gulp.task('default', ['coffee', 'watch']);

1
index.coffee

@ -0,0 +1 @@
module.exports = require "./lib"

1
index.js

@ -0,0 +1 @@
module.exports = require("./lib");

33
lib/index.coffee

@ -0,0 +1,33 @@
Promise = require "bluebird"
bhttp = require "bhttp"
lastRates = null
lastRateCheck = 0
expireTime = 5 * 60 * 1000 # 5 minutes
module.exports = (cacheExpiration) ->
Promise.try ->
if Date.now() > lastRateCheck + expireTime
# We need fresh API data, our cache has expired.
Promise.try ->
Promise.all [
bhttp.get "http://api.fixer.io/latest", decodeJSON: true
bhttp.get "https://blockchain.info/ticker", decodeJSON: true
]
.spread (fixerRates, blockchainRates) ->
eurRates = fixerRates.body.rates
eurRates.BTC = 1 / blockchainRates.body.EUR["15m"]
return eurRates
.then (rates) ->
lastRates = rates
lastRateCheck = Date.now()
if cacheExpiration?
expireTime = cacheExpiration * 1000
return rates
.catch (err) ->
# API is down. FIXME: Allow for logging this error somehow.
return lastRates
else
return lastRates

43
lib/index.js

@ -0,0 +1,43 @@
var Promise, bhttp, expireTime, lastRateCheck, lastRates;
Promise = require("bluebird");
bhttp = require("bhttp");
lastRates = null;
lastRateCheck = 0;
expireTime = 5 * 60 * 1000;
module.exports = function(cacheExpiration) {
return Promise["try"](function() {
if (Date.now() > lastRateCheck + expireTime) {
return Promise["try"](function() {
return Promise.all([
bhttp.get("http://api.fixer.io/latest", {
decodeJSON: true
}), bhttp.get("https://blockchain.info/ticker", {
decodeJSON: true
})
]);
}).spread(function(fixerRates, blockchainRates) {
var eurRates;
eurRates = fixerRates.body.rates;
eurRates.BTC = 1 / blockchainRates.body.EUR["15m"];
return eurRates;
}).then(function(rates) {
lastRates = rates;
lastRateCheck = Date.now();
if (cacheExpiration != null) {
expireTime = cacheExpiration * 1000;
}
return rates;
})["catch"](function(err) {
return lastRates;
});
} else {
return lastRates;
}
});
};

35
package.json

@ -0,0 +1,35 @@
{
"name": "get-exchange-rates",
"version": "1.0.0",
"description": "Exchange rates for Bitcoin and major national currencies. No API key required.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git://github.com/joepie91/node-get-exchange-rates"
},
"keywords": [
"exchange rates",
"currency"
],
"author": "Sven Slootweg",
"license": "WTFPL",
"dependencies": {
"bhttp": "^1.2.1",
"bluebird": "^2.9.24"
},
"devDependencies": {
"gulp": "~3.8.0",
"gulp-cached": "~0.0.3",
"gulp-coffee": "~2.0.1",
"gulp-concat": "~2.2.0",
"gulp-livereload": "~2.1.0",
"gulp-nodemon": "~1.0.4",
"gulp-plumber": "~0.6.3",
"gulp-remember": "~0.2.0",
"gulp-rename": "~1.2.0",
"gulp-util": "~2.2.17"
}
}
Loading…
Cancel
Save