A gem that calculates the exchange rate using published rates from open-exchange-rates
Add this line to your application's Gemfile:
gem 'money-open-exchange-rates'
And then execute:
bundle
Or install it yourself as:
gem install money-open-exchange-rates
require 'money/bank/open_exchange_rates_bank'
moe = Money::Bank::OpenExchangeRatesBank.new
moe.cache = 'path/to/file/cache'
moe.app_id = 'your app id from https://openexchangerates.org/signup'
moe.update_rates
# (optional)
# set the seconds after than the current rates are automatically expired
# by default, they never expire, in this example 1 day.
moe.ttl_in_seconds = 86400
# (optional)
# use https to fetch rates from Open Exchange Rates
# disabled by default to support free-tier users
# see https://openexchangerates.org/documentation#https
moe.secure_connection = true
# (optional)
# set historical date of the rate
# see https://openexchangerates.org/documentation#historical-data
moe.date = '2015-01-01'
# Store in cache
moe.save_rates
Money.default_bank = moe
You can also provide a Proc as a cache to provide your own caching mechanism
perhaps with Redis or just a thread safe Hash
(global). For example:
moe.cache = Proc.new do |v|
key = 'money:exchange_rates'
if v
Thread.current[key] = v
else
Thread.current[key]
end
end
bundle exec rake
- https://github.com/currencybot/open-exchange-rates
- https://github.com/RubyMoney/money
- https://github.com/RubyMoney/eu_central_bank
- https://github.com/RubyMoney/google_currency
See GitHub.
The MIT License
Copyright © 2011-2016 Laurent Arnoud [email protected]