active_currency 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb620b38733eb426ea053d9e540fcd1d94ddbf636f93b377e9295b8bd6ca0d41
4
- data.tar.gz: c6f2aa32d2ddee861583986f013c0864d7f9846691b05daecf391cc9eb89a225
3
+ metadata.gz: deaf57273ac8a06081edea671655192a53a560f5b4d2573818e1b3998d892f94
4
+ data.tar.gz: 78a851b36ae4a92fda99e8dd768685b0e5627069e75bbc07b349154823549c61
5
5
  SHA512:
6
- metadata.gz: 2c10136f43d38a1c998a302261d6c388cf9a491f46de97cf723ed13c112287e1a4c1af59e152d112489c914f16beb95d6208d5e54f62b06787242c8cb74b3c96
7
- data.tar.gz: 744a5f9343e62b907eb5dc05a740728a41963949b60ca762d99e78ba340d832e0124f2f9ed9165d52528d1af329c75e7a87c8564c65ea991f4822e899d41592f
6
+ metadata.gz: 502199ad5fa78e5b7710ef17e82de1e1f9e3ff68758a9193321c15524c636b2bec34c7f9839079122693f9c3044821aec37e862745279dbe34d9c2e45e97dfeb
7
+ data.tar.gz: 1f733721568ddacbc5a99335b761dee893191415a147c6639c0996f6d95a3df6fb373eebae298260906ddf704dbe9b3be0120857d845b9aa88ef0719ae06ca0e
data/README.md CHANGED
@@ -14,12 +14,13 @@ provides the following advantages:
14
14
  any given time.
15
15
  - Does not need to call an API to get the rates when starting or restarting
16
16
  your web server.
17
+ - Does not depend on the file system to store cached rates.
17
18
  - Choose how often you want to update the currency rates (daily for example).
18
19
  - Your users do not suffer the cost of making calls to the bank rates API.
19
20
  - Your app does not go down when the bank rates API does.
20
21
 
21
- To fetch the rates, it uses the [eu_central_bank] gem and uses your application
22
- cache when getting the current rate in order to save on database queries.
22
+ To fetch the *current* rate it uses your application cache instead of making
23
+ a call to the database.
23
24
 
24
25
  ## Usage
25
26
 
@@ -66,6 +67,23 @@ end
66
67
  Then call `bin/rake db:migrate` to create the table that holds
67
68
  the currency rates and fill it for the first time.
68
69
 
70
+ ## Fetching rates
71
+
72
+ By defaut it uses the [eu_central_bank] to update the currency rates.
73
+
74
+ If you prefer another API, you can provide any Money-compatible bank when
75
+ calling `ActiveCurrency::AddRates`. For example with the
76
+ [money-open-exchange-rates] gem:
77
+
78
+ ```rb
79
+ require 'money/bank/open_exchange_rates_bank'
80
+
81
+ bank = Money::Bank::OpenExchangeRatesBank.new(Money::RatesStore::Memory.new)
82
+ bank.app_id = '…'
83
+
84
+ ActiveCurrency::AddRates.call(%w[EUR USD], bank: bank)
85
+ ```
86
+
69
87
  ## Tests
70
88
 
71
89
  In your app test suite you may not want to have to fill your database to be
@@ -93,13 +111,13 @@ Please file issues and pull requests
93
111
  Install:
94
112
 
95
113
  ```sh
96
- BUNDLE_GEMFILE=Gemfile-rails5.2 bundle install
114
+ BUNDLE_GEMFILE=Gemfile-rails6.0 bundle install
97
115
  ```
98
116
 
99
117
  Launch specs and linters:
100
118
 
101
119
  ```sh
102
- BUNDLE_GEMFILE=Gemfile-rails5.2 bin/rake
120
+ BUNDLE_GEMFILE=Gemfile-rails6.0 bin/rake
103
121
  ```
104
122
 
105
123
  ## Release
@@ -109,13 +127,16 @@ Update `CHANGELOG.md`, update version in `lib/active_currency/version.rb`.
109
127
  Then:
110
128
 
111
129
  ```sh
112
- BUNDLE_GEMFILE=Gemfile-rails3.2 bundle install
113
- BUNDLE_GEMFILE=Gemfile-rails4.2 bundle install
114
- BUNDLE_GEMFILE=Gemfile-rails5.2 bundle install
115
- BUNDLE_GEMFILE=Gemfile-rails6.0 bundle install
130
+ gem install bundler:1.17.2
131
+ BUNDLE_GEMFILE=Gemfile-rails3.2 bundle update active_currency
132
+ BUNDLE_GEMFILE=Gemfile-rails4.2 bundle update active_currency
133
+
134
+ gem install bundler:2.1.2
135
+ BUNDLE_GEMFILE=Gemfile-rails5.2 bundle update active_currency
136
+ BUNDLE_GEMFILE=Gemfile-rails6.0 bundle update active_currency
116
137
 
117
138
  git add CHANGELOG.md lib/active_currency/version.rb Gemfile-rails*.lock
118
- git commit -m 'New version'
139
+ git commit -m v`ruby -r./lib/active_currency/version <<< 'puts ActiveCurrency::VERSION'`
119
140
  bin/rake release
120
141
  ```
121
142
 
@@ -125,3 +146,4 @@ The gem is available as open source under the terms of the
125
146
  [MIT License](http://opensource.org/licenses/MIT).
126
147
 
127
148
  [eu_central_bank]: https://github.com/RubyMoney/eu_central_bank
149
+ [money-open-exchange-rates]: https://github.com/spk/money-open-exchange-rates
@@ -3,51 +3,57 @@
3
3
  module ActiveCurrency
4
4
  # Store the latest currency rates.
5
5
  class AddRates
6
- def initialize(currencies)
6
+ def initialize(currencies, bank: EuCentralBank.new)
7
7
  @currencies = currencies
8
+ @bank = bank
8
9
  end
9
10
 
10
11
  def call
12
+ bank.update_rates
13
+
11
14
  other_currencies.each do |to|
12
- store_rate(from, to)
15
+ store_rate(to)
13
16
  end
14
17
  end
15
18
 
16
- def self.call(currencies)
17
- new(currencies).call
19
+ def self.call(currencies, *options)
20
+ new(currencies, *options).call
18
21
  end
19
22
 
20
23
  private
21
24
 
25
+ attr_accessor :bank
26
+
22
27
  def currencies
23
28
  @currencies.map(&:to_s).map(&:upcase)
24
29
  end
25
30
 
26
31
  def other_currencies
27
- currencies - [from]
32
+ currencies.drop(1)
28
33
  end
29
34
 
30
35
  def from
31
- 'EUR'
36
+ @from ||= currencies.first
32
37
  end
33
38
 
34
39
  def store
35
40
  @store ||= ActiveCurrency::RateStore.new
36
41
  end
37
42
 
38
- def bank
39
- @bank ||= EuCentralBank.new.tap(&:update_rates)
43
+ def store_rate(to)
44
+ rate, inverse = get_rates(to)
45
+
46
+ store.add_rate(from, to, rate)
47
+ store.add_rate(to, from, inverse)
40
48
  end
41
49
 
42
- def store_rate(from, to)
50
+ def get_rates(to)
43
51
  rate = bank.get_rate(from, to)
52
+ raise "Unknown rate between #{from} and #{to}" if rate.nil? || rate.zero?
44
53
 
45
- if rate.nil? || rate.zero?
46
- raise "Bank rate must be set but bank returned #{rate.inpsect}"
47
- end
54
+ inverse = bank.get_rate(to, from) || 1.fdiv(rate)
48
55
 
49
- store.add_rate(from, to, rate)
50
- store.add_rate(to, from, 1.fdiv(rate))
56
+ [rate, inverse]
51
57
  end
52
58
  end
53
59
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveCurrency
4
- VERSION = '1.0.2'
4
+ VERSION = '1.1.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_currency
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sunny Ripert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-05 00:00:00.000000000 Z
11
+ date: 2019-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: sqlite3
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 1.3.6
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 1.3.6
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec-rails
71
71
  requirement: !ruby/object:Gem::Requirement