0.0
No commit activity in last 3 years
No release in over 3 years
This gem provides money-gem-compatible rates store connected to Open Exchange Rates API. It offers flexible caching mechanisms and OXR API quota protection.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 10.0
~> 3.0
~> 0.9.1

Runtime

~> 6.0
 Project Readme

MoneyExchange

Money gem integration with Open Exchange Rates API.

Development goals:

  • seamless integration with money gem
  • flexible caching
  • Open Exchange Rates API quota protection

Installation

Add this line to your application's Gemfile:

gem 'money-exchange'

And then execute:

$ bundle

Or install it yourself as:

$ gem install money-exchange

Usage

Create and configure MoneyExchange::Bank and set it as Money.default_bank.

require 'money_exchange'

bank = MoneyExchange::Bank.new(
  client: MoneyExchange::Client::OXR.new(ENV['OXR_APP_ID']),
  ttl: 24 * 60 * 60
)

Money.default_bank = bank

It will make Money use it transparently for currency conversion.

Money.new(10000, 'USD').exchange_to('EUR')
# => #<Money fractional:85 currency:EUR>

Caching

Exchange rates are cached internally on the rates store level for the number of seconds set with ttl option (defaults to 24 hours). If you want to use external cache store, e.g. the same as used in your Rails application, provide it as cache_store option.

MoneyExchange::Bank.new(
  cache_store: Rails.cache,
  ttl: 12 * 60 * 60
)

Open Exchange Rates API limits

OXR imposes usage limits in their API. In order to protect your quota, don't use the actual API in development or test environments. Instead configure the bank to use the local client preloaded with locally cached (sample) data.

Just save a response from https://openexchangerates.org/api/latest.json?app_id=OXR_APP_ID locally and load it when configuring the bank.

data = JSON.parse(File.read("path/to/oxr-response.json"))

MoneyExchange::Bank.new(
  client: MoneyExchange::Client::Local.new(data)
)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/szajbus/money-exchange. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the MoneyExchange project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.