A gem that calculates the exchange rate using published rates from openexchangerates.org
{
"timestamp": 1441101909,
"base": "USD",
"rates": {
/* 190 currencies */
"AUD": 1.413637,
"CAD": 1.316495,
"CHF": 0.96355,
"EUR": 0.888466,
"BTC": 0.004322, /* Includes cryptocurrencies! */
...
}
}
See more about Openexchangerates product plans on https://openexchangerates.org/signup.
- supports over 200 currencies
- includes cryptocurrencies
- precision of rates up to 6 digits after point
- uses fast and reliable json api
- average response time < 20ms
- supports caching currency rates
- calculates every pair rate calculating inverse rate or using base currency rate
- supports multiple server instances, thread safe
Add this line to your application's Gemfile:
gem 'money-openexchangerates-bank'
And then execute:
$ bundle
Or install it yourself as:
$ gem install money-openexchangerates-bank
# Minimal requirements.
require 'money/bank/openexchangerates_bank'
moxb = Money::Bank::OpenexchangeratesBank.new
moxb.access_key = 'your access_key from https://openexchangerates.com/signup'
# (optional)
# Set the base currency for all rates. By default, USD is used.
# OpenexchangeratesBank only allows USD as base currency for the free plan users.
moxb.source = 'EUR'
# (optional)
# Set the seconds after than the current rates are automatically expired.
# By default, they never expire, in this example 1 day.
moxb.ttl_in_seconds = 86400
# (optional)
# Use https to fetch rates from OpenexchangeratesBank.
# OpenexchangeratesBank only allows http as connection for the free plan users.
moxb.secure_connection = true
# Define cache (string or pathname).
moxb.cache = 'path/to/file/cache'
# Update rates (get new rates from remote if expired or access rates from cache).
# Be sure to define the cache first before updating the rates.
moxb.update_rates
# Force update rates from remote and store in cache.
# Be sure to define the cache first before updating the rates.
# moxb.update_rates(true)
# Set money rounding mode.
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
# Set money default bank to Openexchangerates bank.
Money.default_bank = moxb
moxb = Money::Bank::OpenexchangeratesBank.new
# Returns the base currency set for all rates.
moxb.source
# Include also alternative rates.
moxb.alternatives = :all
# Get only alternative rates.
moxb.alternatives = :only
# Default is nil to get only regular rates.
# moxb.alternatives = nil
# Expires rates if the expiration time is reached.
moxb.expire_rates!
# Returns true if the expiration time is reached.
moxb.expired?
# Get the API source url.
moxb.source_url
# Get the rates timestamp of the last API request.
moxb.rates_timestamp
# Get the rates timestamp of loaded rates in memory.
moxb.rates_mem_timestamp
# Exchange 1000 cents (10.0 USD) to EUR
Money.new(1000, 'USD').exchange_to('EUR') # => #<Money fractional:89 currency:EUR>
Money.new(1000, 'USD').exchange_to('EUR').to_f # => 8.9
# Format
Money.new(1000, 'USD').exchange_to('EUR').format # => β¬8.90
# Get the rate
Money.default_bank.get_rate('USD', 'CAD') # => 0.9
See more on https://github.com/RubyMoney/money.
You can also use it in Rails with the gem money-rails.
require 'money/bank/openexchangerates_bank'
MoneyRails.configure do |config|
moxb = Money::Bank::OpenexchangeratesBank.new
moxb.access_key = 'your access_key from https://openexchangerates.com/signup'
moxb.update_rates
config.default_bank = moxb
end
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:
moxb.cache = Proc.new do |v|
key = 'money:openexchangerates_bank'
if v
Thread.current[key] = v
else
Thread.current[key]
end
end
The gem fetches all rates in a cache with USD as base currency. It's possible to compute the rate between any of the currencies by calculating a pair rate using base USD rate.
You can place your own key on a file or environment variable named TEST_ACCESS_KEY and then run:
bundle exec rake
- Gem money
- Gem money-currencylayer-bank
- Gem money-open-exchange-rates
- Gem money-historical-bank
- Gem money-currencylayer-bank
- Gem money-open-exchange-rates
- Gem money-historical-bank
- Gem currencylayer
- Gem eu_central_bank
- Gem nordea
- Gem google_currency
- See github.com/phlegx/money-openexchangerates-bank.
- Inspired by github.com/phlegx/money-currencylayer-bank.
- Fork it ( https://github.com/[your-username]/money-openexchangerates-bank/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
The MIT License
Copyright (c) 2022 Phlegx Systems Technologies GmbH