Skip to content

DEPRECATED, RubyMoney bank that will fetch exchange rates from ECB

Notifications You must be signed in to change notification settings

ct-clearhaus/money-ecb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!

About

DEPRECATED, RubyMoney bank that will fetch exchange rates from ECB

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages