This gem extends Money::Bank::VariableExchange of gem money and gives you access to the current exchange rates using currencylayer.com
- supports 168 currencies
- precision of rates up to 6 digits after point
- uses fast and reliable json api
- average response time < 20ms
- supports caching currency rates
Add this line to your application's Gemfile:
gem 'currencylayer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install currencylayer
First, you should to register account on currencylayer.com and get your personal access_key.
require 'money'
require 'money/bank/currencylayer'
# (optional)
# set the seconds after than the current rates are automatically expired
# by default, they never expire
Money::Bank::Currencylayer.ttl_in_seconds = 7200 # 2 hours ttl
# set careful mode - each rate stores with created_at time to cache and will be flushed
# only if their time is out. If you get exception while request new rate, bank will
# return cached value if present
# by default false
Money::Bank::Currencylayer.rates_careful = true
# create new bank instance
bank = Money::Bank::Currencylayer.new
# create new bank instance with block specifying rounding of exchange result
bank = Money::Bank::Currencylayer.new {|n| n.round(4)} # round result to 4 digits after point
# specify your access_key from currencylayer.com
bank.access_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# set default bank to instance
Money.default_bank = bank
Also you can setup Currencylayer as default_bank for money-rails gem in config/initializers/money.rb
require 'money/bank/currencylayer'
MoneyRails.configure do |config|
bank = Money::Bank::Currencylayer.new
bank.access_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
config.default_bank = bank
end
An AccessError
will be thrown if access_key was not specified.
An RequestError
will be thrown if currencylayer.com api returns error on api request.
Created using VariableExchange
implementation and using google_currency
basics.
More implementations:
- https://github.com/RubyMoney/eu_central_bank
- https://github.com/matiaskorhonen/nordea
- https://github.com/slbug/nbrb_currency
- https://github.com/spk/money-open-exchange-rates
- https://github.com/atwam/money-historical-bank
- https://github.com/rmustafin/russian_central_bank
- Fork it ( https://github.com/[my-github-username]/currencylayer/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request