money-open-exchange-rates 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca0a9252d989a1593a4dc9af24c8789f17eb9ec15cc2e0bfa09b2d44c719e8b1
4
- data.tar.gz: d45842e216378fb1dd04d477b1c203482f955d9607397eb570c0612606211ce8
3
+ metadata.gz: 3da9d4f9bab38eaccb4fb18b6c9349006fdaadc4d90ffe4cd241223040f5a3df
4
+ data.tar.gz: 34c5b0d94aba549eb659f1d7db6f232a17aa73409f554f7126a63a8057ec7628
5
5
  SHA512:
6
- metadata.gz: c69afba4a53c236836ea72c27e4e095c48cc09fb95584e14f9912e6e7c0de43fedf03204e1a62a56558d054b83bc32b18ddc1cbbff8a2672917765e9774d136a
7
- data.tar.gz: 97fc884e5e97ab688d7d6480a88540e5a308c1d838b85ad0c19c63c41b6f73230f92b000ed09daa618727a4bd2a22e714b1006966339545013d956da749e6b50
6
+ metadata.gz: 513b47e7cb4d9c05b80e2614956aa733ab07ec9d50f3dde8c008fdf1c522d8c17d0184fcb2f9e0f6d5c3aa03edfb193e9a4454974f7c6d0299eec2cf6baca4c2
7
+ data.tar.gz: a10d04d150d208e1e8b5fa43a7a656622f675b7284c53ce28c21192196bdaed17eef59dda1530fe83c8255e1412008ccf3cde55c00e8491c80b79144e39437a7
data/History.md CHANGED
@@ -1,4 +1,10 @@
1
1
 
2
+ v1.1.0 / 2018-03-28
3
+ ===================
4
+
5
+ * Force refresh when ttl is expire and cache invalid (issue #47)
6
+ * Less strict dependency version for money/monetize
7
+
2
8
  v1.0.2 / 2018-03-27
3
9
  ===================
4
10
 
data/README.md CHANGED
@@ -43,6 +43,7 @@ gem install money-open-exchange-rates
43
43
  ~~~ ruby
44
44
  require 'money/bank/open_exchange_rates_bank'
45
45
  oxr = Money::Bank::OpenExchangeRatesBank.new
46
+ # see https://github.com/spk/money-open-exchange-rates#cache for more info
46
47
  oxr.cache = 'path/to/file/cache.json'
47
48
  oxr.app_id = 'your app id from https://openexchangerates.org/signup'
48
49
  oxr.update_rates
@@ -65,7 +66,7 @@ oxr.source = 'USD'
65
66
  # rates. By default, false is used
66
67
  # see: https://docs.openexchangerates.org/docs/alternative-currencies
67
68
  oxr.show_alternative = true
68
-
69
+ # (optional)
69
70
  # Store in cache
70
71
  # Force rates storage in cache, this is done automaticly after TTL is expire.
71
72
  # If you are using unicorn-worker-killer gem or on Heroku like platform,
@@ -78,6 +79,8 @@ Money.default_bank = oxr
78
79
  Money.default_bank.get_rate('USD', 'CAD')
79
80
  ~~~
80
81
 
82
+ ## Cache
83
+
81
84
  You can also provide a `Proc` as a cache to provide your own caching mechanism
82
85
  perhaps with Redis or just a thread safe `Hash` (global). For example:
83
86
 
@@ -96,33 +99,27 @@ With `Rails` cache example:
96
99
 
97
100
  ~~~ ruby
98
101
  OXR_CACHE_KEY = 'money:exchange_rates'.freeze
99
- OXR_CACHE_TTL = 86400
100
- # using same ttl with refreshing current rates and cache
101
- oxr.ttl_in_seconds = OXR_CACHE_TTL
102
+ oxr.ttl_in_seconds = 86400
102
103
  oxr.cache = Proc.new do |text|
103
- if text && !Rails.cache.exist?(OXR_CACHE_KEY)
104
- Rails.cache.write(OXR_CACHE_KEY, text, expires_in: OXR_CACHE_TTL)
104
+ if text
105
+ Rails.cache.write(OXR_CACHE_KEY, text)
105
106
  else
106
107
  Rails.cache.read(OXR_CACHE_KEY)
107
108
  end
108
109
  end
109
110
  ~~~
110
111
 
111
- Unknown pair rates are transparently calculated: using inverse rate (if known),
112
- or using base currency rate to both currencies forming the pair.
113
-
114
112
  ## Full example configuration initializer with Rails and cache
115
113
 
116
114
  ~~~ ruby
117
115
  require 'money/bank/open_exchange_rates_bank'
118
116
 
119
117
  OXR_CACHE_KEY = 'money:exchange_rates'.freeze
120
- OXR_CACHE_TTL = 86400
121
118
  oxr = Money::Bank::OpenExchangeRatesBank.new
122
- oxr.ttl_in_seconds = OXR_CACHE_TTL
119
+ oxr.ttl_in_seconds = 86400
123
120
  oxr.cache = Proc.new do |text|
124
- if text && !Rails.cache.exist?(OXR_CACHE_KEY)
125
- Rails.cache.write(OXR_CACHE_KEY, text, expires_in: OXR_CACHE_TTL)
121
+ if text
122
+ Rails.cache.write(OXR_CACHE_KEY, text)
126
123
  else
127
124
  Rails.cache.read(OXR_CACHE_KEY)
128
125
  end
@@ -133,6 +130,11 @@ oxr.update_rates
133
130
  Money.default_bank = oxr
134
131
  ~~~
135
132
 
133
+ ## Pair rates
134
+
135
+ Unknown pair rates are transparently calculated: using inverse rate (if known),
136
+ or using base currency rate to both currencies forming the pair.
137
+
136
138
  ## Tests
137
139
 
138
140
  ~~~
@@ -177,6 +177,7 @@ class Money
177
177
  def expire_rates
178
178
  return unless ttl_in_seconds
179
179
  return if rates_expiration > Time.now
180
+ read_from_url
180
181
  update_rates
181
182
  refresh_rates_expiration
182
183
  end
@@ -251,11 +252,12 @@ class Money
251
252
  #
252
253
  # @return [String] Raw string from file or cache proc
253
254
  def read_from_cache
254
- if cache.is_a?(Proc)
255
- cache.call(nil)
256
- elsif cache.is_a?(String) && File.exist?(cache)
257
- open(cache).read
258
- end
255
+ result = if cache.is_a?(Proc)
256
+ cache.call(nil)
257
+ elsif cache.is_a?(String) && File.exist?(cache)
258
+ open(cache).read
259
+ end
260
+ result if valid_rates?(result)
259
261
  end
260
262
 
261
263
  # Read from url
@@ -276,6 +278,7 @@ class Money
276
278
  # @param [String] text is JSON content
277
279
  # @return [Boolean] valid or not
278
280
  def valid_rates?(text)
281
+ return false unless text
279
282
  parsed = JSON.parse(text)
280
283
  parsed && parsed.key?('rates')
281
284
  rescue JSON::ParserError
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Module for version constant
4
4
  module OpenExchangeRatesBank
5
- VERSION = '1.0.2'.freeze
5
+ VERSION = '1.1.0'.freeze
6
6
  end
@@ -306,6 +306,15 @@ describe Money::Bank::OpenExchangeRatesBank do
306
306
  end
307
307
  end
308
308
 
309
+ it 'should save rates and refresh it when cache is invalid' do
310
+ subject.get_rate('USD', 'EUR').must_equal @old_usd_eur_rate
311
+ Timecop.freeze(Time.now + 1001) do
312
+ @global_rates = []
313
+ subject.get_rate('USD', 'EUR').must_equal @new_usd_eur_rate
314
+ @global_rates.wont_be_empty
315
+ end
316
+ end
317
+
309
318
  it 'updates the next expiration time' do
310
319
  Timecop.freeze(Time.now + 1001) do
311
320
  exp_time = Time.now + 1000
@@ -318,8 +327,8 @@ describe Money::Bank::OpenExchangeRatesBank do
318
327
  describe 'when the ttl has not expired' do
319
328
  it 'not should update the rates' do
320
329
  exp_time = subject.rates_expiration
330
+ dont_allow(subject).read_from_url
321
331
  dont_allow(subject).update_rates
322
- dont_allow(subject).save_rates
323
332
  dont_allow(subject).refresh_rates_expiration
324
333
  subject.expire_rates
325
334
  subject.rates_expiration.must_equal exp_time
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money-open-exchange-rates
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
  - Laurent Arnoud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-27 00:00:00.000000000 Z
11
+ date: 2018-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money
@@ -16,28 +16,34 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '6.8'
19
+ version: '6.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '6.8'
26
+ version: '6.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: monetize
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.1
34
+ - - "<"
32
35
  - !ruby/object:Gem::Version
33
- version: '1.5'
36
+ version: '2'
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
38
- - - "~>"
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.1
44
+ - - "<"
39
45
  - !ruby/object:Gem::Version
40
- version: '1.5'
46
+ version: '2'
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: rake
43
49
  requirement: !ruby/object:Gem::Requirement