money-open-exchange-rates 0.7.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile +4 -0
- data/History.md +103 -0
- data/LICENSE +1 -1
- data/README.md +170 -29
- data/lib/money/bank/open_exchange_rates_bank.rb +176 -68
- data/lib/open_exchange_rates_bank/version.rb +1 -1
- data/test/integration/Gemfile +2 -0
- data/test/integration/api.rb +6 -1
- data/test/open_exchange_rates_bank_test.rb +263 -130
- data/test/test_helper.rb +10 -2
- metadata +36 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
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,107 @@
|
|
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
|
+
|
28
|
+
1.3.0 / 2019-01-20
|
29
|
+
==================
|
30
|
+
|
31
|
+
* Update webmock to 3.5
|
32
|
+
* Update rubocop to 0.63
|
33
|
+
* Switch from rr to mocha/minitest
|
34
|
+
* Add prettyprint option
|
35
|
+
* README: add info about tests
|
36
|
+
* Remove deprecated secure_connection= option
|
37
|
+
* Merge pull request #54 from @LinkUpStudioUA / symbols-doc-link-fix
|
38
|
+
* Fix link to filtering by symbols doc
|
39
|
+
* Fix bundler to <2 on travis (dropped support Ruby < 2.3)
|
40
|
+
* Fix bundler to 1.17.3 (2.0 dropped support Ruby < 2.3)
|
41
|
+
* Merge pull request #52 from @thejchap / feature/symbols
|
42
|
+
* add support for symbols query param
|
43
|
+
* Remove ruby 2.0/2.1 support and fix rubocop offenses
|
44
|
+
* Add minitest-focus gem
|
45
|
+
* Avoid redefine json_response method
|
46
|
+
|
47
|
+
v1.2.2 / 2018-03-31
|
48
|
+
===================
|
49
|
+
|
50
|
+
* Warn secure_connection= is deprecated
|
51
|
+
* More simple code on calc_pair_rate_using_base
|
52
|
+
* Increase code coverage on source_url
|
53
|
+
* Fix parse error specs on refresh_rates
|
54
|
+
* Use Coveralls for coverage
|
55
|
+
* Add tests for rates_timestamp issue
|
56
|
+
|
57
|
+
v1.2.1 / 2018-03-31
|
58
|
+
===================
|
59
|
+
|
60
|
+
* Fix rates_timestamp should be now per default
|
61
|
+
|
62
|
+
v1.2.0 / 2018-03-31
|
63
|
+
===================
|
64
|
+
|
65
|
+
* Merge pull request #51 from spk/fix-expire_rates
|
66
|
+
* Add force_refresh_rate_on_expire option and use api timestamp
|
67
|
+
* README: info currency-exchange
|
68
|
+
|
69
|
+
v1.1.1 / 2018-03-30
|
70
|
+
===================
|
71
|
+
|
72
|
+
* Merge pull request #50 from @v-kolesnikov / fix/avoid-monetize
|
73
|
+
* Use `monetize` as a development dependency only
|
74
|
+
|
75
|
+
v1.1.0 / 2018-03-28
|
76
|
+
===================
|
77
|
+
|
78
|
+
* Force refresh when ttl is expire and cache invalid (issue #47)
|
79
|
+
* Less strict dependency version for money/monetize
|
80
|
+
|
81
|
+
v1.0.2 / 2018-03-27
|
82
|
+
===================
|
83
|
+
|
84
|
+
* Merge pull request #46 from @cedricpim / fix-float-big-decimal-casting
|
85
|
+
* Fix issue with Float casting for BigDecimal
|
86
|
+
|
87
|
+
v1.0.1 / 2018-03-25
|
88
|
+
===================
|
89
|
+
|
90
|
+
* Use BigDecimal instead of Float
|
91
|
+
|
92
|
+
v1.0.0 / 2018-03-25
|
93
|
+
===================
|
94
|
+
|
95
|
+
* Merge pull request #41 from @b-mandelbrot /add-show-alternative
|
96
|
+
* Add support for black market and digital currency rates
|
97
|
+
* Merge pull request #42 from spk/save-rates-when-ttl-expire
|
98
|
+
* Save rates to cache after first fetch and add example with Rails
|
99
|
+
* Improve documation about cache and rates ttl
|
100
|
+
* Save rates when ttl expire
|
101
|
+
* Merge pull request #40 from @Jetbuilt / deprecate-secure_connection
|
102
|
+
* Closes #39 - Make all requests over https and deprecate `secure_connection`
|
103
|
+
* Support Ruby >= 2.0
|
104
|
+
|
2
105
|
v0.7.0 / 2016-10-23
|
3
106
|
===================
|
4
107
|
|
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
@@ -1,7 +1,9 @@
|
|
1
1
|
# Money Open Exchange Rates
|
2
2
|
|
3
3
|
A gem that calculates the exchange rate using published rates from
|
4
|
-
[open-exchange-rates](https://openexchangerates.org/)
|
4
|
+
[open-exchange-rates](https://openexchangerates.org/). Compatible with
|
5
|
+
[Money](https://github.com/RubyMoney/money#currency-exchange) [currency
|
6
|
+
exchange](http://www.rubydoc.info/gems/money/Money/Bank/VariableExchange).
|
5
7
|
|
6
8
|
Check [api documentation](https://docs.openexchangerates.org/)
|
7
9
|
|
@@ -11,69 +13,136 @@ Check [api documentation](https://docs.openexchangerates.org/)
|
|
11
13
|
[180 currencies](https://docs.openexchangerates.org/docs/supported-currencies).
|
12
14
|
* [Free plan](https://openexchangerates.org/signup) hourly updates, with USD
|
13
15
|
base and up to 1,000 requests/month.
|
14
|
-
*
|
16
|
+
* Automatically caches API results to file or Rails cache.
|
15
17
|
* Calculate pair rates.
|
18
|
+
* Automatically fetches new data from API if data becomes stale when
|
19
|
+
`ttl_in_seconds` option is provided.
|
20
|
+
* Support for black market and digital currency rates with `show_alternative`
|
21
|
+
option.
|
16
22
|
|
17
23
|
## Installation
|
18
24
|
|
19
25
|
Add this line to your application's Gemfile:
|
20
26
|
|
21
|
-
|
27
|
+
``` ruby
|
22
28
|
gem 'money-open-exchange-rates'
|
23
|
-
|
29
|
+
```
|
24
30
|
|
25
31
|
And then execute:
|
26
32
|
|
27
|
-
|
33
|
+
```
|
28
34
|
bundle
|
29
|
-
|
35
|
+
```
|
30
36
|
|
31
37
|
Or install it yourself as:
|
32
38
|
|
33
|
-
|
39
|
+
```
|
34
40
|
gem install money-open-exchange-rates
|
35
|
-
|
41
|
+
```
|
36
42
|
|
37
43
|
## Usage
|
38
44
|
|
39
|
-
|
45
|
+
``` ruby
|
40
46
|
require 'money/bank/open_exchange_rates_bank'
|
41
|
-
|
42
|
-
|
47
|
+
|
48
|
+
# Memory store per default; for others just pass as argument a class like
|
49
|
+
# explained in https://github.com/RubyMoney/money#exchange-rate-stores
|
50
|
+
oxr = Money::Bank::OpenExchangeRatesBank.new(Money::RatesStore::Memory.new)
|
43
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`
|
44
58
|
oxr.update_rates
|
45
59
|
|
46
60
|
# (optional)
|
47
|
-
#
|
61
|
+
# See https://github.com/spk/money-open-exchange-rates#cache for more info
|
62
|
+
# Updated only when `refresh_rates` is called
|
63
|
+
oxr.cache = 'path/to/file/cache.json'
|
64
|
+
|
65
|
+
# (optional)
|
66
|
+
# Set the seconds after than the current rates are automatically expired
|
48
67
|
# by default, they never expire, in this example 1 day.
|
68
|
+
# This ttl is about money store (memory, database ...) passed though
|
69
|
+
# `Money::Bank::OpenExchangeRatesBank` as argument not about `cache` option.
|
70
|
+
# The base time is the timestamp fetched from API.
|
49
71
|
oxr.ttl_in_seconds = 86400
|
72
|
+
|
50
73
|
# (optional)
|
51
|
-
#
|
52
|
-
# disabled by default to support free-tier users
|
53
|
-
# see https://openexchangerates.org/documentation#https
|
54
|
-
oxr.secure_connection = true
|
55
|
-
# (optional)
|
56
|
-
# set historical date of the rate
|
74
|
+
# Set historical date of the rate
|
57
75
|
# see https://openexchangerates.org/documentation#historical-data
|
58
76
|
oxr.date = '2015-01-01'
|
77
|
+
|
59
78
|
# (optional)
|
60
79
|
# Set the base currency for all rates. By default, USD is used.
|
61
80
|
# OpenExchangeRates only allows USD as base currency
|
62
81
|
# for the free plan users.
|
63
82
|
oxr.source = 'USD'
|
64
83
|
|
65
|
-
#
|
66
|
-
|
84
|
+
# (optional)
|
85
|
+
# Extend returned values with alternative, black market and digital currency
|
86
|
+
# rates. By default, false is used
|
87
|
+
# see: https://docs.openexchangerates.org/docs/alternative-currencies
|
88
|
+
oxr.show_alternative = true
|
89
|
+
|
90
|
+
# (optional)
|
91
|
+
# Minified Response ('prettyprint')
|
92
|
+
# see https://docs.openexchangerates.org/docs/prettyprint
|
93
|
+
oxr.prettyprint = false
|
94
|
+
|
95
|
+
# (optional)
|
96
|
+
# Refresh rates, store in cache and update rates
|
97
|
+
# Should be used on crontab/worker/scheduler `Money.default_bank.refresh_rates`
|
98
|
+
# If you are using unicorn-worker-killer gem or on Heroku like platform,
|
99
|
+
# you should avoid to put this on the initializer of your Rails application,
|
100
|
+
# because will increase your OXR API usage.
|
101
|
+
oxr.refresh_rates
|
102
|
+
|
103
|
+
# (optional)
|
104
|
+
# Force refresh rates cache and store on the fly when ttl is expired
|
105
|
+
# This will slow down request on get_rate, so use at your on risk, if you don't
|
106
|
+
# want to setup crontab/worker/scheduler for your application.
|
107
|
+
# Again this is not safe with multiple servers and could increase API usage.
|
108
|
+
oxr.force_refresh_rate_on_expire = true
|
67
109
|
|
68
110
|
Money.default_bank = oxr
|
69
111
|
|
70
112
|
Money.default_bank.get_rate('USD', 'CAD')
|
71
|
-
|
113
|
+
```
|
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
|
+
```
|
72
126
|
|
73
|
-
|
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
|
+
|
140
|
+
## Cache
|
141
|
+
|
142
|
+
You can also provide a `Proc` as a cache to provide your own caching mechanism
|
74
143
|
perhaps with Redis or just a thread safe `Hash` (global). For example:
|
75
144
|
|
76
|
-
|
145
|
+
``` ruby
|
77
146
|
oxr.cache = Proc.new do |v|
|
78
147
|
key = 'money:exchange_rates'
|
79
148
|
if v
|
@@ -82,16 +151,88 @@ oxr.cache = Proc.new do |v|
|
|
82
151
|
Thread.current[key]
|
83
152
|
end
|
84
153
|
end
|
85
|
-
|
154
|
+
```
|
155
|
+
|
156
|
+
With `Rails` cache example:
|
157
|
+
|
158
|
+
``` ruby
|
159
|
+
OXR_CACHE_KEY = "#{Rails.env}:money:exchange_rates".freeze
|
160
|
+
oxr.ttl_in_seconds = 86400
|
161
|
+
oxr.cache = Proc.new do |text|
|
162
|
+
if text
|
163
|
+
Rails.cache.write(OXR_CACHE_KEY, text)
|
164
|
+
else
|
165
|
+
Rails.cache.read(OXR_CACHE_KEY)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
To update the cache call `Money.default_bank.refresh_rates` on
|
171
|
+
crontab/worker/scheduler. This have to be done this way because the fetch can
|
172
|
+
take some time (HTTP call) and can fail.
|
173
|
+
|
174
|
+
## Full example configuration initializer with Rails and cache
|
175
|
+
|
176
|
+
``` ruby
|
177
|
+
require 'money/bank/open_exchange_rates_bank'
|
178
|
+
|
179
|
+
OXR_CACHE_KEY = "#{Rails.env}:money:exchange_rates".freeze
|
180
|
+
# ExchangeRate is an ActiveRecord model
|
181
|
+
# more info at https://github.com/RubyMoney/money#exchange-rate-stores
|
182
|
+
oxr = Money::Bank::OpenExchangeRatesBank.new(ExchangeRate)
|
183
|
+
oxr.ttl_in_seconds = 86400
|
184
|
+
oxr.cache = Proc.new do |text|
|
185
|
+
if text
|
186
|
+
# only expire when refresh_rates is called or `force_refresh_rate_on_expire`
|
187
|
+
# option is enabled
|
188
|
+
# you can also set `expires_in` option on write to force fetch new rates
|
189
|
+
Rails.cache.write(OXR_CACHE_KEY, text)
|
190
|
+
else
|
191
|
+
Rails.cache.read(OXR_CACHE_KEY)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
oxr.app_id = ENV['OXR_API_KEY']
|
195
|
+
oxr.show_alternative = true
|
196
|
+
oxr.prettyprint = false
|
197
|
+
|
198
|
+
# This can be removed if you have data to avoid http call on boot for production
|
199
|
+
oxr.update_rates
|
200
|
+
|
201
|
+
Money.default_bank = oxr
|
202
|
+
```
|
203
|
+
|
204
|
+
See also how to [refresh and update rates](#refresh-rates)
|
205
|
+
|
206
|
+
### Tests
|
207
|
+
|
208
|
+
To avoid to hit the API we can use the cache option with a saved file like this:
|
209
|
+
|
210
|
+
``` ruby
|
211
|
+
OXR_CACHE_KEY = "#{Rails.env}:money:exchange_rates".freeze
|
212
|
+
if Rails.env.test?
|
213
|
+
oxr.cache = Rails.root.join("test/fixtures/currency-rates.json").to_s
|
214
|
+
else
|
215
|
+
oxr.ttl_in_seconds = 5.minutes.to_i
|
216
|
+
oxr.cache = Proc.new do |text|
|
217
|
+
if text
|
218
|
+
Rails.cache.write(OXR_CACHE_KEY, text)
|
219
|
+
else
|
220
|
+
Rails.cache.read(OXR_CACHE_KEY)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
```
|
225
|
+
|
226
|
+
## Pair rates
|
86
227
|
|
87
228
|
Unknown pair rates are transparently calculated: using inverse rate (if known),
|
88
229
|
or using base currency rate to both currencies forming the pair.
|
89
230
|
|
90
231
|
## Tests
|
91
232
|
|
92
|
-
|
233
|
+
```
|
93
234
|
bundle exec rake
|
94
|
-
|
235
|
+
```
|
95
236
|
|
96
237
|
## Refs
|
97
238
|
|
@@ -108,12 +249,12 @@ See [GitHub](https://github.com/spk/money-open-exchange-rates/graphs/contributor
|
|
108
249
|
|
109
250
|
The MIT License
|
110
251
|
|
111
|
-
Copyright © 2011-
|
252
|
+
Copyright © 2011-2020 Laurent Arnoud <laurent@spkdev.net>
|
112
253
|
|
113
254
|
---
|
114
|
-
[![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)
|
115
257
|
[![Version](https://img.shields.io/gem/v/money-open-exchange-rates.svg)](https://rubygems.org/gems/money-open-exchange-rates)
|
116
258
|
[![Documentation](https://img.shields.io/badge/doc-rubydoc-blue.svg)](http://www.rubydoc.info/gems/money-open-exchange-rates)
|
117
259
|
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT "MIT")
|
118
|
-
[![Code Climate](https://img.shields.io/codeclimate/github/spk/money-open-exchange-rates.svg)](https://codeclimate.com/github/spk/money-open-exchange-rates)
|
119
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)
|