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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 056017ba095ed7c0a12d48dd98ece18793bdaba4
4
- data.tar.gz: 9c633f17e7a7ce37e3405597b56a42fd99146e51
3
+ metadata.gz: 5cb6b70c3806073d72b5199d009f115eba98e776
4
+ data.tar.gz: bcee4a20aa44098a8591477dc5c3dc73759a2b8a
5
5
  SHA512:
6
- metadata.gz: 927a4f023ac052b62501b55c0caa3bbdbaa279b892f322af6eb8ffdbba0b905e60364769c26cbcffafce77692478787516bd37365f68fcb89b4ccf07141c6a11
7
- data.tar.gz: 1d110cdffb8e532db7bb55eabdc4dba9c156531a34e0e1a17c1c183900375dad60679dbfbfa222d5f9d06f12c552dd01457ec53ee6f972a999cec8a1d9e319b1
6
+ metadata.gz: bc27091d644a2125ae608b4e468bd3b8452862bc745ddc6cc38b1a2abaad5da458a29fd4d2f63a4e94da757d7a469158a0c804efef5f1bbd7ebd5f292b89c531
7
+ data.tar.gz: 78e9164106299808e4d5488b6d7d5d0127e23ddd4bddd772c19e2b10b1303582b6af31f3375648bd6114f531690bdb2966cf6437006112568f700d845afa539e
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.5
1
+ 0.3.6
@@ -6,7 +6,7 @@
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "currency-rate"
9
- s.version = "0.3.5"
9
+ s.version = "0.3.6"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
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
- @storage.fetch(self.class.to_s) do
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 = timeout
6
- @mem_storage = {}
7
+ @timeout = timeout
8
+ @data = {}
7
9
  end
8
10
 
9
- def fetch(key, force_from_storage: false)
10
- if !force_from_storage && (@mem_storage[key].nil? || (@mem_storage[key][:timestamp] < (Time.now.to_i - @timeout)))
11
- @mem_storage[key] = { content: yield, timestamp: Time.now.to_i }
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
- @mem_storage[key][:content]
15
+ @data[key][:content]
14
16
  end
15
17
 
16
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: currency-rate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Snitko