mrpin-sdk 1.0.75 → 1.0.76

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
  SHA1:
3
- metadata.gz: 4feaca6e39dd3a680ab12170570ca60073c8822a
4
- data.tar.gz: 828c11bd3f8d7557ab5609c33d3d41558d6136f3
3
+ metadata.gz: 6b3fc6277056691b958b64a917de5a38145505c3
4
+ data.tar.gz: f3cd8571f82824cc3c60e003945e2b2e7541222d
5
5
  SHA512:
6
- metadata.gz: 6fb3d2f64822b60f261dda148a903ec17f06a2283500668bf07d7dc21bb98235085baf83347c18400cd7e653162240ebdcb856ac05a91fe3a724d534c42f4439
7
- data.tar.gz: e1d60344d47e1aa3ce6daa136cda86cbab5bdad6b216262a3eee317f9e551120abab702424eb62e520c75e96393ebb8fea00d131ad5e1bce14ca36abc8947d32
6
+ metadata.gz: 62b5f36d515675773344ec6dbee0b6e02a3c61bbd1f243bd8ae2c3145a093184a81e47e0d5f5851168e4cafe62cb057af7af3bffb26409905e91988dce81ef2f
7
+ data.tar.gz: a84b5f3c292b5a29ea8965cc60d783c4f696d2ca19e7035c263d778d607fbe0cfb0b58945637216ddc9549ecd5730467c4b9a81e013aa1755e6f48f823a18935
@@ -3,9 +3,7 @@ class CurrencyExchanger
3
3
  def initialize
4
4
  @exchangers = []
5
5
 
6
- if Rails.env.development?
7
- @exchangers << ExchangerLocal.new
8
- end
6
+ @exchanger_local = ExchangerLocal.new
9
7
 
10
8
  @exchangers << ExchangerYahoo.new
11
9
  @exchangers << ExchangerFixer.new
@@ -14,25 +12,51 @@ class CurrencyExchanger
14
12
 
15
13
  public
16
14
  def get_rate(from_currency, to_currency)
15
+ from_currency = from_currency.upcase
16
+ to_currency = to_currency.upcase
17
+
17
18
  result = nil
18
19
 
19
20
  if from_currency == to_currency
20
21
  result = 1.0
21
22
  else
22
- @exchangers.each do |exchanger|
23
- begin
24
23
 
25
- result = exchanger.get_rate(from_currency, to_currency)
24
+ result = @exchanger_local.get_rate(from_currency, to_currency)
25
+
26
+ if result.nil?
27
+ result = get_rate_and_cache(from_currency, to_currency)
28
+ end
29
+ end
30
+
31
+ if result.nil? || result == 0
32
+ if Rails.env.production?
33
+ assert(false, "can't fetch rate from servers")
34
+ else
35
+ result = 1.0
36
+ end
37
+ end
38
+
39
+ result
40
+ end
41
+
42
+ private
43
+ def get_rate_and_cache(from_currency, to_currency)
44
+ from_currency = from_currency.upcase
45
+ to_currency = to_currency.upcase
46
+
47
+ result = nil
26
48
 
27
- break if !result.nil? && result > 0
49
+ @exchangers.each do |exchanger|
50
+ begin
51
+ result = exchanger.get_rate(from_currency, to_currency)
28
52
 
29
- rescue Exception => e
30
- AppInfo.instance.on_server_error("Can't fetch currency rate form #{exchanger.class.name}. Error: #{e.message}")
31
- end
53
+ break if !result.nil? && result > 0
54
+ rescue Exception => e
55
+ AppInfo.instance.on_server_error("Can't fetch currency rate form #{exchanger.class.name}. Error: #{e.message}")
32
56
  end
33
57
  end
34
58
 
35
- assert(!result.nil? && result > 0, "can't fetch rate from servers")
59
+ @exchanger_local.set_rate(from_currency, to_currency, result)
36
60
 
37
61
  result
38
62
  end
@@ -1,10 +1,12 @@
1
- # used only local
1
+ # used for local cache
2
2
  class ExchangerLocal < ExchangerBase
3
3
 
4
4
  #
5
5
  # properties
6
6
  #
7
7
 
8
+ EXPIRE_DURATION = 30.minutes
9
+
8
10
  #
9
11
  # methods
10
12
  #
@@ -13,11 +15,60 @@ class ExchangerLocal < ExchangerBase
13
15
  public
14
16
  def initialize
15
17
  super
18
+
19
+ @locker = Mutex.new
20
+ #key - from + to, value - hash {timestamp_updated_at: rate: }
21
+ @rates_map = {}
16
22
  end
17
23
 
18
24
  public
19
25
  def get_rate(from_currency, to_currency)
20
- 1.0
26
+ result = nil
27
+
28
+ @locker.synchronize do
29
+ key = "#{from_currency}_#{to_currency}"
30
+
31
+ data = @rates_map[key]
32
+
33
+ unless data.nil?
34
+ updated_at = data[:updated_at]
35
+ rate = data[:rate]
36
+
37
+ if Time.now.to_i - updated_at > EXPIRE_DURATION
38
+ @rates_map.delete(key)
39
+ else
40
+ result = rate
41
+ end #check time
42
+ end #check data
43
+ end #locker
44
+
45
+ result
46
+ end
47
+
48
+ public
49
+ def set_rate(from_currency, to_currency, rate)
50
+ return if rate == 0 || rate.nil?
51
+
52
+ @locker.synchronize do
53
+ set_rate_unsafe(from_currency, to_currency, rate)
54
+ set_rate_unsafe(to_currency, from_currency, 1.to_f / rate)
55
+ end
56
+
57
+ nil
58
+ end
59
+
60
+ private
61
+ def set_rate_unsafe(from_currency, to_currency, rate)
62
+ key = "#{from_currency}_#{to_currency}"
63
+ data =
64
+ {
65
+ updated_at: Time.now.to_i,
66
+ rate: rate
67
+ }
68
+
69
+ @rates_map[key] = data
70
+
71
+ nil
21
72
  end
22
73
 
23
74
  end
data/mrpin-sdk.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'mrpin-sdk'
7
- spec.version = '1.0.75'
7
+ spec.version = '1.0.76'
8
8
  spec.authors = %w(Gregory Tkach Yakupov Dmitrij)
9
9
  spec.email = %w(gregory.tkach@gmail.com yakupov.dmitrij@gmail.com)
10
10
  spec.description = %q{Mrpin sdk for backend development.}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mrpin-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.75
4
+ version: 1.0.76
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregory
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2017-07-24 00:00:00.000000000 Z
14
+ date: 2017-07-31 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: bundler
@@ -398,7 +398,6 @@ files:
398
398
  - lib/mrpin/core/constants/constants_remote.rb
399
399
  - lib/mrpin/core/currency_exchanger/base/exchanger_base.rb
400
400
  - lib/mrpin/core/currency_exchanger/currency_exchanger.rb
401
- - lib/mrpin/core/currency_exchanger/e_currency_type.rb
402
401
  - lib/mrpin/core/currency_exchanger/fixer/exchanger_fixer.rb
403
402
  - lib/mrpin/core/currency_exchanger/local/exchanger_local.rb
404
403
  - lib/mrpin/core/currency_exchanger/yahoo/exchanger_yahoo.rb
@@ -1,8 +0,0 @@
1
- class ECurrencyType < EnumBase
2
-
3
- ECT_USD = 'USD'
4
- ECT_EUR = 'EUR'
5
- ECT_RUB = 'RUB'
6
- ECT_UAH = 'UAH'
7
-
8
- end