money-open-exchange-rates 0.7.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
+
3
+ require 'net/http'
2
4
  require 'uri'
3
- require 'open-uri'
4
5
  require 'money'
5
6
  require 'json'
6
- require File.expand_path('../../../open_exchange_rates_bank/version', __FILE__)
7
+ require File.expand_path('../../open_exchange_rates_bank/version', __dir__)
7
8
 
8
9
  # Money gem class
9
- # rubocop:disable ClassLength
10
+ # rubocop:disable Metrics/ClassLength
10
11
  class Money
11
12
  # https://github.com/RubyMoney/money#exchange-rate-stores
12
13
  module Bank
@@ -19,28 +20,16 @@ class Money
19
20
  # OpenExchangeRatesBank base class
20
21
  class OpenExchangeRatesBank < Money::Bank::VariableExchange
21
22
  VERSION = ::OpenExchangeRatesBank::VERSION
22
- BASE_URL = 'http://openexchangerates.org/api/'.freeze
23
+ BASE_URL = 'https://openexchangerates.org/api/'
24
+
23
25
  # OpenExchangeRates urls
24
26
  OER_URL = URI.join(BASE_URL, 'latest.json')
25
27
  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
28
 
32
29
  # 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
30
+ OE_SOURCE = 'USD'
31
+ RATES_KEY = 'rates'
32
+ TIMESTAMP_KEY = 'timestamp'
44
33
 
45
34
  # As of the end of August 2012 all requests to the Open Exchange Rates
46
35
  # API must have a valid app_id
@@ -72,6 +61,13 @@ class Money
72
61
  # @return [String] The requested date in YYYY-MM-DD format
73
62
  attr_accessor :date
74
63
 
64
+ # Force refresh rates cache and store on the fly when ttl is expired
65
+ # This will slow down request on get_rate, so use at your on risk, if you
66
+ # don't want to setup crontab/worker/scheduler for your application
67
+ #
68
+ # @param [Boolean]
69
+ attr_accessor :force_refresh_rate_on_expire
70
+
75
71
  # Rates expiration Time
76
72
  #
77
73
  # @return [Time] expiration time
@@ -82,11 +78,57 @@ class Money
82
78
  # @return [Hash] All rates as Hash
83
79
  attr_reader :oer_rates
84
80
 
81
+ # Unparsed OpenExchangeRates response as String
82
+ #
83
+ # @return [String] OpenExchangeRates json response
84
+ attr_reader :json_response
85
+
85
86
  # Seconds after than the current rates are automatically expired
86
87
  #
87
88
  # @return [Integer] Setted time to live in seconds
88
89
  attr_reader :ttl_in_seconds
89
90
 
91
+ # Set support for the black market and alternative digital currencies
92
+ # see https://docs.openexchangerates.org/docs/alternative-currencies
93
+ # @example
94
+ # oxr.show_alternative = true
95
+ #
96
+ # @param [Boolean] if true show alternative
97
+ # @return [Boolean] Setted show alternative
98
+ attr_writer :show_alternative
99
+
100
+ # Filter response to a list of symbols
101
+ # see https://docs.openexchangerates.org/docs/get-specific-currencies
102
+ # @example
103
+ # oxr.symbols = [:usd, :cad]
104
+ #
105
+ # @param [Array] list of symbols
106
+ # @return [Array] Setted list of symbols
107
+ attr_writer :symbols
108
+
109
+ # Minified Response ('prettyprint')
110
+ # see https://docs.openexchangerates.org/docs/prettyprint
111
+ # @example
112
+ # oxr.prettyprint = false
113
+ #
114
+ # @param [Boolean] Set to false to receive minified (default: true)
115
+ # @return [Boolean]
116
+ attr_writer :prettyprint
117
+
118
+ # Set current rates timestamp
119
+ #
120
+ # @return [Time]
121
+ def rates_timestamp=(at)
122
+ @rates_timestamp = Time.at(at)
123
+ end
124
+
125
+ # Current rates timestamp
126
+ #
127
+ # @return [Time]
128
+ def rates_timestamp
129
+ @rates_timestamp || Time.now
130
+ end
131
+
90
132
  # Set the seconds after than the current rates are automatically expired
91
133
  # by default, they never expire.
92
134
  #
@@ -99,7 +141,7 @@ class Money
99
141
  def ttl_in_seconds=(value)
100
142
  @ttl_in_seconds = value
101
143
  refresh_rates_expiration if ttl_in_seconds
102
- @ttl_in_seconds
144
+ ttl_in_seconds
103
145
  end
104
146
 
105
147
  # Set the base currency for all rates. By default, USD is used.
@@ -113,9 +155,12 @@ class Money
113
155
  #
114
156
  # @return [String] chosen base currency
115
157
  def source=(value)
116
- @source = Money::Currency.find(value.to_s).iso_code
117
- rescue
118
- @source = OE_SOURCE
158
+ scurrency = Money::Currency.find(value.to_s)
159
+ @source = if scurrency
160
+ scurrency.iso_code
161
+ else
162
+ OE_SOURCE
163
+ end
119
164
  end
120
165
 
121
166
  # Get the base currency for all rates. By default, USD is used.
@@ -129,27 +174,19 @@ class Money
129
174
  #
130
175
  # @return [Array] Array of exchange rates
131
176
  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)
177
+ store.transaction do
178
+ clear_rates!
179
+ exchange_rates.each do |exchange_rate|
180
+ rate = exchange_rate.last
181
+ currency = exchange_rate.first
182
+ next unless Money::Currency.find(currency)
183
+
184
+ set_rate(source, currency, rate)
185
+ set_rate(currency, source, 1.0 / rate)
186
+ end
138
187
  end
139
188
  end
140
189
 
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
190
  # Alias super method
154
191
  alias super_get_rate get_rate
155
192
 
@@ -166,30 +203,77 @@ class Money
166
203
  rate || calc_pair_rate_using_base(from_currency, to_currency, opts)
167
204
  end
168
205
 
206
+ # Fetch from url and save cache
207
+ #
208
+ # @return [Array] Array of exchange rates
209
+ def refresh_rates
210
+ read_from_url
211
+ end
212
+
213
+ # Alias refresh_rates method
214
+ alias save_rates refresh_rates
215
+
169
216
  # Expire rates when expired
170
217
  #
171
218
  # @return [NilClass, Time] nil if not expired or new expiration time
172
219
  def expire_rates
173
220
  return unless ttl_in_seconds
174
221
  return if rates_expiration > Time.now
222
+
223
+ refresh_rates if force_refresh_rate_on_expire
175
224
  update_rates
176
225
  refresh_rates_expiration
177
226
  end
178
227
 
228
+ # Get show alternative
229
+ #
230
+ # @return [Boolean] if true show alternative
231
+ def show_alternative
232
+ @show_alternative ||= false
233
+ end
234
+
235
+ # Get prettyprint option
236
+ #
237
+ # @return [Boolean]
238
+ def prettyprint
239
+ return true unless defined? @prettyprint
240
+ return true if @prettyprint.nil?
241
+
242
+ @prettyprint
243
+ end
244
+
245
+ # Get symbols
246
+ #
247
+ # @return [Array] list of symbols to filter by
248
+ def symbols
249
+ @symbols ||= nil
250
+ end
251
+
179
252
  # Source url of openexchangerates
180
- # defined with app_id and secure_connection
253
+ # defined with app_id
181
254
  #
182
255
  # @return [String] URL
183
256
  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
257
+ str = "#{oer_url}?app_id=#{app_id}"
258
+ str = "#{str}&base=#{source}" unless source == OE_SOURCE
259
+ str = "#{str}&show_alternative=#{show_alternative}"
260
+ str = "#{str}&prettyprint=#{prettyprint}"
261
+ str = "#{str}&symbols=#{symbols.join(',')}" if symbols&.is_a?(Array)
262
+ str
189
263
  end
190
264
 
191
265
  protected
192
266
 
267
+ # Save rates on cache
268
+ # Can raise InvalidCache
269
+ #
270
+ # @return [Proc,File]
271
+ def save_cache
272
+ store_in_cache(@json_response) if valid_rates?(@json_response)
273
+ rescue Errno::ENOENT
274
+ raise InvalidCache
275
+ end
276
+
193
277
  # Latest url if no date given
194
278
  #
195
279
  # @return [String] URL
@@ -205,21 +289,19 @@ class Money
205
289
  #
206
290
  # @return [String] URL
207
291
  def historical_url
208
- url = OER_HISTORICAL_URL
209
- url = SECURE_OER_HISTORICAL_URL if secure_connection
210
- URI.join(url, "#{date}.json")
292
+ URI.join(OER_HISTORICAL_URL, "#{date}.json")
211
293
  end
212
294
 
213
295
  # Latest url
214
296
  #
215
297
  # @return [String] URL
216
298
  def latest_url
217
- return SECURE_OER_URL if secure_connection
218
299
  OER_URL
219
300
  end
220
301
 
221
302
  # Store the provided text data by calling the proc method provided
222
303
  # for the cache, or write to the cache file.
304
+ # Can raise InvalidCache
223
305
  #
224
306
  # @example
225
307
  # oxr.store_in_cache("{\"rates\": {\"AED\": 3.67304}}")
@@ -229,10 +311,12 @@ class Money
229
311
  def store_in_cache(text)
230
312
  if cache.is_a?(Proc)
231
313
  cache.call(text)
232
- elsif cache.is_a?(String)
233
- open(cache, 'w') do |f|
314
+ elsif cache.is_a?(String) || cache.is_a?(Pathname)
315
+ File.open(cache.to_s, 'w') do |f|
234
316
  f.write(text)
235
317
  end
318
+ else
319
+ raise InvalidCache
236
320
  end
237
321
  end
238
322
 
@@ -240,11 +324,19 @@ class Money
240
324
  #
241
325
  # @return [String] Raw string from file or cache proc
242
326
  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
327
+ result = if cache.is_a?(Proc)
328
+ cache.call(nil)
329
+ elsif File.exist?(cache.to_s)
330
+ File.read(cache)
331
+ end
332
+ result if valid_rates?(result)
333
+ end
334
+
335
+ # Read API
336
+ #
337
+ # @return [String]
338
+ def api_response
339
+ Net::HTTP.get(URI(source_url))
248
340
  end
249
341
 
250
342
  # Read from url
@@ -252,7 +344,10 @@ class Money
252
344
  # @return [String] JSON content
253
345
  def read_from_url
254
346
  raise NoAppId if app_id.nil? || app_id.empty?
255
- open(source_url).read
347
+
348
+ @json_response = api_response
349
+ save_cache if cache
350
+ @json_response
256
351
  end
257
352
 
258
353
  # Check validity of rates response only for store in cache
@@ -263,8 +358,10 @@ class Money
263
358
  # @param [String] text is JSON content
264
359
  # @return [Boolean] valid or not
265
360
  def valid_rates?(text)
361
+ return false unless text
362
+
266
363
  parsed = JSON.parse(text)
267
- parsed && parsed.key?('rates')
364
+ parsed&.key?(RATES_KEY) && parsed&.key?(TIMESTAMP_KEY)
268
365
  rescue JSON::ParserError
269
366
  false
270
367
  end
@@ -274,14 +371,15 @@ class Money
274
371
  # @return [Hash] key is country code (ISO 3166-1 alpha-3) value Float
275
372
  def exchange_rates
276
373
  doc = JSON.parse(read_from_cache || read_from_url)
277
- @oer_rates = doc['rates']
374
+ self.rates_timestamp = doc[TIMESTAMP_KEY]
375
+ @oer_rates = doc[RATES_KEY]
278
376
  end
279
377
 
280
378
  # Refresh expiration from now
281
379
  #
282
380
  # @return [Time] new expiration time
283
381
  def refresh_rates_expiration
284
- @rates_expiration = Time.now + ttl_in_seconds
382
+ @rates_expiration = rates_timestamp + ttl_in_seconds
285
383
  end
286
384
 
287
385
  # Get rate or calculate it as inverse rate
@@ -312,13 +410,23 @@ class Money
312
410
  def calc_pair_rate_using_base(from_currency, to_currency, opts)
313
411
  from_base_rate = get_rate_or_calc_inverse(source, from_currency, opts)
314
412
  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
413
+ return unless to_base_rate
414
+ return unless from_base_rate
415
+
416
+ rate = BigDecimal(to_base_rate.to_s) / from_base_rate
417
+ add_rate(from_currency, to_currency, rate)
418
+ rate
419
+ end
420
+
421
+ # Clears cached rates in store
422
+ #
423
+ # @return [Hash] All rates from store as Hash
424
+ def clear_rates!
425
+ store.each_rate do |iso_from, iso_to|
426
+ add_rate(iso_from, iso_to, nil)
319
427
  end
320
- nil
321
428
  end
322
429
  end
323
430
  end
324
431
  end
432
+ # 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.0'
6
6
  end
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  gem 'money', '~> 6.7'
2
4
  gem 'money-open-exchange-rates', path: '../../'
@@ -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