Project

money-ecb

0.0
No commit activity in last 3 years
No release in over 3 years
A Money::Bank that will fetch foreign exchange rates from the EU Central Bank (ECB).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 6.0.0, ~> 6.0
>= 1.0.0, ~> 1.0
 Project Readme

money-ecb, DEPRECATED

Build Status Code Climate Coveralls Dependency Status MIT License

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::Banks 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)

Contribute

  • Fork
  • Clone
  • bundle install && bundle exec rake test
  • Make your changes
  • bundle exec rake test again, preferably against Ruby 1.9.3, 2.0.0 and 2.1.0 (Travis will do that).
  • Create a Pull Request
  • Enjoy!