currency-rate 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/currency-rate.gemspec +1 -1
- data/lib/adapter.rb +14 -2
- data/lib/currency_rate.rb +4 -3
- data/lib/storage.rb +8 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5cb6b70c3806073d72b5199d009f115eba98e776
|
4
|
+
data.tar.gz: bcee4a20aa44098a8591477dc5c3dc73759a2b8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc27091d644a2125ae608b4e468bd3b8452862bc745ddc6cc38b1a2abaad5da458a29fd4d2f63a4e94da757d7a469158a0c804efef5f1bbd7ebd5f292b89c531
|
7
|
+
data.tar.gz: 78e9164106299808e4d5488b6d7d5d0127e23ddd4bddd772c19e2b10b1303582b6af31f3375648bd6114f531690bdb2966cf6437006112568f700d845afa539e
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.6
|
data/currency-rate.gemspec
CHANGED
data/lib/adapter.rb
CHANGED
@@ -5,6 +5,8 @@ module CurrencyRate
|
|
5
5
|
class FetchingFailed < Exception; end
|
6
6
|
class CurrencyNotSupported < Exception; end
|
7
7
|
|
8
|
+
attr_accessor :try_storage_on_fetching_failed
|
9
|
+
|
8
10
|
def initialize
|
9
11
|
@storage = Storage.new
|
10
12
|
end
|
@@ -23,6 +25,7 @@ module CurrencyRate
|
|
23
25
|
@rates = JSON.parse(uri.read(read_timeout: 4))
|
24
26
|
end
|
25
27
|
@rates_updated_at = Time.now
|
28
|
+
@rates
|
26
29
|
rescue Exception => e
|
27
30
|
raise FetchingFailed
|
28
31
|
end
|
@@ -30,12 +33,21 @@ module CurrencyRate
|
|
30
33
|
|
31
34
|
def rate_for(from,to)
|
32
35
|
|
33
|
-
|
34
|
-
self.fetch_rates!
|
36
|
+
begin
|
37
|
+
@storage.fetch(self.class.to_s) { self.fetch_rates! }
|
38
|
+
rescue FetchingFailed => e
|
39
|
+
if @try_storage_on_fetching_failed
|
40
|
+
@rates = @storage.data[self.class.to_s][:content]
|
41
|
+
else
|
42
|
+
raise e
|
43
|
+
end
|
35
44
|
end
|
36
45
|
|
37
46
|
raise CurrencyNotSupported unless supports_currency_pair?(from,to)
|
38
47
|
|
48
|
+
# This method is further reloaded in Adapter classes, that's why
|
49
|
+
# here it doesn't really return anything useful!
|
50
|
+
|
39
51
|
end
|
40
52
|
|
41
53
|
def supports_currency_pair?(c1,c2)
|
data/lib/currency_rate.rb
CHANGED
@@ -12,9 +12,10 @@ Dir["#{File.expand_path File.dirname(__FILE__)}/**/*.rb"].each { |f| require f }
|
|
12
12
|
|
13
13
|
module CurrencyRate
|
14
14
|
|
15
|
-
def self.get(adapter_name, from, to, anchor_currency: nil)
|
15
|
+
def self.get(adapter_name, from, to, anchor_currency: nil, try_storage_on_fetching_failed: false)
|
16
16
|
|
17
17
|
a = adapter_class(adapter_name)
|
18
|
+
a.try_storage_on_fetching_failed = try_storage_on_fetching_failed
|
18
19
|
|
19
20
|
# Setting default values for anchor currency depending on
|
20
21
|
# which adapter type is un use.
|
@@ -35,8 +36,8 @@ module CurrencyRate
|
|
35
36
|
end
|
36
37
|
end
|
37
38
|
|
38
|
-
def self.convert(adapter_name, amount:, from:, to:, anchor_currency: nil)
|
39
|
-
result = BigDecimal.new(amount.to_s)*BigDecimal.new(get(adapter_name, from, to, anchor_currency: nil).to_s)
|
39
|
+
def self.convert(adapter_name, amount:, from:, to:, anchor_currency: nil, try_storage_on_fetching_failed: false)
|
40
|
+
result = BigDecimal.new(amount.to_s)*BigDecimal.new(get(adapter_name, from, to, anchor_currency: nil, try_storage_on_fetching_failed: try_storage_on_fetching_failed).to_s)
|
40
41
|
to == 'BTC' ? result.round(9) : result.round(2)
|
41
42
|
end
|
42
43
|
|
data/lib/storage.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
module CurrencyRate
|
2
2
|
class Storage
|
3
3
|
|
4
|
+
attr_reader :data
|
5
|
+
|
4
6
|
def initialize(timeout: 1800)
|
5
|
-
@timeout
|
6
|
-
@
|
7
|
+
@timeout = timeout
|
8
|
+
@data = {}
|
7
9
|
end
|
8
10
|
|
9
|
-
def fetch(key
|
10
|
-
if
|
11
|
-
@
|
11
|
+
def fetch(key)
|
12
|
+
if @data[key].nil? || (@data[key][:timestamp] < (Time.now.to_i - @timeout))
|
13
|
+
@data[key] = { content: yield, timestamp: Time.now.to_i }
|
12
14
|
end
|
13
|
-
@
|
15
|
+
@data[key][:content]
|
14
16
|
end
|
15
17
|
|
16
18
|
end
|