money-currencylayer-bank 0.5.3 → 0.5.4

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
  SHA1:
3
- metadata.gz: 2954dfb52185c910cfed6a3f3b1def920c80c847
4
- data.tar.gz: 408ba3963b1c258ce322e69237c951f34988ba1e
3
+ metadata.gz: cc97450bce8ac6e02fc906a21ca0c10860428557
4
+ data.tar.gz: 371c75e3794e3a7c2be6babd222845eda7cb1ac6
5
5
  SHA512:
6
- metadata.gz: f1696528b4ac735817a8fc476b1b5a3e87512ce4e167baf36601a794ef6de6a521c0496a05f943e0ca9f643ab2f3a1361f4e5a663d32a4a684df27f1894a62fa
7
- data.tar.gz: a58368adeb7bbc501e5ef1e13f55e9deff90084b7b45f9021c1f49b9bb4241b4acfee44303e28f7002efdfc56c230547b98b149c59b3c4bcf34633a597c2e0cc
6
+ metadata.gz: f69464174caabd8f18da9077f579d252f77b5b751bf4032117fbc575efd04a4740f84cf1be1fdb39e1f42791dc1540d5911b168324938108a339facef6bcdce0
7
+ data.tar.gz: e81c35792af291b86f106ed2c2f1efacc78ffcf035ffa01f70d4e030bb9de77de6f41983ccd0052dc61bf105cf4fde668256c1b5b87566bea6cb7e0017ec122e
data/Gemfile CHANGED
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec
4
6
 
5
7
  group :test, :development do
6
8
  platforms :ruby_19, :jruby_19 do
7
- gem 'tins', '~> 1.6.0'
9
+ gem 'tins', '~> 1.6'
8
10
  end
9
11
  end
@@ -1,10 +1,26 @@
1
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
2
4
  require 'open-uri'
3
5
  require 'money'
4
6
  require 'json'
5
7
 
6
8
  # Money gem class
7
9
  class Money
10
+ # Build in memory rates store
11
+ module RatesStore
12
+ # Memory class
13
+ class Memory
14
+ # Add method to reset the build in memory store
15
+ # @param [Hash] rt Optional initial exchange rate data.
16
+ #
17
+ # @return [Object] store.
18
+ def reset!(rt = {})
19
+ transaction { @index = rt }
20
+ end
21
+ end
22
+ end
23
+
8
24
  # https://github.com/RubyMoney/money#exchange-rate-stores
9
25
  module Bank
10
26
  # Invalid cache, file not found or cache empty
@@ -14,14 +30,13 @@ class Money
14
30
  class NoAccessKey < StandardError; end
15
31
 
16
32
  # CurrencylayerBank base class
17
- # rubocop:disable Metrics/ClassLength
18
33
  class CurrencylayerBank < Money::Bank::VariableExchange
19
34
  # CurrencylayerBank url
20
- CL_URL = 'http://apilayer.net/api/live'.freeze
35
+ CL_URL = 'http://apilayer.net/api/live'
21
36
  # CurrencylayerBank secure url
22
- CL_SECURE_URL = CL_URL.gsub('http:', 'https:').freeze
37
+ CL_SECURE_URL = CL_URL.gsub('http:', 'https:')
23
38
  # Default base currency
24
- CL_SOURCE = 'USD'.freeze
39
+ CL_SOURCE = 'USD'
25
40
 
26
41
  # Use https to fetch rates from CurrencylayerBank
27
42
  # CurrencylayerBank only allows http as connection
@@ -49,6 +64,10 @@ class Money
49
64
  # Parsed CurrencylayerBank result as Hash
50
65
  attr_reader :rates
51
66
 
67
+ # Get the timestamp of rates in memory
68
+ # @return [Time] time object or nil
69
+ attr_reader :rates_mem_timestamp
70
+
52
71
  # Set the seconds after than the current rates are automatically expired
53
72
  # by default, they never expire.
54
73
  #
@@ -90,6 +109,7 @@ class Money
90
109
  # Update all rates from CurrencylayerBank JSON
91
110
  # @return [Array] array of exchange rates
92
111
  def update_rates(straight = false)
112
+ store.reset!
93
113
  exchange_rates(straight).each do |exchange_rate|
94
114
  currency = exchange_rate.first[3..-1]
95
115
  rate = exchange_rate.last
@@ -97,11 +117,23 @@ class Money
97
117
  add_rate(source, currency, rate)
98
118
  add_rate(currency, source, 1.0 / rate)
99
119
  end
120
+ @rates_mem_timestamp = rates_timestamp
121
+ end
122
+
123
+ # Override Money `add_rate` method for caching
124
+ # @param [String] from_currency Currency ISO code. ex. 'USD'
125
+ # @param [String] to_currency Currency ISO code. ex. 'CAD'
126
+ # @param [Numeric] rate Rate to use when exchanging currencies.
127
+ #
128
+ # @return [Numeric] rate.
129
+ def add_rate(from_currency, to_currency, rate)
130
+ super
100
131
  end
101
132
 
102
133
  # Override Money `get_rate` method for caching
103
134
  # @param [String] from_currency Currency ISO code. ex. 'USD'
104
135
  # @param [String] to_currency Currency ISO code. ex. 'CAD'
136
+ # @param [Hash] opts Options hash to set special parameters.
105
137
  #
106
138
  # @return [Numeric] rate.
107
139
  def get_rate(from_currency, to_currency, opts = {}) # rubocop:disable all
@@ -135,12 +167,15 @@ class Money
135
167
  rate
136
168
  end
137
169
 
138
- # Fetch new rates if cached rates are expired
170
+ # Fetch new rates if cached rates are expired or stale
139
171
  # @return [Boolean] true if rates are expired and updated from remote
140
172
  def expire_rates!
141
173
  if expired?
142
174
  update_rates(true)
143
175
  true
176
+ elsif stale?
177
+ update_rates
178
+ true
144
179
  else
145
180
  false
146
181
  end
@@ -152,6 +187,14 @@ class Money
152
187
  Time.now > rates_expiration
153
188
  end
154
189
 
190
+ # Check if rates are stale
191
+ # Stale is true if rates are updated straight by another thread.
192
+ # The actual thread has always old rates in memory store.
193
+ # @return [Boolean] true if rates are stale
194
+ def stale?
195
+ rates_timestamp != rates_mem_timestamp
196
+ end
197
+
155
198
  # Source url of CurrencylayerBank
156
199
  # defined with access_key and secure_connection
157
200
  # @return [String] the remote API url
@@ -1,4 +1,6 @@
1
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
2
4
  require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
3
5
 
4
6
  describe Money::Bank::CurrencylayerBank do
@@ -208,9 +210,12 @@ describe Money::Bank::CurrencylayerBank do
208
210
  delimiter: ','
209
211
  }
210
212
  Money::Currency.register(wtf)
211
- subject.add_rate('USD', 'WTF', 2)
212
- subject.add_rate('WTF', 'USD', 2)
213
- subject.exchange_with(5000.to_money('WTF'), 'USD').cents.wont_equal 0
213
+ Timecop.freeze(subject.rates_timestamp) do
214
+ subject.add_rate('USD', 'WTF', 2)
215
+ subject.add_rate('WTF', 'USD', 2)
216
+ subject.exchange_with(5000.to_money('WTF'), 'USD').cents
217
+ subject.exchange_with(5000.to_money('WTF'), 'USD').cents.wont_equal 0
218
+ end
214
219
  end
215
220
  end
216
221
 
@@ -232,9 +237,10 @@ describe Money::Bank::CurrencylayerBank do
232
237
  @old_usd_eur_rate = 0.655
233
238
  # see test/live.json +54
234
239
  @new_usd_eur_rate = 0.886584
235
- subject.add_rate('USD', 'EUR', @old_usd_eur_rate)
236
240
  subject.cache = temp_cache_path
237
241
  stub(subject).source_url { data_path }
242
+ subject.update_rates
243
+ subject.add_rate('USD', 'EUR', @old_usd_eur_rate)
238
244
  end
239
245
 
240
246
  after do
@@ -1,4 +1,6 @@
1
1
  # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
2
4
  require 'minitest/autorun'
3
5
  require 'rr'
4
6
  require 'money/bank/currencylayer_bank'
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.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Egon Zemmer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-17 00:00:00.000000000 Z
11
+ date: 2017-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: money
@@ -42,14 +42,14 @@ dependencies:
42
42
  name: json
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.8'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.8'
55
55
  - !ruby/object:Gem::Dependency
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '10.4'
103
+ version: '12.0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '10.4'
110
+ version: '12.0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: timecop
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -128,14 +128,14 @@ dependencies:
128
128
  requirements:
129
129
  - - "~>"
130
130
  - !ruby/object:Gem::Version
131
- version: 0.37.2
131
+ version: 0.48.1
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
- version: 0.37.2
138
+ version: 0.48.1
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: inch
141
141
  requirement: !ruby/object:Gem::Requirement
@@ -186,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  version: '0'
187
187
  requirements: []
188
188
  rubyforge_project:
189
- rubygems_version: 2.4.8
189
+ rubygems_version: 2.5.2
190
190
  signing_key:
191
191
  specification_version: 4
192
192
  summary: A gem that calculates the exchange rate using published rates from currencylayer.com.