active_currency 1.1.0 → 1.2.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 +4 -4
- data/README.md +12 -4
- data/app/models/active_currency/rate.rb +5 -2
- data/lib/active_currency/add_rates.rb +28 -24
- data/lib/active_currency/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfc5f4cd4fb66fb4bf8792dacfd16014f2bb5bb44d603a4b5cd72985999b1729
|
4
|
+
data.tar.gz: 20995e8b1c9b3d4935bc1e6f1d9a2832fd0cd703e4fcb60de44b413e78995dae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8804e7be48d851288b48efb05d7dbba0c57b62d3b71937d5e375c98de4485bbcab9a2de042abe4614e37d77cfc0a709b310faca4d6245156373c4924c4311b80
|
7
|
+
data.tar.gz: 3843245a24efe3d2eec8ae19a834b4d7e1e7b82d48c7203c5f41326e894c42228b524c6379f38beee9732448587b58392f814894dfbc75a1b5199d344a9290a0
|
data/README.md
CHANGED
@@ -84,6 +84,9 @@ bank.app_id = '…'
|
|
84
84
|
ActiveCurrency::AddRates.call(%w[EUR USD], bank: bank)
|
85
85
|
```
|
86
86
|
|
87
|
+
The first currency you give to `AddRates` is considered the default currency
|
88
|
+
against which other currency rates will be guessed if they are unavailable.
|
89
|
+
|
87
90
|
## Tests
|
88
91
|
|
89
92
|
In your app test suite you may not want to have to fill your database to be
|
@@ -128,12 +131,17 @@ Then:
|
|
128
131
|
|
129
132
|
```sh
|
130
133
|
gem install bundler:1.17.2
|
131
|
-
BUNDLE_GEMFILE=Gemfile-rails3.2 bundle update
|
132
|
-
BUNDLE_GEMFILE=Gemfile-rails4.2 bundle update
|
134
|
+
BUNDLE_GEMFILE=Gemfile-rails3.2 bundle update
|
135
|
+
BUNDLE_GEMFILE=Gemfile-rails4.2 bundle update
|
133
136
|
|
134
137
|
gem install bundler:2.1.2
|
135
|
-
BUNDLE_GEMFILE=Gemfile-rails5.2 bundle update
|
136
|
-
BUNDLE_GEMFILE=Gemfile-rails6.0 bundle update
|
138
|
+
BUNDLE_GEMFILE=Gemfile-rails5.2 bundle update
|
139
|
+
BUNDLE_GEMFILE=Gemfile-rails6.0 bundle update
|
140
|
+
|
141
|
+
BUNDLE_GEMFILE=Gemfile-rails3.2 bundle exec rake
|
142
|
+
BUNDLE_GEMFILE=Gemfile-rails4.2 bundle exec rake
|
143
|
+
BUNDLE_GEMFILE=Gemfile-rails5.2 bundle exec rake
|
144
|
+
BUNDLE_GEMFILE=Gemfile-rails6.0 bundle exec rake
|
137
145
|
|
138
146
|
git add CHANGELOG.md lib/active_currency/version.rb Gemfile-rails*.lock
|
139
147
|
git commit -m v`ruby -r./lib/active_currency/version <<< 'puts ActiveCurrency::VERSION'`
|
@@ -11,10 +11,13 @@ module ActiveCurrency
|
|
11
11
|
}
|
12
12
|
|
13
13
|
def self.value_for(from, to, date = nil)
|
14
|
-
|
14
|
+
from = Money::Currency.new(from)
|
15
|
+
to = Money::Currency.new(to)
|
16
|
+
return 1 if from == to
|
15
17
|
|
18
|
+
scope = date ? before(date) : all_scope
|
16
19
|
scope
|
17
|
-
.where(from: from, to: to)
|
20
|
+
.where(from: from.iso_code, to: to.iso_code)
|
18
21
|
.order(:created_at)
|
19
22
|
.last
|
20
23
|
&.value
|
@@ -4,16 +4,18 @@ module ActiveCurrency
|
|
4
4
|
# Store the latest currency rates.
|
5
5
|
class AddRates
|
6
6
|
def initialize(currencies, bank: EuCentralBank.new)
|
7
|
-
@currencies = currencies
|
7
|
+
@currencies = currencies.map(&:to_s).map(&:upcase)
|
8
8
|
@bank = bank
|
9
9
|
end
|
10
10
|
|
11
11
|
def call
|
12
12
|
bank.update_rates
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
rates_hash.each do |(from, to), rate|
|
15
|
+
store.add_rate(from, to, rate)
|
16
16
|
end
|
17
|
+
|
18
|
+
nil
|
17
19
|
end
|
18
20
|
|
19
21
|
def self.call(currencies, *options)
|
@@ -22,38 +24,40 @@ module ActiveCurrency
|
|
22
24
|
|
23
25
|
private
|
24
26
|
|
25
|
-
attr_accessor :bank
|
26
|
-
|
27
|
-
def currencies
|
28
|
-
@currencies.map(&:to_s).map(&:upcase)
|
29
|
-
end
|
30
|
-
|
31
|
-
def other_currencies
|
32
|
-
currencies.drop(1)
|
33
|
-
end
|
34
|
-
|
35
|
-
def from
|
36
|
-
@from ||= currencies.first
|
37
|
-
end
|
27
|
+
attr_accessor :bank, :currencies
|
38
28
|
|
39
29
|
def store
|
40
30
|
@store ||= ActiveCurrency::RateStore.new
|
41
31
|
end
|
42
32
|
|
43
|
-
def
|
44
|
-
|
33
|
+
def rates_hash
|
34
|
+
currencies.each_with_object({}) do |from, hash|
|
35
|
+
currencies.each do |to|
|
36
|
+
next if from == to
|
45
37
|
|
46
|
-
|
47
|
-
|
38
|
+
hash[[from, to]] = get_rate(hash, from, to)
|
39
|
+
end
|
40
|
+
end
|
48
41
|
end
|
49
42
|
|
50
|
-
def
|
43
|
+
def get_rate(hash, from, to)
|
51
44
|
rate = bank.get_rate(from, to)
|
52
|
-
|
45
|
+
return rate if rate
|
46
|
+
|
47
|
+
# Inverse rate (not so good)
|
48
|
+
inverse = hash[[to, from]]
|
49
|
+
return 1.fdiv(inverse) if inverse
|
53
50
|
|
54
|
-
|
51
|
+
# Rate going through the first currency (desperate)
|
52
|
+
from_main = hash[[from, main_currency]]
|
53
|
+
to_main = hash[[main_currency, to]]
|
54
|
+
return from_main * to_main if from_main && to_main
|
55
|
+
|
56
|
+
raise "Unknown rate between #{from} and #{to}"
|
57
|
+
end
|
55
58
|
|
56
|
-
|
59
|
+
def main_currency
|
60
|
+
currencies.first
|
57
61
|
end
|
58
62
|
end
|
59
63
|
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.
|
4
|
+
version: 1.2.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:
|
11
|
+
date: 2020-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rubocop-rails
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
description: Store your currency.
|
140
154
|
email:
|
141
155
|
- sunny@sunfox.org
|