Money Open Exchange Rates

A gem that calculates the exchange rate using published rates from open-exchange-rates

Check api documentation

Installation

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

Usage

require 'money/bank/open_exchange_rates_bank'
oxr = Money::Bank::OpenExchangeRatesBank.new
oxr.cache = 'path/to/file/cache.json'
oxr.app_id = 'your app id from https://openexchangerates.org/signup'
oxr.update_rates

# (optional)
# set the seconds after than the current rates are automatically expired
# by default, they never expire, in this example 1 day.
oxr.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
oxr.secure_connection = true
# (optional)
# set historical date of the rate
# see https://openexchangerates.org/documentation#historical-data
oxr.date = '2015-01-01'
# (optional)
# Set the base currency for all rates. By default, USD is used.
# OpenExchangeRates only allows USD as base currency
# for the free plan users.
oxr.source = 'USD'

# Store in cache
oxr.save_rates

Money.default_bank = oxr

Money.default_bank.get_rate('USD', 'CAD')

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:

oxr.cache = Proc.new do |v|
  key = 'money:exchange_rates'
  if v
    Thread.current[key] = v
  else
    Thread.current[key]
  end
end

Unknown pair rates are transparently calculated: using inverse rate (if known), or using base currency rate to both currencies forming the pair.

Tests

bundle exec rake

Refs

Contributors

See GitHub.

License

The MIT License

Copyright © 2011-2016 Laurent Arnoud [email protected]


Build Version Documentation License Code Climate Inline docs