Coder Social home page Coder Social logo

active_currency's Introduction

ActiveCurrency

CircleCI

Rails plugin to retrieve and store the currency rates daily to integrate with the money-rails gem.

Rationale

Storing the current currency rates in the database using ActiveCurrency provides the following advantages:

  • Lets you query for the currency rate you actually used in your application at any given time.
  • Does not need to call an API to get the rates when starting or restarting your web server.
  • Does not depend on the file system to store cached rates.
  • Choose how often you want to update the currency rates (daily for example).
  • Your users do not suffer the cost of making calls to the bank rates API.
  • Your app does not go down when the bank rates API does.

To fetch the current rate it uses your application cache instead of making a call to the database.

Usage

Store the current rate regularly by calling in a scheduled job (using something like sidekiq-scheduler, whenever, or active_scheduler) with the currencies you want to store:

ActiveCurrency::AddRates.call(%w[EUR USD])

You can then exchange money by using the Money gem helpers:

10.to_money('EUR').exchange_to('USD').cents

If you need to look up the previous currency rates:

ActiveCurrency::Rate.value_for('EUR', 'USD', 1.month.ago)
# => 1.151
ActiveCurrency::Rate.where(from: 'EUR', to: 'USD').pluck(:value)
# => [1.162, 1.162, 1.161, 1.161, 1.163, …]

Installation

Add these lines to your application's Gemfile:

# Store and retrieve the currency from the database.
gem 'active_currency'

And in config/initializers/money.rb:

MoneyRails.configure do |config|
  config.default_bank = ActiveCurrency::Bank.new
end

Then call bin/rake db:migrate to create the table that holds the currency rates and fill it for the first time.

Fetching rates

By defaut it uses the eu_central_bank to update the currency rates.

If you prefer another API, you can provide any Money-compatible bank when calling ActiveCurrency::AddRates. For example with the money-open-exchange-rates gem:

require 'money/bank/open_exchange_rates_bank'

bank = Money::Bank::OpenExchangeRatesBank.new(Money::RatesStore::Memory.new)
bank.app_id = '…'

ActiveCurrency::AddRates.call(%w[EUR USD], bank: bank)

The first currency you give to AddRates is considered the default currency against which other currency rates will be guessed if they are unavailable.

Tests

In your app test suite you may not want to have to fill your database to be able to exchange currencies.

For that, you can use a fake rate store in your rails_helper.rb:

MoneyRails.configure do |config|
  rate_store = Money::RatesStore::Memory.new.tap do |store|
    store.add_rate('USD', 'EUR', 1.5)
    store.add_rate('EUR', 'USD', 1.4)
  end
  config.default_bank = Money::Bank::VariableExchange.new(rate_store)
end

Contributing

Please file issues and pull requests on GitHub.

Development

Install:

BUNDLE_GEMFILE=Gemfile-rails6.0 bundle install

Launch specs and linters:

BUNDLE_GEMFILE=Gemfile-rails6.0 bin/rake

Release

Update CHANGELOG.md, update version in lib/active_currency/version.rb.

Then:

gem install bundler:1.17.2
BUNDLE_GEMFILE=Gemfile-rails4.2 bundle update
BUNDLE_GEMFILE=Gemfile-rails5.2 bundle update

gem install bundler:2.1.2
BUNDLE_GEMFILE=Gemfile-rails6.0 bundle update

git add CHANGELOG.md lib/active_currency/version.rb Gemfile-rails*.lock
git commit -m v`ruby -r./lib/active_currency/version <<< 'puts ActiveCurrency::VERSION'`
bin/rake release

License

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

active_currency's People

Contributors

sunny avatar williampollet avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.