money-currencylayer-bank 0.5.7 → 0.5.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -14
- data/lib/money/bank/currencylayer_bank.rb +14 -7
- data/test/currencylayer_bank_test.rb +27 -26
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad15fc05b8e56c61e10554fd8574e0dbd358e3adc287c140ee11cb4b5c5747d8
|
4
|
+
data.tar.gz: 3ace2bd923ee971f5f6fca67f5ada19a0edf25687fbf0806b197f8876ee33146
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25849b05854111ab7ec51474fdbe9d02a5dd03d80719d46f0358a1c9c87b63e6296f2edcacd46e65e975e5510f16a456b4be50f1c92aa87c2b9af81504d5cf49
|
7
|
+
data.tar.gz: ce7667380baef8501825232ff7425b3a3121e85f0bfc4d8656d4c8b310e50d84ed787e6384d7da1bd692bf44233cccbaadee7dcf559232aece1ef83b28f258ec
|
data/README.md
CHANGED
@@ -3,9 +3,8 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/money-currencylayer-bank.svg)](https://rubygems.org/gems/money-currencylayer-bank)
|
4
4
|
[![Gem](https://img.shields.io/gem/dt/money-currencylayer-bank.svg?maxAge=2592000)](https://rubygems.org/gems/money-currencylayer-bank)
|
5
5
|
[![Build Status](https://secure.travis-ci.org/phlegx/money-currencylayer-bank.svg?branch=master)](https://travis-ci.org/phlegx/money-currencylayer-bank)
|
6
|
-
[![Code Climate](
|
6
|
+
[![Code Climate](https://codeclimate.com/github/phlegx/money-currencylayer-bank.svg)](https://codeclimate.com/github/phlegx/money-currencylayer-bank)
|
7
7
|
[![Inline Docs](http://inch-ci.org/github/phlegx/money-currencylayer-bank.svg?branch=master)](http://inch-ci.org/github/phlegx/money-currencylayer-bank)
|
8
|
-
[![Dependency Status](https://gemnasium.com/phlegx/money-currencylayer-bank.svg)](https://gemnasium.com/phlegx/money-currencylayer-bank)
|
9
8
|
[![License](https://img.shields.io/github/license/phlegx/money-currencylayer-bank.svg)](http://opensource.org/licenses/MIT)
|
10
9
|
|
11
10
|
A gem that calculates the exchange rate using published rates from
|
@@ -61,36 +60,41 @@ Or install it yourself as:
|
|
61
60
|
## Usage
|
62
61
|
|
63
62
|
~~~ ruby
|
64
|
-
# Minimal requirements
|
63
|
+
# Minimal requirements.
|
65
64
|
require 'money/bank/currencylayer_bank'
|
66
65
|
mclb = Money::Bank::CurrencylayerBank.new
|
67
66
|
mclb.access_key = 'your access_key from https://currencylayer.com/product'
|
68
67
|
|
69
|
-
# Update rates (get new rates from remote if expired or access rates from cache)
|
70
|
-
mclb.update_rates
|
71
|
-
|
72
|
-
# Force update rates from remote and store in cache
|
73
|
-
# mclb.update_rates(true)
|
74
|
-
|
75
68
|
# (optional)
|
76
69
|
# Set the base currency for all rates. By default, USD is used.
|
77
70
|
# CurrencylayerBank only allows USD as base currency for the free plan users.
|
78
71
|
mclb.source = 'EUR'
|
79
72
|
|
80
73
|
# (optional)
|
81
|
-
# Set the seconds after than the current rates are automatically expired
|
74
|
+
# Set the seconds after than the current rates are automatically expired.
|
82
75
|
# by default, they never expire, in this example 1 day.
|
83
76
|
mclb.ttl_in_seconds = 86400
|
84
77
|
|
85
78
|
# (optional)
|
86
|
-
# Use https to fetch rates from CurrencylayerBank
|
79
|
+
# Use https to fetch rates from CurrencylayerBank.
|
87
80
|
# CurrencylayerBank only allows http as connection for the free plan users.
|
88
81
|
mclb.secure_connection = true
|
89
82
|
|
90
|
-
# Define cache (string or pathname)
|
83
|
+
# Define cache (string or pathname).
|
91
84
|
mclb.cache = 'path/to/file/cache'
|
92
85
|
|
93
|
-
#
|
86
|
+
# Update rates (get new rates from remote if expired or access rates from cache).
|
87
|
+
# Be sure to define the cache first before updating the rates.
|
88
|
+
mclb.update_rates
|
89
|
+
|
90
|
+
# Force update rates from remote and store in cache.
|
91
|
+
# Be sure to define the cache first before updating the rates.
|
92
|
+
# mclb.update_rates(true)
|
93
|
+
|
94
|
+
# Set money rounding mode.
|
95
|
+
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
|
96
|
+
|
97
|
+
# Set money default bank to Currencylayer bank.
|
94
98
|
Money.default_bank = mclb
|
95
99
|
~~~
|
96
100
|
|
@@ -212,4 +216,4 @@ bundle exec rake
|
|
212
216
|
|
213
217
|
The MIT License
|
214
218
|
|
215
|
-
Copyright (c)
|
219
|
+
Copyright (c) 2022 Phlegx Systems OG
|
@@ -31,7 +31,7 @@ class Money
|
|
31
31
|
# CurrencylayerBank base class
|
32
32
|
class CurrencylayerBank < Money::Bank::VariableExchange
|
33
33
|
# CurrencylayerBank url
|
34
|
-
CL_URL = 'http://
|
34
|
+
CL_URL = 'http://api.currencylayer.com/live'.freeze
|
35
35
|
# CurrencylayerBank secure url
|
36
36
|
CL_SECURE_URL = CL_URL.sub('http:', 'https:')
|
37
37
|
# Default base currency
|
@@ -240,7 +240,7 @@ class Money
|
|
240
240
|
# Opens an url and reads the content
|
241
241
|
# @return [String] unparsed JSON content
|
242
242
|
def open_url
|
243
|
-
|
243
|
+
URI.open(source_url).read
|
244
244
|
rescue OpenURI::HTTPError
|
245
245
|
''
|
246
246
|
end
|
@@ -268,11 +268,18 @@ class Money
|
|
268
268
|
# @param straight [Boolean] true for straight, default is careful
|
269
269
|
# @return [Hash] key is country code (ISO 3166-1 alpha-3) value Float
|
270
270
|
def exchange_rates(straight = false)
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
271
|
+
rates = if straight
|
272
|
+
raw_rates_straight
|
273
|
+
else
|
274
|
+
raw_rates_careful
|
275
|
+
end
|
276
|
+
if rates.key?('quotes')
|
277
|
+
@rates = rates['quotes']
|
278
|
+
elsif rates.key?('error')
|
279
|
+
raise Error, rates.dig('error', 'info')
|
280
|
+
else
|
281
|
+
raise Error, 'Unknown rates situation!'
|
282
|
+
end
|
276
283
|
end
|
277
284
|
|
278
285
|
# Get raw exchange rates from cache and then from url
|
@@ -4,6 +4,7 @@
|
|
4
4
|
require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
|
5
5
|
|
6
6
|
describe Money::Bank::CurrencylayerBank do
|
7
|
+
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
|
7
8
|
subject { Money::Bank::CurrencylayerBank.new }
|
8
9
|
let(:url) { Money::Bank::CurrencylayerBank::CL_URL }
|
9
10
|
let(:secure_url) { Money::Bank::CurrencylayerBank::CL_SECURE_URL }
|
@@ -30,12 +31,12 @@ describe Money::Bank::CurrencylayerBank do
|
|
30
31
|
describe 'without rates' do
|
31
32
|
it 'able to exchange a money to its own currency even without rates' do
|
32
33
|
money = Money.new(0, 'USD')
|
33
|
-
subject.exchange_with(money, 'USD').must_equal money
|
34
|
+
_(subject.exchange_with(money, 'USD')).must_equal money
|
34
35
|
end
|
35
36
|
|
36
37
|
it "raise if it can't find an exchange rate" do
|
37
38
|
money = Money.new(0, 'USD')
|
38
|
-
proc { subject.exchange_with(money, 'SSP') }
|
39
|
+
_(proc { subject.exchange_with(money, 'SSP') })
|
39
40
|
.must_raise Money::Bank::UnknownRate
|
40
41
|
end
|
41
42
|
end
|
@@ -47,17 +48,17 @@ describe Money::Bank::CurrencylayerBank do
|
|
47
48
|
|
48
49
|
it 'should be able to exchange money from USD to a known exchange rate' do
|
49
50
|
money = Money.new(100, 'USD')
|
50
|
-
subject.exchange_with(money, 'BBD').must_equal Money.new(200, 'BBD')
|
51
|
+
_(subject.exchange_with(money, 'BBD')).must_equal Money.new(200, 'BBD')
|
51
52
|
end
|
52
53
|
|
53
54
|
it 'should be able to exchange money from a known exchange rate to USD' do
|
54
55
|
money = Money.new(200, 'BBD')
|
55
|
-
subject.exchange_with(money, 'USD').must_equal Money.new(100, 'USD')
|
56
|
+
_(subject.exchange_with(money, 'USD')).must_equal Money.new(100, 'USD')
|
56
57
|
end
|
57
58
|
|
58
59
|
it "should raise if it can't find an exchange rate" do
|
59
60
|
money = Money.new(0, 'USD')
|
60
|
-
proc { subject.exchange_with(money, 'SSP') }
|
61
|
+
_(proc { subject.exchange_with(money, 'SSP') })
|
61
62
|
.must_raise Money::Bank::UnknownRate
|
62
63
|
end
|
63
64
|
end
|
@@ -87,21 +88,21 @@ describe Money::Bank::CurrencylayerBank do
|
|
87
88
|
initial_size = File.read(temp_cache_path).size
|
88
89
|
stub(subject).open_url { '' }
|
89
90
|
subject.update_rates
|
90
|
-
File.read(temp_cache_path).size.must_equal initial_size
|
91
|
+
_(File.read(temp_cache_path).size).must_equal initial_size
|
91
92
|
end
|
92
93
|
|
93
94
|
it 'should not break an existing file if save returns json without rates' do
|
94
95
|
initial_size = File.read(temp_cache_path).size
|
95
96
|
stub(subject).open_url { '{ "error": "An error" }' }
|
96
97
|
subject.update_rates
|
97
|
-
File.read(temp_cache_path).size.must_equal initial_size
|
98
|
+
_(File.read(temp_cache_path).size).must_equal initial_size
|
98
99
|
end
|
99
100
|
|
100
101
|
it 'should not break an existing file if save returns a invalid json' do
|
101
102
|
initial_size = File.read(temp_cache_path).size
|
102
103
|
stub(subject).open_url { '{ invalid_json: "An error" }' }
|
103
104
|
subject.update_rates
|
104
|
-
File.read(temp_cache_path).size.must_equal initial_size
|
105
|
+
_(File.read(temp_cache_path).size).must_equal initial_size
|
105
106
|
end
|
106
107
|
end
|
107
108
|
|
@@ -114,7 +115,7 @@ describe Money::Bank::CurrencylayerBank do
|
|
114
115
|
|
115
116
|
it 'should get from url' do
|
116
117
|
subject.update_rates
|
117
|
-
subject.rates.wont_be_empty
|
118
|
+
_(subject.rates).wont_be_empty
|
118
119
|
end
|
119
120
|
end
|
120
121
|
|
@@ -126,7 +127,7 @@ describe Money::Bank::CurrencylayerBank do
|
|
126
127
|
end
|
127
128
|
|
128
129
|
it 'should raise an error if invalid path is given' do
|
129
|
-
proc { subject.update_rates }.must_raise Money::Bank::InvalidCache
|
130
|
+
_(proc { subject.update_rates }).must_raise Money::Bank::InvalidCache
|
130
131
|
end
|
131
132
|
end
|
132
133
|
|
@@ -146,16 +147,16 @@ describe Money::Bank::CurrencylayerBank do
|
|
146
147
|
it 'should get from url normally' do
|
147
148
|
stub(subject).source_url { data_path }
|
148
149
|
subject.update_rates
|
149
|
-
subject.rates.wont_be_empty
|
150
|
+
_(subject.rates).wont_be_empty
|
150
151
|
end
|
151
152
|
|
152
153
|
it 'should save from url and get from cache' do
|
153
154
|
stub(subject).source_url { data_path }
|
154
155
|
subject.update_rates
|
155
|
-
@global_rates.wont_be_empty
|
156
|
+
_(@global_rates).wont_be_empty
|
156
157
|
dont_allow(subject).source_url
|
157
158
|
subject.update_rates
|
158
|
-
subject.rates.wont_be_empty
|
159
|
+
_(subject.rates).wont_be_empty
|
159
160
|
end
|
160
161
|
end
|
161
162
|
|
@@ -163,23 +164,23 @@ describe Money::Bank::CurrencylayerBank do
|
|
163
164
|
it "should use the non-secure http url if secure_connection isn't set" do
|
164
165
|
subject.secure_connection = nil
|
165
166
|
subject.access_key = TEST_ACCESS_KEY
|
166
|
-
subject.source_url.must_equal "#{url}?source=#{source}&"\
|
167
|
+
_(subject.source_url).must_equal "#{url}?source=#{source}&"\
|
167
168
|
"access_key=#{TEST_ACCESS_KEY}"
|
168
169
|
end
|
169
170
|
|
170
171
|
it 'should use the non-secure http url if secure_connection is false' do
|
171
172
|
subject.secure_connection = false
|
172
173
|
subject.access_key = TEST_ACCESS_KEY
|
173
|
-
subject.source_url.must_equal "#{url}?source=#{source}&"\
|
174
|
+
_(subject.source_url).must_equal "#{url}?source=#{source}&"\
|
174
175
|
"access_key=#{TEST_ACCESS_KEY}"
|
175
176
|
end
|
176
177
|
|
177
178
|
it 'should use the secure https url if secure_connection is set to true' do
|
178
179
|
subject.secure_connection = true
|
179
180
|
subject.access_key = TEST_ACCESS_KEY
|
180
|
-
subject.source_url.must_equal "#{secure_url}?source=#{source}&"\
|
181
|
+
_(subject.source_url).must_equal "#{secure_url}?source=#{source}&"\
|
181
182
|
"access_key=#{TEST_ACCESS_KEY}"
|
182
|
-
subject.source_url.must_include 'https://'
|
183
|
+
_(subject.source_url).must_include 'https://'
|
183
184
|
end
|
184
185
|
end
|
185
186
|
|
@@ -214,7 +215,7 @@ describe Money::Bank::CurrencylayerBank do
|
|
214
215
|
subject.add_rate('USD', 'WTF', 2)
|
215
216
|
subject.add_rate('WTF', 'USD', 2)
|
216
217
|
subject.exchange_with(5000.to_money('WTF'), 'USD').cents
|
217
|
-
subject.exchange_with(5000.to_money('WTF'), 'USD').cents.wont_equal 0
|
218
|
+
_(subject.exchange_with(5000.to_money('WTF'), 'USD').cents).wont_equal 0
|
218
219
|
end
|
219
220
|
end
|
220
221
|
end
|
@@ -226,7 +227,7 @@ describe Money::Bank::CurrencylayerBank do
|
|
226
227
|
end
|
227
228
|
|
228
229
|
it 'should raise an error if no access key is set' do
|
229
|
-
proc { subject.update_rates }.must_raise Money::Bank::NoAccessKey
|
230
|
+
_(proc { subject.update_rates }).must_raise Money::Bank::NoAccessKey
|
230
231
|
end
|
231
232
|
end
|
232
233
|
|
@@ -250,11 +251,11 @@ describe Money::Bank::CurrencylayerBank do
|
|
250
251
|
describe 'when the ttl has expired' do
|
251
252
|
it 'should update the rates' do
|
252
253
|
Timecop.freeze(subject.rates_timestamp + 1000) do
|
253
|
-
subject.get_rate('USD', 'EUR').must_equal @old_usd_eur_rate
|
254
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @old_usd_eur_rate
|
254
255
|
end
|
255
256
|
Timecop.freeze(subject.rates_timestamp + 1001) do
|
256
|
-
subject.get_rate('USD', 'EUR').wont_equal @old_usd_eur_rate
|
257
|
-
subject.get_rate('USD', 'EUR').must_equal @new_usd_eur_rate
|
257
|
+
_(subject.get_rate('USD', 'EUR')).wont_equal @old_usd_eur_rate
|
258
|
+
_(subject.get_rate('USD', 'EUR')).must_equal @new_usd_eur_rate
|
258
259
|
end
|
259
260
|
end
|
260
261
|
|
@@ -262,7 +263,7 @@ describe Money::Bank::CurrencylayerBank do
|
|
262
263
|
Timecop.freeze(subject.rates_timestamp + 1001) do
|
263
264
|
exp_time = subject.rates_timestamp + 1000
|
264
265
|
subject.expire_rates!
|
265
|
-
subject.rates_expiration.must_equal exp_time
|
266
|
+
_(subject.rates_expiration).must_equal exp_time
|
266
267
|
end
|
267
268
|
end
|
268
269
|
end
|
@@ -272,7 +273,7 @@ describe Money::Bank::CurrencylayerBank do
|
|
272
273
|
subject.update_rates
|
273
274
|
exp_time = subject.rates_expiration
|
274
275
|
subject.expire_rates!
|
275
|
-
subject.rates_expiration.must_equal exp_time
|
276
|
+
_(subject.rates_expiration).must_equal exp_time
|
276
277
|
end
|
277
278
|
end
|
278
279
|
end
|
@@ -291,12 +292,12 @@ describe Money::Bank::CurrencylayerBank do
|
|
291
292
|
it 'should return 1970-01-01 datetime if no rates' do
|
292
293
|
stub(subject).open_url { '' }
|
293
294
|
subject.update_rates
|
294
|
-
subject.rates_timestamp.must_equal Time.at(0)
|
295
|
+
_(subject.rates_timestamp).must_equal Time.at(0)
|
295
296
|
end
|
296
297
|
|
297
298
|
it 'should return a Time object' do
|
298
299
|
subject.update_rates
|
299
|
-
subject.rates_timestamp.class.must_equal Time
|
300
|
+
_(subject.rates_timestamp.class).must_equal Time
|
300
301
|
end
|
301
302
|
end
|
302
303
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money-currencylayer-bank
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Egon Zemmer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: money
|
@@ -185,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
185
185
|
- !ruby/object:Gem::Version
|
186
186
|
version: '0'
|
187
187
|
requirements: []
|
188
|
-
rubygems_version: 3.2.
|
188
|
+
rubygems_version: 3.2.24
|
189
189
|
signing_key:
|
190
190
|
specification_version: 4
|
191
191
|
summary: A gem that calculates the exchange rate using published rates from currencylayer.com.
|