russian_central_bank 0.2.1 → 0.3.0

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: dbe7d78854c964ee285030b941ecf65ce0818d3d
4
- data.tar.gz: 2543a1a59cbf247955bf79359b2b33c4de65c99d
3
+ metadata.gz: 22f458d677cf82c7a11d3c4a88cffbfda4904bf0
4
+ data.tar.gz: fab07fb36ba2531fab496ec9bb37f070a3c76f41
5
5
  SHA512:
6
- metadata.gz: 2f521e9e52fed5250ee5a6d63e067fa04d683c5d0936b359b9b4ee315ba975b68774a1a78bc3eede6ba763ab72ab03cb06b90520b8101dd5284540e5f1cad6a7
7
- data.tar.gz: 0beb519a6f5ca257034e5deb6c2053c3de229b64cfba6e2fa4fc9351bf7494a1c4a7295dfd07ed7fbe93c0466eee9274f6063a018dbf4d84775bd91c3ae972a9
6
+ metadata.gz: 4cfbaccf01fc6f1da5c6edb7ef320aff5f8b4eb3dde5ef02d3db8488117d632b44f9a462142754561e50e75d4495ccde6d367a80d8817f29755e61cf96ff3e55
7
+ data.tar.gz: ef2ac1c0a4f56649e9c1f83327811b136923271ec730c408b7b19e41a21aca08b6890f50474f719c692fff32865a248f37dce644458a903a3e762b4496a257b4
data/README.md CHANGED
@@ -27,6 +27,7 @@ Or install it yourself as:
27
27
 
28
28
  # Load today's rates
29
29
  bank.update_rates
30
+
30
31
  # Or you can specify the date
31
32
  bank.update_rates(3.days.ago)
32
33
 
@@ -41,6 +42,11 @@ Or install it yourself as:
41
42
  # Check on which date rates were updated
42
43
  bank.rates_updated_on
43
44
 
45
+ # Use ttl attribute to enable rates autoupdate
46
+ bank.ttl = 1.day
47
+ # Check expiration date
48
+ bank.rates_expired_at
49
+
44
50
  ## Contributing
45
51
 
46
52
  1. Fork it
@@ -7,7 +7,7 @@ class Money
7
7
 
8
8
  CBR_SERVICE_URL = 'http://www.cbr.ru/DailyInfoWebServ/DailyInfo.asmx?WSDL'
9
9
 
10
- attr_reader :rates_updated_at, :rates_updated_on
10
+ attr_reader :rates_updated_at, :rates_updated_on, :ttl, :rates_expired_at
11
11
 
12
12
  def flush_rates
13
13
  @mutex.synchronize{
@@ -20,6 +20,7 @@ class Money
20
20
  update_parsed_rates exchange_rates(date)
21
21
  @rates_updated_at = Time.now
22
22
  @rates_updated_on = date
23
+ update_expired_at
23
24
  @rates
24
25
  }
25
26
  end
@@ -30,11 +31,30 @@ class Money
30
31
  end
31
32
 
32
33
  def get_rate from, to
34
+ update_rates if rates_expired?
33
35
  @rates[rate_key_for(from, to)] || indirect_rate(from, to)
34
36
  end
35
37
 
38
+ def ttl=(value)
39
+ @ttl = value
40
+ update_expired_at
41
+ @ttl
42
+ end
43
+
44
+ def rates_expired?
45
+ rates_expired_at && rates_expired_at <= Time.now
46
+ end
47
+
36
48
  private
37
49
 
50
+ def update_expired_at
51
+ @rates_expired_at = if ttl
52
+ @rates_updated_at ? @rates_updated_at + ttl : Time.now
53
+ else
54
+ nil
55
+ end
56
+ end
57
+
38
58
  def indirect_rate from, to
39
59
  from_base_rate = @rates[rate_key_for('RUB', from)]
40
60
  to_base_rate = @rates[rate_key_for('RUB', to)]
@@ -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 = 'russian_central_bank'
7
- spec.version = '0.2.1'
7
+ spec.version = '0.3.0'
8
8
  spec.authors = ['Ramil Mustafin']
9
9
  spec.email = ['rommel.rmm@gmail.com']
10
10
  spec.description = 'RussianCentralBank extends Money::Bank::VariableExchange and gives you access to the Central Bank of Russia currency exchange rates.'
@@ -64,5 +64,45 @@ describe 'RussianCentralBank' do
64
64
  expect(@bank.get_rate('RUB', 'USD')).to eq(1.0 / indirect_rate)
65
65
  end
66
66
  end
67
+
68
+ context "when ttl is not set" do
69
+ before do
70
+ @bank.add_rate('RUB', 'USD', 123)
71
+ @bank.ttl = nil
72
+ end
73
+
74
+ it "should not update rates" do
75
+ @bank.should_not_receive(:update_rates)
76
+ @bank.get_rate('RUB', 'USD')
77
+ end
78
+ end
79
+
80
+ context "when ttl is set" do
81
+ before { @bank.add_rate('RUB', 'USD', 123) }
82
+
83
+ context "and raks are expired" do
84
+ before do
85
+ @bank.instance_variable_set('@rates_updated_at', Time.now - 3600)
86
+ @bank.ttl = 3600
87
+ end
88
+
89
+ it "should update rates" do
90
+ @bank.should_receive(:update_rates)
91
+ @bank.get_rate('RUB', 'USD')
92
+ end
93
+ end
94
+
95
+ context "and ranks are not expired" do
96
+ before do
97
+ @bank.instance_variable_set('@rates_updated_at', Time.now - 3000)
98
+ @bank.ttl = 3600
99
+ end
100
+
101
+ it "should not update rates" do
102
+ @bank.should_not_receive(:update_rates)
103
+ @bank.get_rate('RUB', 'USD')
104
+ end
105
+ end
106
+ end
67
107
  end
68
108
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: russian_central_bank
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ramil Mustafin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-16 00:00:00.000000000 Z
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler