money-currencylayer-bank 0.6.0 → 0.7.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: 2cd0e712f363b756776096cd7cb5ff5b3fbb473147f5659a524f6cd3a4d675d9
4
- data.tar.gz: 415066f35b6dd18c2f86d9259003c26559a73a042c52c4192ab97247c0b23856
3
+ metadata.gz: 468cd2adad236896e66ed821576b5bd5df64931909a85f41f48e171c8c3fd6b8
4
+ data.tar.gz: e09e3d6aff104763a6b069747e302dd7baa9d76d97d729d34dd186fc2a4eb8c9
5
5
  SHA512:
6
- metadata.gz: cac0709ae73bbc1efac414dde9b770793905a3e9a5c50abc4fa1ddab7040c0626b776c5c46be46b6ca234bfed8a220c11089f62a705009f1f08dbc214bda08e5
7
- data.tar.gz: 34e550142270e65d075183ebe528306d749f8ceaa115bd5ad3e09d1193bcc415183443f81f1ed2d2354392c4dda8616dc205fc9f5d16b4b8b30b1507793980a6
6
+ metadata.gz: 29aed62e0d5e6eef896a2ce8136e42549fede2fe9c84dba9a0ba294dfc8d815fd8a93790cc6350463c40fbc243d5c80563d2fe02e51c04ed810e0a72978fa622
7
+ data.tar.gz: 971a319e510241c517480c2212ff5c9c938eb5eb67f234e9cd5545d76a12766e8f28ff2a967f161a123e1925e8ba7410d9960078620ea31e1521ffdd27822919
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
@@ -243,6 +259,7 @@ class Money
243
259
  # @return [String] unparsed JSON content
244
260
  def open_url
245
261
  URI.open(source_url).read
262
+ currencylayer ? URI.open(source_url).read : URI.open(source_url, apikey: access_key).read
246
263
  rescue OpenURI::HTTPError
247
264
  ''
248
265
  end
@@ -270,15 +287,14 @@ class Money
270
287
  # @param straight [Boolean] true for straight, default is careful
271
288
  # @return [Hash] key is country code (ISO 3166-1 alpha-3) value Float
272
289
  def exchange_rates(straight = false)
273
- rates = if straight
274
- raw_rates_straight
275
- else
276
- raw_rates_careful
277
- end
290
+ rates = straight ? raw_rates_straight : raw_rates_careful
278
291
  if rates.key?('quotes')
279
292
  @rates = rates['quotes']
280
293
  elsif rates.key?('error')
281
- raise Error, rates.dig('error', 'info')
294
+ raise Error, rates.dig('error', 'info') unless rescue_with_cache
295
+
296
+ rates = raw_rates_careful(false)
297
+ @rates = rates.key?('quotes') ? rates['quotes'] : { 'quotes' => {} }
282
298
  else
283
299
  raise Error, 'Unknown rates situation!'
284
300
  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.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: 2022-08-08 00:00:00.000000000 Z
11
+ date: 2022-08-25 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