money-open-exchange-rates 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -1
- data/History.md +26 -0
- data/LICENSE +1 -1
- data/README.md +47 -11
- data/lib/money/bank/open_exchange_rates_bank.rb +34 -21
- data/lib/open_exchange_rates_bank/version.rb +1 -1
- data/test/integration/Gemfile +2 -0
- data/test/integration/api.rb +3 -1
- data/test/open_exchange_rates_bank_test.rb +108 -57
- data/test/test_helper.rb +4 -5
- metadata +6 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 213847dd55f3bbc717a20dc056e61510f4b00ede31bce5b1c7022b16eedc875e
|
4
|
+
data.tar.gz: e245e87886f4c2fc2d8ae6fcf25c80d5f7797ffc3d877eaafab18abd2f911050
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a63501cb0baad531f152bedc2a232697f9c64da9bfeef9dd576ecbcda9dceaed83aba902f272c5cd4d44af20e9a55bd7654e1c244a60a5620b9db6f033c6f4b
|
7
|
+
data.tar.gz: 0c5466657cac8fb4e826d7d9e24ac354778eca529ac156385d3dc44070ed78d3e9278ffc2e17ccad705311195fdaa2822d17855cbcd2e721d90cab37ebe1c0c2
|
data/Gemfile
CHANGED
data/History.md
CHANGED
@@ -1,4 +1,30 @@
|
|
1
1
|
|
2
|
+
1.4.0 / 2020-09-11
|
3
|
+
==================
|
4
|
+
|
5
|
+
* Fix jruby test for cross courses
|
6
|
+
* Fix deprecation global use of must_equal
|
7
|
+
* Merge pull request #66 from @korun / clear-cross-courses
|
8
|
+
* Update rates inside transaction, to prevent RC issues
|
9
|
+
* Merge pull request #65 from @korun / ensure-file-closed
|
10
|
+
* Fix unclosed file descriptor after read_from_cache
|
11
|
+
* Merge pull request #64 from @anton-smagin / clear-cross-courses
|
12
|
+
* Set old rates to nil on #update_rates before set new one to expire pair rates using base
|
13
|
+
* Add coverage stage on ci
|
14
|
+
* Remove Ruby 2.3 support
|
15
|
+
* Switch to gitlab ci
|
16
|
+
* Update README
|
17
|
+
* Fix rubocop to 0.76.0 and remove minitest-focus
|
18
|
+
* Update gitlab ci gem install does not have --no-ri anymore
|
19
|
+
* README add more doc about update_rates
|
20
|
+
* Fix deprecation for minitest 6
|
21
|
+
* Fix flaky test about Money::Bank::NoAppId
|
22
|
+
* Support Pathname for cache
|
23
|
+
* Update rubocop to 0.76
|
24
|
+
* Remove Ruby 2.2 support and update rubocop
|
25
|
+
* Update travis list
|
26
|
+
* README: improve refresh doc
|
27
|
+
|
2
28
|
1.3.0 / 2019-01-20
|
3
29
|
==================
|
4
30
|
|
data/LICENSE
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
The MIT License
|
2
2
|
|
3
|
-
Copyright (c) 2011-
|
3
|
+
Copyright (c) 2011-2020 Laurent Arnoud <laurent@spkdev.net>
|
4
4
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining
|
6
6
|
a copy of this software and associated documentation files (the
|
data/README.md
CHANGED
@@ -30,15 +30,15 @@ gem 'money-open-exchange-rates'
|
|
30
30
|
|
31
31
|
And then execute:
|
32
32
|
|
33
|
-
|
33
|
+
```
|
34
34
|
bundle
|
35
|
-
|
35
|
+
```
|
36
36
|
|
37
37
|
Or install it yourself as:
|
38
38
|
|
39
|
-
|
39
|
+
```
|
40
40
|
gem install money-open-exchange-rates
|
41
|
-
|
41
|
+
```
|
42
42
|
|
43
43
|
## Usage
|
44
44
|
|
@@ -47,8 +47,14 @@ require 'money/bank/open_exchange_rates_bank'
|
|
47
47
|
|
48
48
|
# Memory store per default; for others just pass as argument a class like
|
49
49
|
# explained in https://github.com/RubyMoney/money#exchange-rate-stores
|
50
|
-
oxr = Money::Bank::OpenExchangeRatesBank.new
|
50
|
+
oxr = Money::Bank::OpenExchangeRatesBank.new(Money::RatesStore::Memory.new)
|
51
51
|
oxr.app_id = 'your app id from https://openexchangerates.org/signup'
|
52
|
+
|
53
|
+
# Update the rates for the current rates storage
|
54
|
+
# If the storage is memory you will have to restart the server to be taken into
|
55
|
+
# account.
|
56
|
+
# If the storage is a database, file, this can be added to
|
57
|
+
# crontab/worker/scheduler `Money.default_bank.update_rates`
|
52
58
|
oxr.update_rates
|
53
59
|
|
54
60
|
# (optional)
|
@@ -97,7 +103,8 @@ oxr.refresh_rates
|
|
97
103
|
# (optional)
|
98
104
|
# Force refresh rates cache and store on the fly when ttl is expired
|
99
105
|
# This will slow down request on get_rate, so use at your on risk, if you don't
|
100
|
-
# want to setup crontab/worker/scheduler for your application
|
106
|
+
# want to setup crontab/worker/scheduler for your application.
|
107
|
+
# Again this is not safe with multiple servers and could increase API usage.
|
101
108
|
oxr.force_refresh_rate_on_expire = true
|
102
109
|
|
103
110
|
Money.default_bank = oxr
|
@@ -105,6 +112,31 @@ Money.default_bank = oxr
|
|
105
112
|
Money.default_bank.get_rate('USD', 'CAD')
|
106
113
|
```
|
107
114
|
|
115
|
+
## Refresh rates
|
116
|
+
|
117
|
+
### With [whenever](https://github.com/javan/whenever)
|
118
|
+
|
119
|
+
``` ruby
|
120
|
+
every :hour do
|
121
|
+
runner "Money.default_bank.refresh_rates"
|
122
|
+
# you will have to restart the server if you are using memory rate store
|
123
|
+
runner "Money.default_bank.update_rates"
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
127
|
+
### With rake task
|
128
|
+
|
129
|
+
``` ruby
|
130
|
+
namespace :open_exchange_rates do
|
131
|
+
desc "Refresh rates from cache and update rates"
|
132
|
+
task :refresh_rates => :environment do
|
133
|
+
Money.default_bank.refresh_rates
|
134
|
+
# you will have to restart the server if you are using memory rate store
|
135
|
+
Money.default_bank.update_rates
|
136
|
+
end
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
108
140
|
## Cache
|
109
141
|
|
110
142
|
You can also provide a `Proc` as a cache to provide your own caching mechanism
|
@@ -162,11 +194,15 @@ end
|
|
162
194
|
oxr.app_id = ENV['OXR_API_KEY']
|
163
195
|
oxr.show_alternative = true
|
164
196
|
oxr.prettyprint = false
|
197
|
+
|
198
|
+
# This can be removed if you have data to avoid http call on boot for production
|
165
199
|
oxr.update_rates
|
166
200
|
|
167
201
|
Money.default_bank = oxr
|
168
202
|
```
|
169
203
|
|
204
|
+
See also how to [refresh and update rates](#refresh-rates)
|
205
|
+
|
170
206
|
### Tests
|
171
207
|
|
172
208
|
To avoid to hit the API we can use the cache option with a saved file like this:
|
@@ -194,9 +230,9 @@ or using base currency rate to both currencies forming the pair.
|
|
194
230
|
|
195
231
|
## Tests
|
196
232
|
|
197
|
-
|
233
|
+
```
|
198
234
|
bundle exec rake
|
199
|
-
|
235
|
+
```
|
200
236
|
|
201
237
|
## Refs
|
202
238
|
|
@@ -213,12 +249,12 @@ See [GitHub](https://github.com/spk/money-open-exchange-rates/graphs/contributor
|
|
213
249
|
|
214
250
|
The MIT License
|
215
251
|
|
216
|
-
Copyright © 2011-
|
252
|
+
Copyright © 2011-2020 Laurent Arnoud <laurent@spkdev.net>
|
217
253
|
|
218
254
|
---
|
219
|
-
[![Build](https://img.shields.io/
|
255
|
+
[![Build](https://img.shields.io/gitlab/pipeline/spkdev/money-open-exchange-rates/master)](https://gitlab.com/spkdev/money-open-exchange-rates/-/commits/master)
|
256
|
+
[![Coverage](https://gitlab.com/spkdev/money-open-exchange-rates/badges/master/coverage.svg)](https://gitlab.com/spkdev/money-open-exchange-rates/-/commits/master)
|
220
257
|
[![Version](https://img.shields.io/gem/v/money-open-exchange-rates.svg)](https://rubygems.org/gems/money-open-exchange-rates)
|
221
258
|
[![Documentation](https://img.shields.io/badge/doc-rubydoc-blue.svg)](http://www.rubydoc.info/gems/money-open-exchange-rates)
|
222
259
|
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT "MIT")
|
223
|
-
[![Coverage Status](https://img.shields.io/coveralls/github/spk/money-open-exchange-rates.svg)](https://coveralls.io/github/spk/money-open-exchange-rates?branch=master)
|
224
260
|
[![Inline docs](https://inch-ci.org/github/spk/money-open-exchange-rates.svg?branch=master)](http://inch-ci.org/github/spk/money-open-exchange-rates)
|
@@ -7,7 +7,7 @@ require 'json'
|
|
7
7
|
require File.expand_path('../../open_exchange_rates_bank/version', __dir__)
|
8
8
|
|
9
9
|
# Money gem class
|
10
|
-
# rubocop:disable ClassLength
|
10
|
+
# rubocop:disable Metrics/ClassLength
|
11
11
|
class Money
|
12
12
|
# https://github.com/RubyMoney/money#exchange-rate-stores
|
13
13
|
module Bank
|
@@ -20,16 +20,16 @@ class Money
|
|
20
20
|
# OpenExchangeRatesBank base class
|
21
21
|
class OpenExchangeRatesBank < Money::Bank::VariableExchange
|
22
22
|
VERSION = ::OpenExchangeRatesBank::VERSION
|
23
|
-
BASE_URL = 'https://openexchangerates.org/api/'
|
23
|
+
BASE_URL = 'https://openexchangerates.org/api/'
|
24
24
|
|
25
25
|
# OpenExchangeRates urls
|
26
26
|
OER_URL = URI.join(BASE_URL, 'latest.json')
|
27
27
|
OER_HISTORICAL_URL = URI.join(BASE_URL, 'historical/')
|
28
28
|
|
29
29
|
# Default base currency "base": "USD"
|
30
|
-
OE_SOURCE = 'USD'
|
31
|
-
RATES_KEY = 'rates'
|
32
|
-
TIMESTAMP_KEY = 'timestamp'
|
30
|
+
OE_SOURCE = 'USD'
|
31
|
+
RATES_KEY = 'rates'
|
32
|
+
TIMESTAMP_KEY = 'timestamp'
|
33
33
|
|
34
34
|
# As of the end of August 2012 all requests to the Open Exchange Rates
|
35
35
|
# API must have a valid app_id
|
@@ -174,13 +174,16 @@ class Money
|
|
174
174
|
#
|
175
175
|
# @return [Array] Array of exchange rates
|
176
176
|
def update_rates
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
177
|
+
store.transaction do
|
178
|
+
clear_rates!
|
179
|
+
exchange_rates.each do |exchange_rate|
|
180
|
+
rate = exchange_rate.last
|
181
|
+
currency = exchange_rate.first
|
182
|
+
next unless Money::Currency.find(currency)
|
183
|
+
|
184
|
+
set_rate(source, currency, rate)
|
185
|
+
set_rate(currency, source, 1.0 / rate)
|
186
|
+
end
|
184
187
|
end
|
185
188
|
end
|
186
189
|
|
@@ -255,9 +258,7 @@ class Money
|
|
255
258
|
str = "#{str}&base=#{source}" unless source == OE_SOURCE
|
256
259
|
str = "#{str}&show_alternative=#{show_alternative}"
|
257
260
|
str = "#{str}&prettyprint=#{prettyprint}"
|
258
|
-
|
259
|
-
str = "#{str}&symbols=#{symbols.join(',')}"
|
260
|
-
end
|
261
|
+
str = "#{str}&symbols=#{symbols.join(',')}" if symbols&.is_a?(Array)
|
261
262
|
str
|
262
263
|
end
|
263
264
|
|
@@ -300,6 +301,7 @@ class Money
|
|
300
301
|
|
301
302
|
# Store the provided text data by calling the proc method provided
|
302
303
|
# for the cache, or write to the cache file.
|
304
|
+
# Can raise InvalidCache
|
303
305
|
#
|
304
306
|
# @example
|
305
307
|
# oxr.store_in_cache("{\"rates\": {\"AED\": 3.67304}}")
|
@@ -309,10 +311,12 @@ class Money
|
|
309
311
|
def store_in_cache(text)
|
310
312
|
if cache.is_a?(Proc)
|
311
313
|
cache.call(text)
|
312
|
-
elsif cache.is_a?(String)
|
313
|
-
File.open(cache, 'w') do |f|
|
314
|
+
elsif cache.is_a?(String) || cache.is_a?(Pathname)
|
315
|
+
File.open(cache.to_s, 'w') do |f|
|
314
316
|
f.write(text)
|
315
317
|
end
|
318
|
+
else
|
319
|
+
raise InvalidCache
|
316
320
|
end
|
317
321
|
end
|
318
322
|
|
@@ -322,8 +326,8 @@ class Money
|
|
322
326
|
def read_from_cache
|
323
327
|
result = if cache.is_a?(Proc)
|
324
328
|
cache.call(nil)
|
325
|
-
elsif
|
326
|
-
File.
|
329
|
+
elsif File.exist?(cache.to_s)
|
330
|
+
File.read(cache)
|
327
331
|
end
|
328
332
|
result if valid_rates?(result)
|
329
333
|
end
|
@@ -357,7 +361,7 @@ class Money
|
|
357
361
|
return false unless text
|
358
362
|
|
359
363
|
parsed = JSON.parse(text)
|
360
|
-
parsed
|
364
|
+
parsed&.key?(RATES_KEY) && parsed&.key?(TIMESTAMP_KEY)
|
361
365
|
rescue JSON::ParserError
|
362
366
|
false
|
363
367
|
end
|
@@ -413,7 +417,16 @@ class Money
|
|
413
417
|
add_rate(from_currency, to_currency, rate)
|
414
418
|
rate
|
415
419
|
end
|
420
|
+
|
421
|
+
# Clears cached rates in store
|
422
|
+
#
|
423
|
+
# @return [Hash] All rates from store as Hash
|
424
|
+
def clear_rates!
|
425
|
+
store.each_rate do |iso_from, iso_to|
|
426
|
+
add_rate(iso_from, iso_to, nil)
|
427
|
+
end
|
428
|
+
end
|
416
429
|
end
|
417
430
|
end
|
418
431
|
end
|
419
|
-
# rubocop:enable ClassLength
|
432
|
+
# rubocop:enable Metrics/ClassLength
|
data/test/integration/Gemfile
CHANGED
data/test/integration/api.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'json'
|
2
4
|
require 'money/bank/open_exchange_rates_bank'
|
3
5
|
|
4
|
-
ERROR_MSG = 'Integration test failed!'
|
6
|
+
ERROR_MSG = 'Integration test failed!'
|
5
7
|
cache_path = '/tmp/latest.json'
|
6
8
|
to_currency = 'CAD'
|
7
9
|
app_id = ENV['OXR_APP_ID']
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
2
4
|
|
3
5
|
# rubocop:disable Metrics/BlockLength
|
@@ -14,6 +16,9 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
14
16
|
let(:temp_cache_path) do
|
15
17
|
data_file('tmp.json')
|
16
18
|
end
|
19
|
+
let(:temp_cache_pathname) do
|
20
|
+
Pathname.new('test/data/tmp.json')
|
21
|
+
end
|
17
22
|
let(:oer_latest_path) do
|
18
23
|
data_file('latest.json')
|
19
24
|
end
|
@@ -35,12 +40,12 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
35
40
|
describe 'without rates' do
|
36
41
|
it 'able to exchange a money to its own currency even without rates' do
|
37
42
|
money = Money.new(0, 'USD')
|
38
|
-
subject.exchange_with(money, 'USD').must_equal money
|
43
|
+
_(subject.exchange_with(money, 'USD')).must_equal money
|
39
44
|
end
|
40
45
|
|
41
46
|
it "raise if it can't find an exchange rate" do
|
42
47
|
money = Money.new(0, 'USD')
|
43
|
-
proc { subject.exchange_with(money, 'SSP') }
|
48
|
+
_(proc { subject.exchange_with(money, 'SSP') })
|
44
49
|
.must_raise Money::Bank::UnknownRate
|
45
50
|
end
|
46
51
|
end
|
@@ -52,33 +57,33 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
52
57
|
|
53
58
|
it 'should be able to exchange money from USD to a known exchange rate' do
|
54
59
|
money = Money.new(100, 'USD')
|
55
|
-
subject.exchange_with(money, 'BBD').must_equal Money.new(200, 'BBD')
|
60
|
+
_(subject.exchange_with(money, 'BBD')).must_equal Money.new(200, 'BBD')
|
56
61
|
end
|
57
62
|
|
58
63
|
it 'should be able to exchange money from a known exchange rate to USD' do
|
59
64
|
money = Money.new(200, 'BBD')
|
60
|
-
subject.exchange_with(money, 'USD').must_equal Money.new(100, 'USD')
|
65
|
+
_(subject.exchange_with(money, 'USD')).must_equal Money.new(100, 'USD')
|
61
66
|
end
|
62
67
|
|
63
68
|
it 'should be able to exchange money when direct rate is unknown' do
|
64
69
|
money = Money.new(100, 'BBD')
|
65
|
-
subject.exchange_with(money, 'BMD').must_equal Money.new(50, 'BMD')
|
70
|
+
_(subject.exchange_with(money, 'BMD')).must_equal Money.new(50, 'BMD')
|
66
71
|
end
|
67
72
|
|
68
73
|
it 'should be able to handle non integer rates' do
|
69
74
|
money = Money.new(100, 'BBD')
|
70
|
-
subject.exchange_with(money, 'TJS').must_equal Money.new(250, 'TJS')
|
75
|
+
_(subject.exchange_with(money, 'TJS')).must_equal Money.new(250, 'TJS')
|
71
76
|
end
|
72
77
|
|
73
78
|
it "should raise if it can't find an currency" do
|
74
79
|
money = Money.new(0, 'USD')
|
75
|
-
proc { subject.exchange_with(money, 'PLP') }
|
80
|
+
_(proc { subject.exchange_with(money, 'PLP') })
|
76
81
|
.must_raise Money::Currency::UnknownCurrency
|
77
82
|
end
|
78
83
|
|
79
84
|
it "should raise if it can't find an exchange rate" do
|
80
85
|
money = Money.new(0, 'USD')
|
81
|
-
proc { subject.exchange_with(money, 'SSP') }
|
86
|
+
_(proc { subject.exchange_with(money, 'SSP') })
|
82
87
|
.must_raise Money::Bank::UnknownRate
|
83
88
|
end
|
84
89
|
end
|
@@ -95,7 +100,7 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
95
100
|
subject.oer_rates.keys.each do |currency|
|
96
101
|
next unless Money::Currency.find(currency)
|
97
102
|
|
98
|
-
subject.get_rate('USD', currency).must_be :>, 0
|
103
|
+
_(subject.get_rate('USD', currency)).must_be :>, 0
|
99
104
|
end
|
100
105
|
end
|
101
106
|
|
@@ -113,7 +118,7 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
113
118
|
Money::Currency.register(wtf)
|
114
119
|
subject.add_rate('USD', 'WTF', 2)
|
115
120
|
subject.add_rate('WTF', 'USD', 2)
|
116
|
-
subject.exchange_with(5000.to_money('WTF'), 'USD').cents.wont_equal 0
|
121
|
+
_(subject.exchange_with(5000.to_money('WTF'), 'USD').cents).wont_equal 0
|
117
122
|
end
|
118
123
|
|
119
124
|
# in response to #4
|
@@ -132,17 +137,50 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
132
137
|
rate = 13.7603
|
133
138
|
subject.add_rate('USD', 'BTC', 1 / 13.7603)
|
134
139
|
subject.add_rate('BTC', 'USD', rate)
|
135
|
-
subject.exchange_with(100.to_money('BTC'), 'USD').cents
|
140
|
+
_(subject.exchange_with(100.to_money('BTC'), 'USD').cents)
|
141
|
+
.must_equal 137_603
|
136
142
|
end
|
137
143
|
end
|
138
144
|
|
139
145
|
describe 'App ID' do
|
146
|
+
describe 'nil' do
|
147
|
+
before do
|
148
|
+
subject.app_id = nil
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'should raise an error if no App ID is set' do
|
152
|
+
_(proc { subject.update_rates }).must_raise Money::Bank::NoAppId
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe 'empty' do
|
157
|
+
before do
|
158
|
+
subject.app_id = ''
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'should raise an error if no App ID is set' do
|
162
|
+
_(proc { subject.update_rates }).must_raise Money::Bank::NoAppId
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
describe '#cache' do
|
140
168
|
before do
|
141
|
-
subject.
|
169
|
+
subject.app_id = TEST_APP_ID
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'support Pathname object' do
|
173
|
+
subject.cache = temp_cache_pathname
|
174
|
+
subject.stubs(:api_response).returns File.read(oer_latest_path)
|
175
|
+
subject.update_rates
|
176
|
+
subject.expects(:read_from_url).never
|
177
|
+
subject.update_rates
|
142
178
|
end
|
143
179
|
|
144
|
-
it '
|
145
|
-
|
180
|
+
it 'raise InvalidCache when the arg is not known' do
|
181
|
+
subject.cache = Array
|
182
|
+
add_to_webmock(subject)
|
183
|
+
_(proc { subject.update_rates }).must_raise Money::Bank::InvalidCache
|
146
184
|
end
|
147
185
|
end
|
148
186
|
|
@@ -155,7 +193,7 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
155
193
|
it 'should get from url' do
|
156
194
|
subject.expects(:save_cache).never
|
157
195
|
subject.update_rates
|
158
|
-
subject.oer_rates.wont_be_empty
|
196
|
+
_(subject.oer_rates).wont_be_empty
|
159
197
|
end
|
160
198
|
end
|
161
199
|
|
@@ -181,17 +219,18 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
181
219
|
end
|
182
220
|
|
183
221
|
it 'should use the secure https url' do
|
184
|
-
subject.source_url.must_equal historical_url
|
185
|
-
subject.source_url.must_include 'https://'
|
186
|
-
|
222
|
+
_(subject.source_url).must_equal historical_url
|
223
|
+
_(subject.source_url).must_include 'https://'
|
224
|
+
exp_url = "/api/historical/#{subject.date}.json"
|
225
|
+
_(subject.source_url).must_include exp_url
|
187
226
|
end
|
188
227
|
end
|
189
228
|
|
190
229
|
describe 'latest' do
|
191
230
|
it 'should use the secure https url' do
|
192
|
-
subject.source_url.must_equal source_url
|
193
|
-
subject.source_url.must_include 'https://'
|
194
|
-
subject.source_url.must_include '/api/latest.json'
|
231
|
+
_(subject.source_url).must_equal source_url
|
232
|
+
_(subject.source_url).must_include 'https://'
|
233
|
+
_(subject.source_url).must_include '/api/latest.json'
|
195
234
|
end
|
196
235
|
end
|
197
236
|
end
|
@@ -203,7 +242,7 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
203
242
|
end
|
204
243
|
|
205
244
|
it 'should raise an error if invalid path is given to update_rates' do
|
206
|
-
proc { subject.update_rates }.must_raise Money::Bank::InvalidCache
|
245
|
+
_(proc { subject.update_rates }).must_raise Money::Bank::InvalidCache
|
207
246
|
end
|
208
247
|
end
|
209
248
|
|
@@ -222,14 +261,14 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
222
261
|
end
|
223
262
|
|
224
263
|
it 'should get from url normally' do
|
225
|
-
subject.oer_rates.wont_be_empty
|
264
|
+
_(subject.oer_rates).wont_be_empty
|
226
265
|
end
|
227
266
|
|
228
267
|
it 'should save from url and get from cache' do
|
229
|
-
@global_rates.wont_be_empty
|
268
|
+
_(@global_rates).wont_be_empty
|
230
269
|
subject.expects(:source_url).never
|
231
270
|
subject.update_rates
|
232
|
-
subject.oer_rates.wont_be_empty
|
271
|
+
_(subject.oer_rates).wont_be_empty
|
233
272
|
end
|
234
273
|
end
|
235
274
|
|
@@ -259,21 +298,21 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
259
298
|
initial_size = File.read(temp_cache_path).size
|
260
299
|
subject.stubs(:api_response).returns ''
|
261
300
|
subject.refresh_rates
|
262
|
-
File.open(temp_cache_path).read.size.must_equal initial_size
|
301
|
+
_(File.open(temp_cache_path).read.size).must_equal initial_size
|
263
302
|
end
|
264
303
|
|
265
304
|
it 'should not break an existing file if save returns json without rates' do
|
266
305
|
initial_size = File.read(temp_cache_path).size
|
267
306
|
subject.stubs(:api_response).returns '{"error": "An error"}'
|
268
307
|
subject.refresh_rates
|
269
|
-
File.open(temp_cache_path).read.size.must_equal initial_size
|
308
|
+
_(File.open(temp_cache_path).read.size).must_equal initial_size
|
270
309
|
end
|
271
310
|
|
272
311
|
it 'should not break an existing file if save returns a invalid json' do
|
273
312
|
initial_size = File.read(temp_cache_path).size
|
274
313
|
subject.stubs(:api_response).returns '{invalid_json: "An error"}'
|
275
314
|
subject.refresh_rates
|
276
|
-
File.open(temp_cache_path).read.size.must_equal initial_size
|
315
|
+
_(File.open(temp_cache_path).read.size).must_equal initial_size
|
277
316
|
end
|
278
317
|
end
|
279
318
|
|
@@ -283,12 +322,12 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
283
322
|
end
|
284
323
|
|
285
324
|
it 'should be now when not updated from api' do
|
286
|
-
subject.rates_timestamp.must_be :>, Time.at(1_414_008_044)
|
325
|
+
_(subject.rates_timestamp).must_be :>, Time.at(1_414_008_044)
|
287
326
|
end
|
288
327
|
|
289
328
|
it 'should be set on update_rates' do
|
290
329
|
subject.update_rates
|
291
|
-
subject.rates_timestamp.must_equal Time.at(1_414_008_044)
|
330
|
+
_(subject.rates_timestamp).must_equal Time.at(1_414_008_044)
|
292
331
|
end
|
293
332
|
end
|
294
333
|
|
@@ -316,21 +355,21 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
316
355
|
describe 'when the ttl has expired' do
|
317
356
|
it 'should update the rates' do
|
318
357
|
Timecop.freeze(subject.rates_timestamp) do
|
319
|
-
subject.get_rate('USD', 'EUR').must_equal @old_usd_eur_rate
|
358
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @old_usd_eur_rate
|
320
359
|
end
|
321
360
|
Timecop.freeze(subject.rates_timestamp + (@ttl_in_seconds + 1)) do
|
322
|
-
subject.get_rate('USD', 'EUR').wont_equal @old_usd_eur_rate
|
323
|
-
subject.get_rate('USD', 'EUR').must_equal @new_usd_eur_rate
|
361
|
+
_(subject.get_rate('USD', 'EUR')).wont_equal @old_usd_eur_rate
|
362
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @new_usd_eur_rate
|
324
363
|
end
|
325
364
|
end
|
326
365
|
|
327
366
|
it 'should save rates' do
|
328
367
|
Timecop.freeze(subject.rates_timestamp) do
|
329
|
-
subject.get_rate('USD', 'EUR').must_equal @old_usd_eur_rate
|
368
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @old_usd_eur_rate
|
330
369
|
end
|
331
370
|
Timecop.freeze(subject.rates_timestamp + (@ttl_in_seconds + 1)) do
|
332
|
-
subject.get_rate('USD', 'EUR').must_equal @new_usd_eur_rate
|
333
|
-
@global_rates.wont_be_empty
|
371
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @new_usd_eur_rate
|
372
|
+
_(@global_rates).wont_be_empty
|
334
373
|
end
|
335
374
|
end
|
336
375
|
|
@@ -338,7 +377,7 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
338
377
|
Timecop.freeze(subject.rates_timestamp + (@ttl_in_seconds + 1)) do
|
339
378
|
exp_time = subject.rates_timestamp + @ttl_in_seconds
|
340
379
|
subject.expire_rates
|
341
|
-
subject.rates_expiration.must_equal exp_time
|
380
|
+
_(subject.rates_expiration).must_equal exp_time
|
342
381
|
end
|
343
382
|
end
|
344
383
|
|
@@ -346,12 +385,12 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
346
385
|
it 'should save rates and force refresh' do
|
347
386
|
subject.force_refresh_rate_on_expire = true
|
348
387
|
Timecop.freeze(subject.rates_timestamp) do
|
349
|
-
subject.get_rate('USD', 'EUR').must_equal @old_usd_eur_rate
|
388
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @old_usd_eur_rate
|
350
389
|
end
|
351
390
|
Timecop.freeze(Time.now + 1001) do
|
352
391
|
@global_rates = []
|
353
|
-
subject.get_rate('USD', 'EUR').must_equal @new_usd_eur_rate
|
354
|
-
@global_rates.wont_be_empty
|
392
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @new_usd_eur_rate
|
393
|
+
_(@global_rates).wont_be_empty
|
355
394
|
end
|
356
395
|
end
|
357
396
|
end
|
@@ -364,7 +403,7 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
364
403
|
subject.expects(:update_rates).never
|
365
404
|
subject.expects(:refresh_rates_expiration).never
|
366
405
|
subject.expire_rates
|
367
|
-
subject.rates_expiration.must_equal exp_time
|
406
|
+
_(subject.rates_expiration).must_equal exp_time
|
368
407
|
end
|
369
408
|
end
|
370
409
|
end
|
@@ -373,19 +412,31 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
373
412
|
describe 'historical' do
|
374
413
|
before do
|
375
414
|
add_to_webmock(subject)
|
376
|
-
# see test/latest.json +52
|
415
|
+
# see test/data/latest.json +52
|
377
416
|
@latest_usd_eur_rate = 0.79085
|
378
|
-
|
417
|
+
@latest_chf_eur_rate = 0.830792859
|
418
|
+
# see test/data/2015-01-01.json +52
|
379
419
|
@old_usd_eur_rate = 0.830151
|
420
|
+
@old_chf_eur_rate = 0.832420177
|
380
421
|
subject.update_rates
|
381
422
|
end
|
382
423
|
|
383
424
|
it 'should be different than the latest' do
|
384
|
-
subject.get_rate('USD', 'EUR').must_equal @latest_usd_eur_rate
|
425
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @latest_usd_eur_rate
|
426
|
+
subject.date = '2015-01-01'
|
427
|
+
add_to_webmock(subject, oer_historical_path)
|
428
|
+
subject.update_rates
|
429
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @old_usd_eur_rate
|
430
|
+
end
|
431
|
+
|
432
|
+
it 'should update cross courses' do
|
433
|
+
_(subject.get_rate('CHF', 'EUR').round(9).to_f)
|
434
|
+
.must_equal @latest_chf_eur_rate
|
385
435
|
subject.date = '2015-01-01'
|
386
436
|
add_to_webmock(subject, oer_historical_path)
|
387
437
|
subject.update_rates
|
388
|
-
subject.get_rate('
|
438
|
+
_(subject.get_rate('CHF', 'EUR').round(9).to_f)
|
439
|
+
.must_equal @old_chf_eur_rate
|
389
440
|
end
|
390
441
|
end
|
391
442
|
|
@@ -393,15 +444,15 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
393
444
|
it 'should be changed when a known currency is given' do
|
394
445
|
source = 'EUR'
|
395
446
|
subject.source = source
|
396
|
-
subject.source.must_equal source
|
397
|
-
subject.source_url.must_include "base=#{source}"
|
447
|
+
_(subject.source).must_equal source
|
448
|
+
_(subject.source_url).must_include "base=#{source}"
|
398
449
|
end
|
399
450
|
|
400
451
|
it 'should use USD when given unknown currency' do
|
401
452
|
source = 'invalid'
|
402
453
|
subject.source = source
|
403
|
-
subject.source.must_equal default_source
|
404
|
-
subject.source_url.wont_include "base=#{default_source}"
|
454
|
+
_(subject.source).must_equal default_source
|
455
|
+
_(subject.source_url).wont_include "base=#{default_source}"
|
405
456
|
end
|
406
457
|
end
|
407
458
|
|
@@ -412,11 +463,11 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
412
463
|
end
|
413
464
|
|
414
465
|
it 'should return the default value' do
|
415
|
-
subject.prettyprint.must_equal true
|
466
|
+
_(subject.prettyprint).must_equal true
|
416
467
|
end
|
417
468
|
|
418
469
|
it 'should include prettyprint param as true' do
|
419
|
-
subject.source_url.must_include 'prettyprint=true'
|
470
|
+
_(subject.source_url).must_include 'prettyprint=true'
|
420
471
|
end
|
421
472
|
end
|
422
473
|
|
@@ -426,11 +477,11 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
426
477
|
end
|
427
478
|
|
428
479
|
it 'should return the value' do
|
429
|
-
subject.prettyprint.must_equal false
|
480
|
+
_(subject.prettyprint).must_equal false
|
430
481
|
end
|
431
482
|
|
432
483
|
it 'should include prettyprint param as false' do
|
433
|
-
subject.source_url.must_include 'prettyprint=false'
|
484
|
+
_(subject.source_url).must_include 'prettyprint=false'
|
434
485
|
end
|
435
486
|
end
|
436
487
|
end
|
@@ -442,11 +493,11 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
442
493
|
end
|
443
494
|
|
444
495
|
it 'should return the default value' do
|
445
|
-
subject.show_alternative.must_equal false
|
496
|
+
_(subject.show_alternative).must_equal false
|
446
497
|
end
|
447
498
|
|
448
499
|
it 'should include show_alternative param as false' do
|
449
|
-
subject.source_url.must_include 'show_alternative=false'
|
500
|
+
_(subject.source_url).must_include 'show_alternative=false'
|
450
501
|
end
|
451
502
|
end
|
452
503
|
|
@@ -456,11 +507,11 @@ describe Money::Bank::OpenExchangeRatesBank do
|
|
456
507
|
end
|
457
508
|
|
458
509
|
it 'should return the value' do
|
459
|
-
subject.show_alternative.must_equal true
|
510
|
+
_(subject.show_alternative).must_equal true
|
460
511
|
end
|
461
512
|
|
462
513
|
it 'should include show_alternative param as true' do
|
463
|
-
subject.source_url.must_include 'show_alternative=true'
|
514
|
+
_(subject.source_url).must_include 'show_alternative=true'
|
464
515
|
end
|
465
516
|
end
|
466
517
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,21 +1,20 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
begin
|
4
|
-
require '
|
5
|
-
|
4
|
+
require 'simplecov'
|
5
|
+
SimpleCov.start
|
6
6
|
rescue LoadError
|
7
|
-
|
7
|
+
warn 'simplecov not loaded'
|
8
8
|
end
|
9
9
|
|
10
10
|
require 'minitest/autorun'
|
11
|
-
require 'minitest/focus'
|
12
11
|
require 'mocha/minitest'
|
13
12
|
require 'webmock/minitest'
|
14
13
|
require 'money/bank/open_exchange_rates_bank'
|
15
14
|
require 'monetize'
|
16
15
|
require 'timecop'
|
17
16
|
|
18
|
-
TEST_APP_ID = 'TEST_APP_ID'
|
17
|
+
TEST_APP_ID = 'TEST_APP_ID'
|
19
18
|
|
20
19
|
def data_file(file)
|
21
20
|
File.expand_path(File.join(File.dirname(__FILE__), 'data', file))
|
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.
|
4
|
+
version: 1.4.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:
|
11
|
+
date: 2020-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: money
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '5'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: minitest-focus
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '1'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '1'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: mocha
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -106,14 +92,14 @@ dependencies:
|
|
106
92
|
requirements:
|
107
93
|
- - "~>"
|
108
94
|
- !ruby/object:Gem::Version
|
109
|
-
version:
|
95
|
+
version: 0.76.0
|
110
96
|
type: :development
|
111
97
|
prerelease: false
|
112
98
|
version_requirements: !ruby/object:Gem::Requirement
|
113
99
|
requirements:
|
114
100
|
- - "~>"
|
115
101
|
- !ruby/object:Gem::Version
|
116
|
-
version:
|
102
|
+
version: 0.76.0
|
117
103
|
- !ruby/object:Gem::Dependency
|
118
104
|
name: timecop
|
119
105
|
requirement: !ruby/object:Gem::Requirement
|
@@ -175,15 +161,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
175
161
|
requirements:
|
176
162
|
- - ">="
|
177
163
|
- !ruby/object:Gem::Version
|
178
|
-
version: '2.
|
164
|
+
version: '2.4'
|
179
165
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
166
|
requirements:
|
181
167
|
- - ">="
|
182
168
|
- !ruby/object:Gem::Version
|
183
169
|
version: '0'
|
184
170
|
requirements: []
|
185
|
-
|
186
|
-
rubygems_version: 2.7.6
|
171
|
+
rubygems_version: 3.1.2
|
187
172
|
signing_key:
|
188
173
|
specification_version: 4
|
189
174
|
summary: A gem that calculates the exchange rate using published rates from open-exchange-rates.
|