money-currencylayer-bank 0.5.8 → 0.7.0

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: ad15fc05b8e56c61e10554fd8574e0dbd358e3adc287c140ee11cb4b5c5747d8
4
- data.tar.gz: 3ace2bd923ee971f5f6fca67f5ada19a0edf25687fbf0806b197f8876ee33146
3
+ metadata.gz: 468cd2adad236896e66ed821576b5bd5df64931909a85f41f48e171c8c3fd6b8
4
+ data.tar.gz: e09e3d6aff104763a6b069747e302dd7baa9d76d97d729d34dd186fc2a4eb8c9
5
5
  SHA512:
6
- metadata.gz: 25849b05854111ab7ec51474fdbe9d02a5dd03d80719d46f0358a1c9c87b63e6296f2edcacd46e65e975e5510f16a456b4be50f1c92aa87c2b9af81504d5cf49
7
- data.tar.gz: ce7667380baef8501825232ff7425b3a3121e85f0bfc4d8656d4c8b310e50d84ed787e6384d7da1bd692bf44233cccbaadee7dcf559232aece1ef83b28f258ec
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
@@ -4,7 +4,7 @@
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
6
  [![Code Climate](https://codeclimate.com/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)
7
+ [![Inline Docs](https://inch-ci.org/github/phlegx/money-currencylayer-bank.svg?branch=master)](http://inch-ci.org/github/phlegx/money-currencylayer-bank)
8
8
  [![License](https://img.shields.io/github/license/phlegx/money-currencylayer-bank.svg)](http://opensource.org/licenses/MIT)
9
9
 
10
10
  A gem that calculates the exchange rate using published rates from
@@ -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.
@@ -72,7 +79,7 @@ mclb.source = 'EUR'
72
79
 
73
80
  # (optional)
74
81
  # Set the seconds after than the current rates are automatically expired.
75
- # by default, they never expire, in this example 1 day.
82
+ # By default, they never expire, in this example 1 day.
76
83
  mclb.ttl_in_seconds = 86400
77
84
 
78
85
  # (optional)
@@ -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
@@ -155,6 +170,8 @@ class Money
155
170
  # Check if rates are expired
156
171
  # @return [Boolean] true if rates are expired
157
172
  def expired?
173
+ return false if ttl_in_seconds.zero?
174
+
158
175
  Time.now > rates_expiration
159
176
  end
160
177
 
@@ -171,9 +188,10 @@ class Money
171
188
  # @return [String] the remote API url
172
189
  def source_url
173
190
  raise NoAccessKey if access_key.nil? || access_key.empty?
174
- cl_url = CL_URL
175
- cl_url = CL_SECURE_URL if secure_connection
176
- "#{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
177
195
  end
178
196
 
179
197
  # Get rates expiration time based on ttl
@@ -241,6 +259,7 @@ class Money
241
259
  # @return [String] unparsed JSON content
242
260
  def open_url
243
261
  URI.open(source_url).read
262
+ currencylayer ? URI.open(source_url).read : URI.open(source_url, apikey: access_key).read
244
263
  rescue OpenURI::HTTPError
245
264
  ''
246
265
  end
@@ -268,15 +287,14 @@ class Money
268
287
  # @param straight [Boolean] true for straight, default is careful
269
288
  # @return [Hash] key is country code (ISO 3166-1 alpha-3) value Float
270
289
  def exchange_rates(straight = false)
271
- rates = if straight
272
- raw_rates_straight
273
- else
274
- raw_rates_careful
275
- end
290
+ rates = straight ? raw_rates_straight : raw_rates_careful
276
291
  if rates.key?('quotes')
277
292
  @rates = rates['quotes']
278
293
  elsif rates.key?('error')
279
- 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' => {} }
280
298
  else
281
299
  raise Error, 'Unknown rates situation!'
282
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.5.8
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-02-15 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