OpenExchangeRates
Ruby gem for currency conversion based on Open Exchange Rates API - free / open source hourly-updated currency data for everybody
Accuracy
Please see https://github.com/josscrowcroft/open-exchange-rates#accuracy
Installation
Add this line to your application's Gemfile:
gem 'open_exchange_rates'
And then execute:
$ bundle
Or install it yourself as:
$ gem install open_exchange_rates
Configuration
You will need App ID to use OpenExchangeRates, you can get one here for free.
Option 1
Set OPEN_EXCHANGE_RATES_APP_ID environment variable and it will be used automatically. If you are using foreman for example just add it to your .env file like this
OPEN_EXCHANGE_RATES_APP_ID=YourAppID
Option 2
OpenExchangeRates.configure do |config|
config.app_id = "YourAppID"
end
If you are using Rails good place to add this is config/initializers/open_exchange_rates.rb
Option 3
Pass it on initialization
fx = OpenExchangeRates::Rates.new(:app_id => "YourAppID")
Usage
Start by creating OpenExchangeRates::Rates instance
fx = OpenExchangeRates::Rates.new
Convert between currencies using current rates
fx.convert(123.45, :from => "USD", :to => "EUR") # => 99.87
Convert between currencies on specific date
fx.convert(123.45, :from => "USD", :to => "EUR", :on => "2012-05-10") # => 95.47
Get current exchange rate
fx.exchange_rate(:from => "USD", :to => "EUR") # => 0.808996
Get exchange rate on specific date
fx.exchange_rate(:from => "USD", :to => "EUR", :on => "2012-05-10") # => 0.773329
Default currency
If you omit :from or :to option conversion will be related to base currency. USD is set as base currency (plan is to add this as config option in the near future).
fx.convert(123.45, :to => "EUR") # => 99.87 EUR
fx.convert(123.45, :from => "EUR") # => 152.51 USD
fx.exchange_rate(:to => "EUR") # => 0.808996
fx.exchange_rate(:from => "EUR") # => 1.235414
TODO
- ability to set default currency (USD is currently always set as base currency)
ability to pass Date as :on option (only 'yyyy-mm-dd' works currently)- write some docs
- write more test for specific situations (
invalid date, ...)
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Running tests
- Copy env.example to .env
cp env.example .env
- Open
.env
and enter your API ID - Run
rake
Licence and Terms
This project rocks and uses MIT-LICENSE.
Please check Open Exchange Rates API license and terms also.