money-currencylayer-bank 0.6.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2cd0e712f363b756776096cd7cb5ff5b3fbb473147f5659a524f6cd3a4d675d9
4
- data.tar.gz: 415066f35b6dd18c2f86d9259003c26559a73a042c52c4192ab97247c0b23856
3
+ metadata.gz: c3f50af5ecdfe22212d99381d062f3db9bec09415f0734b54db59b64a8f42d47
4
+ data.tar.gz: 60aebccb74c462df8a0a8d7c48a73e86f8bb75f52c6520c7478792475416087c
5
5
  SHA512:
6
- metadata.gz: cac0709ae73bbc1efac414dde9b770793905a3e9a5c50abc4fa1ddab7040c0626b776c5c46be46b6ca234bfed8a220c11089f62a705009f1f08dbc214bda08e5
7
- data.tar.gz: 34e550142270e65d075183ebe528306d749f8ceaa115bd5ad3e09d1193bcc415183443f81f1ed2d2354392c4dda8616dc205fc9f5d16b4b8b30b1507793980a6
6
+ metadata.gz: e0dd0f1cf80928b7b293ae220bdd684701aa5a03eee129520505940baef899225f27a360ff13a04798f51607a8802a551376eb0e025942266389788b1ddb93fb
7
+ data.tar.gz: 41fce81a38a14709b7439751d7ab3d1c944fb901eb9749a68cfbbc02b623d2809838a3849cb35e6679a71eeaef85d171fe008f7cba7a0d5a491fc88369c6a17f
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2017 Phlegx Systems OG
3
+ Copyright (c) 2022 Phlegx Systems OG
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -63,7 +63,14 @@ Or install it yourself as:
63
63
  # Minimal requirements.
64
64
  require 'money/bank/currencylayer_bank'
65
65
  mclb = Money::Bank::CurrencylayerBank.new
66
- mclb.access_key = 'your access_key from https://currencylayer.com/product'
66
+
67
+ # New endpoint: https://apilayer.com/marketplace/currency_data-api or
68
+ # old endpoint: https://currencylayer.com/product
69
+ mclb.access_key = 'your access_key'
70
+
71
+ # (optional)
72
+ # Use the old endpoint api.currencylayer.com. By default, the new endpoint is used.
73
+ mclb.currencylayer = true
67
74
 
68
75
  # (optional)
69
76
  # Set the base currency for all rates. By default, USD is used.
@@ -80,6 +87,10 @@ mclb.ttl_in_seconds = 86400
80
87
  # CurrencylayerBank only allows http as connection for the free plan users.
81
88
  mclb.secure_connection = true
82
89
 
90
+ # (optional)
91
+ # Rescue with rates from the cache instead of reporting an error when the endpoint fails.
92
+ mclb.rescue_with_cache = true
93
+
83
94
  # Define cache (string or pathname).
84
95
  mclb.cache = 'path/to/file/cache'
85
96
 
@@ -103,9 +114,15 @@ Money.default_bank = mclb
103
114
  ~~~ ruby
104
115
  mclb = Money::Bank::CurrencylayerBank.new
105
116
 
117
+ # Returns true if configured to use the old endpoint.
118
+ mclb.currencylayer
119
+
106
120
  # Returns the base currency set for all rates.
107
121
  mclb.source
108
122
 
123
+ # Returns true if configured to rescue rates from the cache.
124
+ mclb.rescue_with_cache
125
+
109
126
  # Expires rates if the expiration time is reached.
110
127
  mclb.expire_rates!
111
128
 
@@ -30,12 +30,20 @@ class Money
30
30
 
31
31
  # CurrencylayerBank base class
32
32
  class CurrencylayerBank < Money::Bank::VariableExchange
33
+ # Apilayer url
34
+ URL_AL = 'http://api.apilayer.com/currency_data/live'.freeze
33
35
  # CurrencylayerBank url
34
- CL_URL = 'http://api.currencylayer.com/live'.freeze
35
- # CurrencylayerBank secure url
36
- CL_SECURE_URL = CL_URL.sub('http:', 'https:')
36
+ URL_CL = 'http://api.currencylayer.com/live'.freeze
37
37
  # Default base currency
38
- CL_SOURCE = 'USD'.freeze
38
+ SOURCE = 'USD'.freeze
39
+
40
+ # Use new or old endpoint.
41
+ # new: api.apilayer.com
42
+ # old: api.currencylayer.com
43
+ #
44
+ # @param value [Boolean] true for old endpoint
45
+ # @return [Boolean] chosen old endpoint if true
46
+ attr_accessor :currencylayer
39
47
 
40
48
  # Use https to fetch rates from CurrencylayerBank
41
49
  # CurrencylayerBank only allows http as connection
@@ -51,6 +59,13 @@ class Money
51
59
  # @return [String] chosen API access key
52
60
  attr_accessor :access_key
53
61
 
62
+ # Rescue with rates from the cache instead of reporting an
63
+ # error when the endpoint fails.
64
+ #
65
+ # @param value [Boolean] true for rescue error with cache rates
66
+ # @return [Boolean] chosen rescue with cache rates
67
+ attr_accessor :rescue_with_cache
68
+
54
69
  # Cache accessor, can be a String or a Proc
55
70
  #
56
71
  # @param value [String,Pathname,Proc] cache system
@@ -84,13 +99,13 @@ class Money
84
99
  # @param value [String] Currency code, ISO 3166-1 alpha-3
85
100
  # @return [String] chosen base currency
86
101
  def source=(value)
87
- @source = Money::Currency.find(value.to_s).try(:iso_code) || CL_SOURCE
102
+ @source = Money::Currency.find(value.to_s).try(:iso_code) || SOURCE
88
103
  end
89
104
 
90
105
  # Get the base currency for all rates. By default, USD is used.
91
106
  # @return [String] base currency
92
107
  def source
93
- @source ||= CL_SOURCE
108
+ @source ||= SOURCE
94
109
  end
95
110
 
96
111
  # Get the seconds after than the current rates are automatically expired
@@ -173,9 +188,10 @@ class Money
173
188
  # @return [String] the remote API url
174
189
  def source_url
175
190
  raise NoAccessKey if access_key.nil? || access_key.empty?
176
- cl_url = CL_URL
177
- cl_url = CL_SECURE_URL if secure_connection
178
- "#{cl_url}?source=#{source}&access_key=#{access_key}"
191
+ url = "#{currencylayer ? URL_CL : URL_AL}?source=#{source}"
192
+ url = url.sub('http:', 'https:') if secure_connection
193
+ url = "#{url}&access_key=#{access_key}" if currencylayer
194
+ url
179
195
  end
180
196
 
181
197
  # Get rates expiration time based on ttl
@@ -242,7 +258,7 @@ class Money
242
258
  # Opens an url and reads the content
243
259
  # @return [String] unparsed JSON content
244
260
  def open_url
245
- URI.open(source_url).read
261
+ currencylayer ? URI.open(source_url).read : URI.open(source_url, apikey: access_key).read
246
262
  rescue OpenURI::HTTPError
247
263
  ''
248
264
  end
@@ -270,15 +286,14 @@ class Money
270
286
  # @param straight [Boolean] true for straight, default is careful
271
287
  # @return [Hash] key is country code (ISO 3166-1 alpha-3) value Float
272
288
  def exchange_rates(straight = false)
273
- rates = if straight
274
- raw_rates_straight
275
- else
276
- raw_rates_careful
277
- end
289
+ rates = straight ? raw_rates_straight : raw_rates_careful
278
290
  if rates.key?('quotes')
279
291
  @rates = rates['quotes']
280
292
  elsif rates.key?('error')
281
- raise Error, rates.dig('error', 'info')
293
+ raise Error, rates.dig('error', 'info') unless rescue_with_cache
294
+
295
+ rates = raw_rates_careful(false)
296
+ @rates = rates.key?('quotes') ? rates['quotes'] : { 'quotes' => {} }
282
297
  else
283
298
  raise Error, 'Unknown rates situation!'
284
299
  end
data/test/TEST_ACCESS_KEY CHANGED
@@ -1 +1 @@
1
- your access_key from https://currencylayer.com/product
1
+ your access_key from https://currencylayer.com/product or https://apilayer.com/marketplace/currency_data-api
@@ -6,9 +6,11 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
6
6
  describe Money::Bank::CurrencylayerBank do
7
7
  Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
8
8
  subject { Money::Bank::CurrencylayerBank.new }
9
- let(:url) { Money::Bank::CurrencylayerBank::CL_URL }
10
- let(:secure_url) { Money::Bank::CurrencylayerBank::CL_SECURE_URL }
11
- let(:source) { Money::Bank::CurrencylayerBank::CL_SOURCE }
9
+ let(:url_al) { Money::Bank::CurrencylayerBank::URL_AL }
10
+ let(:url_cl) { Money::Bank::CurrencylayerBank::URL_CL }
11
+ let(:secure_url_al) { Money::Bank::CurrencylayerBank::URL_AL.sub('http:', 'https:') }
12
+ let(:secure_url_cl) { Money::Bank::CurrencylayerBank::URL_CL.sub('http:', 'https:') }
13
+ let(:source) { Money::Bank::CurrencylayerBank::SOURCE }
12
14
  let(:temp_cache_path) do
13
15
  File.expand_path(File.join(File.dirname(__FILE__), 'temp.json'))
14
16
  end
@@ -161,24 +163,46 @@ describe Money::Bank::CurrencylayerBank do
161
163
  end
162
164
 
163
165
  describe '#secure_connection' do
164
- it "should use the non-secure http url if secure_connection isn't set" do
166
+ it "should use the non-secure http apilayer.com url if secure_connection isn't set" do
165
167
  subject.secure_connection = nil
166
168
  subject.access_key = TEST_ACCESS_KEY
167
- _(subject.source_url).must_equal "#{url}?source=#{source}&"\
169
+ _(subject.source_url).must_equal "#{url_al}?source=#{source}"
170
+ end
171
+
172
+ it 'should use the non-secure http apilayer.com url if secure_connection is false' do
173
+ subject.secure_connection = false
174
+ subject.access_key = TEST_ACCESS_KEY
175
+ _(subject.source_url).must_equal "#{url_al}?source=#{source}"
176
+ end
177
+
178
+ it 'should use the secure https apilayer.com url if secure_connection is set to true' do
179
+ subject.secure_connection = true
180
+ subject.access_key = TEST_ACCESS_KEY
181
+ _(subject.source_url).must_equal "#{secure_url_al}?source=#{source}"
182
+ _(subject.source_url).must_include 'https://'
183
+ end
184
+
185
+ it "should use the non-secure http currencylayer.com url if secure_connection isn't set" do
186
+ subject.secure_connection = nil
187
+ subject.currencylayer = true
188
+ subject.access_key = TEST_ACCESS_KEY
189
+ _(subject.source_url).must_equal "#{url_cl}?source=#{source}&"\
168
190
  "access_key=#{TEST_ACCESS_KEY}"
169
191
  end
170
192
 
171
- it 'should use the non-secure http url if secure_connection is false' do
193
+ it 'should use the non-secure http currencylayer.com url if secure_connection is false' do
172
194
  subject.secure_connection = false
195
+ subject.currencylayer = true
173
196
  subject.access_key = TEST_ACCESS_KEY
174
- _(subject.source_url).must_equal "#{url}?source=#{source}&"\
197
+ _(subject.source_url).must_equal "#{url_cl}?source=#{source}&"\
175
198
  "access_key=#{TEST_ACCESS_KEY}"
176
199
  end
177
200
 
178
- it 'should use the secure https url if secure_connection is set to true' do
201
+ it 'should use the secure https currencylayer.com url if secure_connection is set to true' do
179
202
  subject.secure_connection = true
203
+ subject.currencylayer = true
180
204
  subject.access_key = TEST_ACCESS_KEY
181
- _(subject.source_url).must_equal "#{secure_url}?source=#{source}&"\
205
+ _(subject.source_url).must_equal "#{secure_url_cl}?source=#{source}&"\
182
206
  "access_key=#{TEST_ACCESS_KEY}"
183
207
  _(subject.source_url).must_include 'https://'
184
208
  end
@@ -223,7 +247,7 @@ describe Money::Bank::CurrencylayerBank do
223
247
  describe '#access_key' do
224
248
  before do
225
249
  subject.cache = temp_cache_path
226
- stub(OpenURI::OpenRead).open(url) { File.read data_path }
250
+ stub(OpenURI::OpenRead).open(url_al) { File.read data_path }
227
251
  end
228
252
 
229
253
  it 'should raise an error if no access key is set' do
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.6.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Egon Zemmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-08 00:00:00.000000000 Z
11
+ date: 2022-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money
@@ -150,8 +150,8 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: 0.7.1
153
- description: A gem that calculates the exchange rate using published rates from currencylayer.com.
154
- Compatible with the money gem.
153
+ description: A gem that calculates the exchange rate using published rates from currencylayer.com
154
+ and apilayer.com. Compatible with the money gem.
155
155
  email: office@phlegx.com
156
156
  executables: []
157
157
  extensions: []
@@ -188,6 +188,7 @@ requirements: []
188
188
  rubygems_version: 3.2.24
189
189
  signing_key:
190
190
  specification_version: 4
191
- summary: A gem that calculates the exchange rate using published rates from currencylayer.com.
191
+ summary: A gem that calculates the exchange rate using published rates from currencylayer.com
192
+ and apilayer.com.
192
193
  test_files:
193
194
  - test/currencylayer_bank_test.rb