money-ecb, DEPRECATED
Introduction
This gem is a RubyMoney bank that can
exchange Money
using rates from the ECB (European Central Bank). It will
automatically keep the rates updated.
It is not production ready yet. Not because it isn't stable, but because I might change the API. Wait a month or two. Or three 😄
Installation
gem install money-ecb
In your Gemfile
may want to have gem 'money-ecb', :require =>
'money/bank/ecb'
.
Dependencies
- RubyMoney's
money
gem -
rubyzip
gem
Example
Using Money and Monetize:
require 'money'
require 'money/bank/ecb'
require 'monetize/core_extensions'
Money.default_bank = Money::Bank::ECB.new
puts '1 EUR'.to_money.exchange_to(:USD)
Rounding
By default, Money::Bank
s will truncate. If you prefer to round:
puts '1 EUR'.to_money.exchange_to(:USD) {|x| x.round}
If you would like to have rounding done by default, you can set the default when creating the bank:
Money.default_bank = Money::Bank::ECB.new {|x| x.round}
Local cache file
For your convenience, .new
will accept a string representing a file
path (or a Money::Bank::ECB::Cache
).
If the file path holds a valid CSV file with exchange rates, the rates will be
used for conversion (unless newer rates are available—see auto-update). If
the file does not exist or is "somehow bogus", new rates will be downloaded from
the European Central Bank and stored in the file (or an
InvalidCacheError
will be raise if auto-update is off).
Auto-update rates
The European Central Bank publishes foreign exchange rates daily, and they
should be available at 14:00 CE(S)T. The cache is automatically updated when
doing an exchange after new rates has been published; to disable this, set
#auto_update = false
; to force, #update_cache
and
#reload
(or both in one take, #update
).
Also notice that when instantiating an ECB
, rates will be loaded from
the cache file, and if that fails, new rates will be fetched automatically. So
if you want to handle updating rates "by hand", you should place a valid cache
before .new
and then call #reload
after you updated the cache.
Can I code my own cache?
Yes, have a look in lib/money/bank/ecb/
; just include
Money::Bank::ECB::Cache
and implement #set
and #get
and use
MyCache
:
cache = Money::Bank::ECB::MyCache.new(params)
Money.default_bank = Money::Bank::ECB.new(cache)