Active Currency

A presentation at ParisRB Meetup in October 2018 in Paris, France by Sunny Ripert

Slide 1

Slide 1

Slide 2

Slide 2

2 / 27

Slide 3

Slide 3

3 / 27

Slide 4

Slide 4

EUR 4 / 27

Slide 5

Slide 5

USD GBP CAD … 5 / 27

Slide 6

Slide 6

💰

Represent and exchange Money. gem “money-rails”

6 / 27

Slide 7

Slide 7

42.to_money(:eur).exchange_to(:usd).to_f # => 48.8 7 / 27

Slide 8

Slide 8

config/initializers/money.rb MoneyRails.configure do |config| bank = Money::Bank::VariableExchange.new bank.add_rate(“EUR”, “USD”, 1.16) bank.add_rate(“USD”, “EUR”, 1 / 1.16) # … config.default_bank = bank end

8 / 27

Slide 9

Slide 9

🏦

Fetch the current currency. gem “eu_central_bank”

9 / 27

Slide 10

Slide 10

config/initializers/money.rb MoneyRails.configure do |config| bank = EuCentralBank.new bank.update_rates config.default_bank = bank end

10 / 27

Slide 11

Slide 11

11 / 27

Slide 12

Slide 12

12 / 27

Slide 13

Slide 13

13 / 27

Slide 14

Slide 14

14 / 27

Slide 15

Slide 15

15 / 27

Slide 16

Slide 16

16 / 27

Slide 17

Slide 17

class DailyRateUpdater include Sidekiq::Worker def perform # 1. Récupérer les taux de change du jour # 2. Les enregistrer en base de données end end 17 / 27

Slide 18

Slide 18

42.to_money(:eur).exchange_to(:usd).to_f # => 48.8 18 / 27

Slide 19

Slide 19

😍

Fetch and store the current currency. gem “active_currency”

19 / 27

Slide 20

Slide 20

config/initializers/money.rb MoneyRails.configure do |config| config.default_bank = ActiveCurrency::Bank.new end

20 / 27

Slide 21

Slide 21

class DailyRateUpdater include Sidekiq::Worker def perform ActiveCurrency::AddRates.call(%w[EUR USD]) end end 21 / 27

Slide 22

Slide 22

ActiveCurrency::Rate.value_for(“EUR”, “USD”, 1.day.ago) # => 1.162 22 / 27

Slide 23

Slide 23

✨ Avant ActiveCurrency Mise à jour Démarrage Tâche journalière Lecture Mémoire Base de données (+ cache) 23 / 27

Slide 24

Slide 24

Inconvénients Performances moins bonnes en lecture 😔 24 / 27

Slide 25

Slide 25

Avantages 📆 Choisir quand mettre à jour le taux Ne pas dépendre d’un appel API au démarrage Historiser les taux de conversion utilisés Utiliser les mêmes taux de change sur chaque serveur 📈 💥 🤝 25 / 27

Slide 26

Slide 26

active_currency v1.0 26 / 27

Slide 27

Slide 27

Merci ! github.com/sunny/active_currency @sunfox sur Twitter @sunfox@mamot.fr sur Mastodon KissKissBankBank.com cults3d.com Je vous ait dit qu’on recrutait ? Icône “Hello” de Sewon Park. CC BY Icône “Love” de Gregor Cresnar. CC BY Icône “Recovering heart” de Sergey Demushkin. CC BY Icône “Party” de Adrien Coquet. CC BY 27 / 27