money-open-exchange-rates 0.7.0 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
- require 'uri'
3
- require 'open-uri'
4
- require 'money'
2
+
5
3
  require 'json'
6
- require File.expand_path('../../../open_exchange_rates_bank/version', __FILE__)
4
+ require 'money'
5
+ require 'net/http'
6
+ require 'time'
7
+ require 'uri'
8
+ require File.expand_path('../../open_exchange_rates_bank/version', __dir__)
7
9
 
8
10
  # Money gem class
9
- # rubocop:disable ClassLength
11
+ # rubocop:disable Metrics/ClassLength
10
12
  class Money
11
13
  # https://github.com/RubyMoney/money#exchange-rate-stores
12
14
  module Bank
@@ -16,31 +18,30 @@ class Money
16
18
  # APP_ID not set error
17
19
  class NoAppId < StandardError; end
18
20
 
21
+ # Access restricted (e.g. usage/request limit exceeded for account)
22
+ class AccessRestricted < StandardError; end
23
+
24
+ # app_id_inactive
25
+ class AppIdInactive < StandardError; end
26
+
27
+ ERROR_MAP = {
28
+ access_restricted: AccessRestricted,
29
+ app_id_inactive: AppIdInactive
30
+ }.freeze
31
+
19
32
  # OpenExchangeRatesBank base class
20
33
  class OpenExchangeRatesBank < Money::Bank::VariableExchange
21
34
  VERSION = ::OpenExchangeRatesBank::VERSION
22
- BASE_URL = 'http://openexchangerates.org/api/'.freeze
35
+ BASE_URL = 'https://openexchangerates.org/api/'
36
+
23
37
  # OpenExchangeRates urls
24
38
  OER_URL = URI.join(BASE_URL, 'latest.json')
25
39
  OER_HISTORICAL_URL = URI.join(BASE_URL, 'historical/')
26
- # OpenExchangeRates secure url
27
- SECURE_OER_URL = OER_URL.clone
28
- SECURE_OER_URL.scheme = 'https'
29
- SECURE_OER_HISTORICAL_URL = OER_HISTORICAL_URL.clone
30
- SECURE_OER_HISTORICAL_URL.scheme = 'https'
31
40
 
32
41
  # Default base currency "base": "USD"
33
- OE_SOURCE = 'USD'.freeze
34
-
35
- # use https to fetch rates from Open Exchange Rates
36
- # disabled by default to support free-tier users
37
- #
38
- # @example
39
- # oxr.secure_connection = true
40
- #
41
- # @param [Boolean] true for https, false for http
42
- # @return [Boolean] true for https, false for http
43
- attr_accessor :secure_connection
42
+ OE_SOURCE = 'USD'
43
+ RATES_KEY = 'rates'
44
+ TIMESTAMP_KEY = 'timestamp'
44
45
 
45
46
  # As of the end of August 2012 all requests to the Open Exchange Rates
46
47
  # API must have a valid app_id
@@ -72,6 +73,13 @@ class Money
72
73
  # @return [String] The requested date in YYYY-MM-DD format
73
74
  attr_accessor :date
74
75
 
76
+ # Force refresh rates cache and store on the fly when ttl is expired
77
+ # This will slow down request on get_rate, so use at your on risk, if you
78
+ # don't want to setup crontab/worker/scheduler for your application
79
+ #
80
+ # @param [Boolean]
81
+ attr_accessor :force_refresh_rate_on_expire
82
+
75
83
  # Rates expiration Time
76
84
  #
77
85
  # @return [Time] expiration time
@@ -82,11 +90,57 @@ class Money
82
90
  # @return [Hash] All rates as Hash
83
91
  attr_reader :oer_rates
84
92
 
93
+ # Unparsed OpenExchangeRates response as String
94
+ #
95
+ # @return [String] OpenExchangeRates json response
96
+ attr_reader :json_response
97
+
85
98
  # Seconds after than the current rates are automatically expired
86
99
  #
87
100
  # @return [Integer] Setted time to live in seconds
88
101
  attr_reader :ttl_in_seconds
89
102
 
103
+ # Set support for the black market and alternative digital currencies
104
+ # see https://docs.openexchangerates.org/docs/alternative-currencies
105
+ # @example
106
+ # oxr.show_alternative = true
107
+ #
108
+ # @param [Boolean] if true show alternative
109
+ # @return [Boolean] Setted show alternative
110
+ attr_writer :show_alternative
111
+
112
+ # Filter response to a list of symbols
113
+ # see https://docs.openexchangerates.org/docs/get-specific-currencies
114
+ # @example
115
+ # oxr.symbols = [:usd, :cad]
116
+ #
117
+ # @param [Array] list of symbols
118
+ # @return [Array] Setted list of symbols
119
+ attr_writer :symbols
120
+
121
+ # Minified Response ('prettyprint')
122
+ # see https://docs.openexchangerates.org/docs/prettyprint
123
+ # @example
124
+ # oxr.prettyprint = false
125
+ #
126
+ # @param [Boolean] Set to false to receive minified (default: true)
127
+ # @return [Boolean]
128
+ attr_writer :prettyprint
129
+
130
+ # Set current rates timestamp
131
+ #
132
+ # @return [Time]
133
+ def rates_timestamp=(at)
134
+ @rates_timestamp = Time.at(at)
135
+ end
136
+
137
+ # Current rates timestamp
138
+ #
139
+ # @return [Time]
140
+ def rates_timestamp
141
+ @rates_timestamp || Time.now
142
+ end
143
+
90
144
  # Set the seconds after than the current rates are automatically expired
91
145
  # by default, they never expire.
92
146
  #
@@ -99,7 +153,7 @@ class Money
99
153
  def ttl_in_seconds=(value)
100
154
  @ttl_in_seconds = value
101
155
  refresh_rates_expiration if ttl_in_seconds
102
- @ttl_in_seconds
156
+ ttl_in_seconds
103
157
  end
104
158
 
105
159
  # Set the base currency for all rates. By default, USD is used.
@@ -113,9 +167,12 @@ class Money
113
167
  #
114
168
  # @return [String] chosen base currency
115
169
  def source=(value)
116
- @source = Money::Currency.find(value.to_s).iso_code
117
- rescue
118
- @source = OE_SOURCE
170
+ scurrency = Money::Currency.find(value.to_s)
171
+ @source = if scurrency
172
+ scurrency.iso_code
173
+ else
174
+ OE_SOURCE
175
+ end
119
176
  end
120
177
 
121
178
  # Get the base currency for all rates. By default, USD is used.
@@ -129,27 +186,19 @@ class Money
129
186
  #
130
187
  # @return [Array] Array of exchange rates
131
188
  def update_rates
132
- exchange_rates.each do |exchange_rate|
133
- rate = exchange_rate.last
134
- currency = exchange_rate.first
135
- next unless Money::Currency.find(currency)
136
- set_rate(source, currency, rate)
137
- set_rate(currency, source, 1.0 / rate)
189
+ store.transaction do
190
+ clear_rates!
191
+ exchange_rates.each do |exchange_rate|
192
+ rate = exchange_rate.last
193
+ currency = exchange_rate.first
194
+ next unless Money::Currency.find(currency)
195
+
196
+ set_rate(source, currency, rate)
197
+ set_rate(currency, source, 1.0 / rate)
198
+ end
138
199
  end
139
200
  end
140
201
 
141
- # Save rates on cache
142
- # Can raise InvalidCache
143
- #
144
- # @return [Proc,File]
145
- def save_rates
146
- raise InvalidCache unless cache
147
- text = read_from_url
148
- store_in_cache(text) if valid_rates?(text)
149
- rescue Errno::ENOENT
150
- raise InvalidCache
151
- end
152
-
153
202
  # Alias super method
154
203
  alias super_get_rate get_rate
155
204
 
@@ -166,30 +215,77 @@ class Money
166
215
  rate || calc_pair_rate_using_base(from_currency, to_currency, opts)
167
216
  end
168
217
 
218
+ # Fetch from url and save cache
219
+ #
220
+ # @return [Array] Array of exchange rates
221
+ def refresh_rates
222
+ read_from_url
223
+ end
224
+
225
+ # Alias refresh_rates method
226
+ alias save_rates refresh_rates
227
+
169
228
  # Expire rates when expired
170
229
  #
171
230
  # @return [NilClass, Time] nil if not expired or new expiration time
172
231
  def expire_rates
173
232
  return unless ttl_in_seconds
174
233
  return if rates_expiration > Time.now
234
+
235
+ refresh_rates if force_refresh_rate_on_expire
175
236
  update_rates
176
237
  refresh_rates_expiration
177
238
  end
178
239
 
240
+ # Get show alternative
241
+ #
242
+ # @return [Boolean] if true show alternative
243
+ def show_alternative
244
+ @show_alternative ||= false
245
+ end
246
+
247
+ # Get prettyprint option
248
+ #
249
+ # @return [Boolean]
250
+ def prettyprint
251
+ return true unless defined? @prettyprint
252
+ return true if @prettyprint.nil?
253
+
254
+ @prettyprint
255
+ end
256
+
257
+ # Get symbols
258
+ #
259
+ # @return [Array] list of symbols to filter by
260
+ def symbols
261
+ @symbols ||= nil
262
+ end
263
+
179
264
  # Source url of openexchangerates
180
- # defined with app_id and secure_connection
265
+ # defined with app_id
181
266
  #
182
267
  # @return [String] URL
183
268
  def source_url
184
- if source == OE_SOURCE
185
- "#{oer_url}?app_id=#{app_id}"
186
- else
187
- "#{oer_url}?app_id=#{app_id}&base=#{source}"
188
- end
269
+ str = "#{oer_url}?app_id=#{app_id}"
270
+ str = "#{str}&base=#{source}" unless source == OE_SOURCE
271
+ str = "#{str}&show_alternative=#{show_alternative}"
272
+ str = "#{str}&prettyprint=#{prettyprint}"
273
+ str = "#{str}&symbols=#{symbols.join(',')}" if symbols&.is_a?(Array)
274
+ str
189
275
  end
190
276
 
191
277
  protected
192
278
 
279
+ # Save rates on cache
280
+ # Can raise InvalidCache
281
+ #
282
+ # @return [Proc,File]
283
+ def save_cache
284
+ store_in_cache(@json_response) if valid_rates?(@json_response)
285
+ rescue Errno::ENOENT
286
+ raise InvalidCache
287
+ end
288
+
193
289
  # Latest url if no date given
194
290
  #
195
291
  # @return [String] URL
@@ -205,21 +301,19 @@ class Money
205
301
  #
206
302
  # @return [String] URL
207
303
  def historical_url
208
- url = OER_HISTORICAL_URL
209
- url = SECURE_OER_HISTORICAL_URL if secure_connection
210
- URI.join(url, "#{date}.json")
304
+ URI.join(OER_HISTORICAL_URL, "#{date}.json")
211
305
  end
212
306
 
213
307
  # Latest url
214
308
  #
215
309
  # @return [String] URL
216
310
  def latest_url
217
- return SECURE_OER_URL if secure_connection
218
311
  OER_URL
219
312
  end
220
313
 
221
314
  # Store the provided text data by calling the proc method provided
222
315
  # for the cache, or write to the cache file.
316
+ # Can raise InvalidCache
223
317
  #
224
318
  # @example
225
319
  # oxr.store_in_cache("{\"rates\": {\"AED\": 3.67304}}")
@@ -229,10 +323,12 @@ class Money
229
323
  def store_in_cache(text)
230
324
  if cache.is_a?(Proc)
231
325
  cache.call(text)
232
- elsif cache.is_a?(String)
233
- open(cache, 'w') do |f|
326
+ elsif cache.is_a?(String) || cache.is_a?(Pathname)
327
+ File.open(cache.to_s, 'w') do |f|
234
328
  f.write(text)
235
329
  end
330
+ else
331
+ raise InvalidCache
236
332
  end
237
333
  end
238
334
 
@@ -240,11 +336,19 @@ class Money
240
336
  #
241
337
  # @return [String] Raw string from file or cache proc
242
338
  def read_from_cache
243
- if cache.is_a?(Proc)
244
- cache.call(nil)
245
- elsif cache.is_a?(String) && File.exist?(cache)
246
- open(cache).read
247
- end
339
+ result = if cache.is_a?(Proc)
340
+ cache.call(nil)
341
+ elsif File.exist?(cache.to_s)
342
+ File.read(cache)
343
+ end
344
+ result if valid_rates?(result)
345
+ end
346
+
347
+ # Read API
348
+ #
349
+ # @return [String]
350
+ def api_response
351
+ Net::HTTP.get(URI(source_url))
248
352
  end
249
353
 
250
354
  # Read from url
@@ -252,7 +356,10 @@ class Money
252
356
  # @return [String] JSON content
253
357
  def read_from_url
254
358
  raise NoAppId if app_id.nil? || app_id.empty?
255
- open(source_url).read
359
+
360
+ @json_response = api_response
361
+ save_cache if cache
362
+ @json_response
256
363
  end
257
364
 
258
365
  # Check validity of rates response only for store in cache
@@ -263,8 +370,10 @@ class Money
263
370
  # @param [String] text is JSON content
264
371
  # @return [Boolean] valid or not
265
372
  def valid_rates?(text)
373
+ return false unless text
374
+
266
375
  parsed = JSON.parse(text)
267
- parsed && parsed.key?('rates')
376
+ parsed&.key?(RATES_KEY) && parsed&.key?(TIMESTAMP_KEY)
268
377
  rescue JSON::ParserError
269
378
  false
270
379
  end
@@ -274,14 +383,19 @@ class Money
274
383
  # @return [Hash] key is country code (ISO 3166-1 alpha-3) value Float
275
384
  def exchange_rates
276
385
  doc = JSON.parse(read_from_cache || read_from_url)
277
- @oer_rates = doc['rates']
386
+ if doc['error'] && ERROR_MAP.key?(doc['message'].to_sym)
387
+ raise ERROR_MAP[doc['message'].to_sym]
388
+ end
389
+
390
+ self.rates_timestamp = doc[TIMESTAMP_KEY]
391
+ @oer_rates = doc[RATES_KEY]
278
392
  end
279
393
 
280
394
  # Refresh expiration from now
281
395
  #
282
396
  # @return [Time] new expiration time
283
397
  def refresh_rates_expiration
284
- @rates_expiration = Time.now + ttl_in_seconds
398
+ @rates_expiration = rates_timestamp + ttl_in_seconds
285
399
  end
286
400
 
287
401
  # Get rate or calculate it as inverse rate
@@ -312,13 +426,23 @@ class Money
312
426
  def calc_pair_rate_using_base(from_currency, to_currency, opts)
313
427
  from_base_rate = get_rate_or_calc_inverse(source, from_currency, opts)
314
428
  to_base_rate = get_rate_or_calc_inverse(source, to_currency, opts)
315
- if to_base_rate && from_base_rate
316
- rate = to_base_rate.to_f / from_base_rate
317
- add_rate(from_currency, to_currency, rate)
318
- return rate
429
+ return unless to_base_rate
430
+ return unless from_base_rate
431
+
432
+ rate = BigDecimal(to_base_rate.to_s) / from_base_rate
433
+ add_rate(from_currency, to_currency, rate)
434
+ rate
435
+ end
436
+
437
+ # Clears cached rates in store
438
+ #
439
+ # @return [Hash] All rates from store as Hash
440
+ def clear_rates!
441
+ store.each_rate do |iso_from, iso_to|
442
+ add_rate(iso_from, iso_to, nil)
319
443
  end
320
- nil
321
444
  end
322
445
  end
323
446
  end
324
447
  end
448
+ # rubocop:enable Metrics/ClassLength
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Module for version constant
4
4
  module OpenExchangeRatesBank
5
- VERSION = '0.7.0'.freeze
5
+ VERSION = '1.4.2'
6
6
  end
@@ -0,0 +1,6 @@
1
+ {
2
+ "error": true,
3
+ "status": 429,
4
+ "message": "access_restricted",
5
+ "description": "Access restricted until 2050-01-01 (reason: too_many_requests). If there has been a mistake, please contact support@openexchangerates.org."
6
+ }
@@ -0,0 +1 @@
1
+ {"error": true, "status": 401, "message": "app_id_inactive", "description": "This App ID has been deactivated. Please use another or contact support@openexchangerates.org for assistance."}
@@ -1,2 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
1
5
  gem 'money', '~> 6.7'
2
6
  gem 'money-open-exchange-rates', path: '../../'
@@ -1,26 +1,24 @@
1
1
  PATH
2
- remote: ../../
2
+ remote: ../..
3
3
  specs:
4
- money-open-exchange-rates (0.6.1)
5
- monetize (~> 1.4)
6
- money (~> 6.7)
4
+ money-open-exchange-rates (1.4.1)
5
+ money (~> 6.12)
7
6
 
8
7
  GEM
8
+ remote: https://rubygems.org/
9
9
  specs:
10
- i18n (0.7.0)
11
- monetize (1.4.0)
12
- money (~> 6.7)
13
- money (6.7.1)
14
- i18n (>= 0.6.4, <= 0.7.0)
15
- sixarm_ruby_unaccent (>= 1.1.1, < 2)
16
- sixarm_ruby_unaccent (1.1.1)
10
+ concurrent-ruby (1.2.2)
11
+ i18n (1.13.0)
12
+ concurrent-ruby (~> 1.0)
13
+ money (6.16.0)
14
+ i18n (>= 0.6.4, <= 2)
17
15
 
18
16
  PLATFORMS
19
- ruby
17
+ x86_64-linux
20
18
 
21
19
  DEPENDENCIES
22
20
  money (~> 6.7)
23
21
  money-open-exchange-rates!
24
22
 
25
23
  BUNDLED WITH
26
- 1.13.1
24
+ 2.4.12
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
  require 'money/bank/open_exchange_rates_bank'
3
5
 
4
- ERROR_MSG = 'Integration test failed!'.freeze
6
+ ERROR_MSG = 'Integration test failed!'
5
7
  cache_path = '/tmp/latest.json'
6
8
  to_currency = 'CAD'
7
9
  app_id = ENV['OXR_APP_ID']
@@ -29,6 +31,9 @@ begin
29
31
  puts 'Money to_currency', cad_rate
30
32
  # rubocop:disable Style/AndOr
31
33
  json_to_currency == cad_rate or raise ERROR_MSG
34
+ # rubocop:enable Style/AndOr
35
+ # rubocop:disable Style/RescueStandardError
32
36
  rescue
37
+ # rubocop:enable Style/RescueStandardError
33
38
  raise ERROR_MSG
34
39
  end