money-currencylayer-bank 0.5.7 → 0.6.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: b3c0fb9028cd1aeffa15e39f1ac11611a98ebc98c4388b3d7e15c3926f186ae2
4
- data.tar.gz: 4cd0872bd7ddf5957a80b344a781656532b98c77b4eaf69f1d5b36eb6b4b4a48
3
+ metadata.gz: 2cd0e712f363b756776096cd7cb5ff5b3fbb473147f5659a524f6cd3a4d675d9
4
+ data.tar.gz: 415066f35b6dd18c2f86d9259003c26559a73a042c52c4192ab97247c0b23856
5
5
  SHA512:
6
- metadata.gz: cddb985e9d2f028674acff2e4f5605ff9d504396abbcec1e63cadadc3ea7a9620517e9af28d622fccf4729a70e0500918b6f9d218e38d01d42dd1fb91cc00581
7
- data.tar.gz: b138d9a8d0ca0fa1ece3d4c576088ad7fe6e023b96fd3d21537e048764df887c871e9eebc2cfe8c425bca682aa20e63d2e1a38ebc0259611ee2a57f8bfb9e7a3
6
+ metadata.gz: cac0709ae73bbc1efac414dde9b770793905a3e9a5c50abc4fa1ddab7040c0626b776c5c46be46b6ca234bfed8a220c11089f62a705009f1f08dbc214bda08e5
7
+ data.tar.gz: 34e550142270e65d075183ebe528306d749f8ceaa115bd5ad3e09d1193bcc415183443f81f1ed2d2354392c4dda8616dc205fc9f5d16b4b8b30b1507793980a6
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](http://img.shields.io/codeclimate/github/phlegx/money-currencylayer-bank.svg)](https://codeclimate.com/github/phlegx/money-currencylayer-bank)
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)
6
+ [![Code Climate](https://codeclimate.com/github/phlegx/money-currencylayer-bank.svg)](https://codeclimate.com/github/phlegx/money-currencylayer-bank)
7
+ [![Inline Docs](https://inch-ci.org/github/phlegx/money-currencylayer-bank.svg?branch=master)](http://inch-ci.org/github/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
82
- # by default, they never expire, in this example 1 day.
74
+ # Set the seconds after than the current rates are automatically expired.
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
- # Set money default bank to Currencylayer bank
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) 2017 Phlegx Systems OG
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://apilayer.net/api/live'.freeze
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
@@ -155,6 +155,8 @@ class Money
155
155
  # Check if rates are expired
156
156
  # @return [Boolean] true if rates are expired
157
157
  def expired?
158
+ return false if ttl_in_seconds.zero?
159
+
158
160
  Time.now > rates_expiration
159
161
  end
160
162
 
@@ -240,7 +242,7 @@ class Money
240
242
  # Opens an url and reads the content
241
243
  # @return [String] unparsed JSON content
242
244
  def open_url
243
- ::OpenURI.open_uri(source_url).read
245
+ URI.open(source_url).read
244
246
  rescue OpenURI::HTTPError
245
247
  ''
246
248
  end
@@ -268,11 +270,18 @@ class Money
268
270
  # @param straight [Boolean] true for straight, default is careful
269
271
  # @return [Hash] key is country code (ISO 3166-1 alpha-3) value Float
270
272
  def exchange_rates(straight = false)
271
- @rates = if straight
272
- raw_rates_straight['quotes']
273
- else
274
- raw_rates_careful['quotes']
275
- end
273
+ rates = if straight
274
+ raw_rates_straight
275
+ else
276
+ raw_rates_careful
277
+ end
278
+ if rates.key?('quotes')
279
+ @rates = rates['quotes']
280
+ elsif rates.key?('error')
281
+ raise Error, rates.dig('error', 'info')
282
+ else
283
+ raise Error, 'Unknown rates situation!'
284
+ end
276
285
  end
277
286
 
278
287
  # 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.7
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Egon Zemmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-26 00:00:00.000000000 Z
11
+ date: 2022-08-08 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.16
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.