google_currency_rails_cache 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ CLOBBER.include('.yardoc', 'doc')
5
5
 
6
6
  def gemspec
7
7
  @gemspec ||= begin
8
- file = File.expand_path("../google_currency.gemspec", __FILE__)
8
+ file = File.expand_path("../google_currency_rails_cache.gemspec", __FILE__)
9
9
  eval(File.read(file), binding, file)
10
10
  end
11
11
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "google_currency_rails_cache"
3
- s.version = "1.0.0"
3
+ s.version = "1.1.0"
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = ["Shane Emmons", "Alex Kremer"]
6
6
  s.email = ["semmons99@gmail.com", "alex@flextrip.com"]
@@ -9,23 +9,21 @@ class Money
9
9
  SERVICE_HOST = "www.google.com"
10
10
  SERVICE_PATH = "/ig/calculator"
11
11
 
12
- # @return [Hash] Stores the currently known rates.
13
- attr_reader :rates
14
-
15
12
  ##
16
13
  # Clears all rates stored in @rates
17
14
  #
18
- # @return [Hash] The empty @rates Hash.
19
- #
20
15
  # @example
21
- # @bank = GoogleCurrency.new #=> <Money::Bank::GoogleCurrency...>
16
+ # @bank = GoogleCurrencyRailsCache.new #=> <Money::Bank::GoogleCurrency...>
22
17
  # @bank.get_rate(:USD, :EUR) #=> 0.776337241
23
- # @bank.flush_rates #=> {}
18
+ # @bank.flush_rates #=> nil
24
19
  def flush_rates
25
- @rates.keys.each do |rate_key|
26
- Rails.cache.delete "exchange_rate/#{rate_key}"
20
+ rates = Rails.cache.read("exchange_rate/all_rates")
21
+ unless rates.nil?
22
+ rates.each do |rate_key|
23
+ Rails.cache.delete "exchange_rate/#{rate_key}"
24
+ end
27
25
  end
28
- @rates.clear
26
+ Rails.cache.delete("exchange_rate/all_rates")
29
27
  end
30
28
 
31
29
  ##
@@ -36,16 +34,16 @@ class Money
36
34
  # @param [String, Symbol, Currency] to Currency to convert to (used for
37
35
  # key into @rates).
38
36
  #
39
- # @return [Float] The flushed rate.
37
+ # @return [Boolean] Indicating successful removal.
40
38
  #
41
39
  # @example
42
- # @bank = GoogleCurrency.new #=> <Money::Bank::GoogleCurrency...>
40
+ # @bank = GoogleCurrencyRailsCache.new #=> <Money::Bank::GoogleCurrency...>
43
41
  # @bank.get_rate(:USD, :EUR) #=> 0.776337241
44
42
  # @bank.flush_rate(:USD, :EUR) #=> 0.776337241
45
43
  def flush_rate(from, to)
46
44
  rate_key = rate_key_for(from, to)
47
- Rails.cache.delete "exchange_rate/#{rate_key}"
48
- @rates.delete(rate_key)
45
+ Rails.cache.delete("exchange_rate/#{rate_key}")
46
+ remove_from_stored_rates(rate_key)
49
47
  end
50
48
 
51
49
  ##
@@ -57,13 +55,22 @@ class Money
57
55
  # @return [Float] The requested rate.
58
56
  #
59
57
  # @example
60
- # @bank = GoogleCurrency.new #=> <Money::Bank::GoogleCurrency...>
58
+ # @bank = GoogleCurrencyRailsCache.new #=> <Money::Bank::GoogleCurrency...>
61
59
  # @bank.get_rate(:USD, :EUR) #=> 0.776337241
62
60
  def get_rate(from, to)
63
- @rates[rate_key_for(from, to)] ||= Rails.cache.fetch("exchange_rate/#{rate_key_for(from, to)}", :expires_in => 12.hours ) {
64
- fetch_rate(from, to)
61
+ Rails.cache.fetch("exchange_rate/#{rate_key_for(from, to)}", :expires_in => 12.hours ) {
62
+ fetch_rate(from, to)
65
63
  }
66
64
  end
65
+
66
+ ##
67
+ # Lists all rates currency cached.
68
+ #
69
+ # @return [Array] An array of rates.
70
+ #
71
+ def cached_rates
72
+ Rails.cache.read("exchange_rate/all_rates")
73
+ end
67
74
 
68
75
  private
69
76
 
@@ -82,6 +89,7 @@ class Money
82
89
 
83
90
  error = data['error']
84
91
  raise UnknownRate unless error == '' || error == '0'
92
+ add_to_stored_rates(rate_key_for(from, to))
85
93
  decode_rate data['rhs']
86
94
  end
87
95
 
@@ -164,6 +172,36 @@ class Money
164
172
  def decode_basic_rate(rhs)
165
173
  BigDecimal(rhs.gsub(/[^\d\.]/, ''))
166
174
  end
175
+
176
+ ##
177
+ # Removes the specified rate from the global list of stored rates
178
+ #
179
+ # @param [String] rate The rate to remove
180
+ #
181
+ # @return [Boolean]
182
+ def remove_from_stored_rates(rate)
183
+ rates = Rails.cache.read("exchange_rate/all_rates")
184
+ unless rates.nil?
185
+ rates.delete(rate)
186
+ Rails.cache.write("exchange_rate/all_rates", rates)
187
+ end
188
+ end
189
+
190
+ ##
191
+ # Add the specified rate to the global list of stored rates
192
+ #
193
+ # @param [String] rate The rate to add
194
+ #
195
+ # @return [Boolean]
196
+ def add_to_stored_rates(rate)
197
+ rates = Rails.cache.read("exchange_rate/all_rates")
198
+ if rates.nil?
199
+ rates = []
200
+ end
201
+ rates << rate
202
+ Rails.cache.write("exchange_rate/all_rates", rates)
203
+ end
204
+
167
205
  end
168
206
  end
169
207
  end
@@ -29,10 +29,10 @@ describe "GoogleCurrency" do
29
29
  it "should return the correct rate" do
30
30
  @bank.get_rate('USD', 'USD').should == 1.0
31
31
  end
32
-
32
+
33
33
  it "should store the rate for faster retreival" do
34
34
  @bank.get_rate('USD', 'EUR')
35
- @bank.rates.should include('USD_TO_EUR')
35
+ @bank.cached_rates.should include('USD_TO_EUR')
36
36
  end
37
37
 
38
38
  it "should handle complex rates" do
@@ -45,20 +45,32 @@ describe "GoogleCurrency" do
45
45
  end
46
46
 
47
47
  describe "#flush_rates" do
48
- it "should empty @rates" do
48
+ it "should empty all rates" do
49
49
  @bank.get_rate('USD', 'EUR')
50
50
  @bank.flush_rates
51
- @bank.rates.should == {}
51
+ @bank.cached_rates.should == nil
52
+ @bank.should_receive(:fetch_rate)
53
+ @bank.get_rate('USD', 'EUR')
54
+ end
55
+
56
+ it "should handle flushing rates even if there are no rates" do
57
+ @bank.flush_rates
58
+ @bank.cached_rates.should == nil
52
59
  end
53
60
  end
54
61
 
55
62
  describe "#flush_rate" do
56
- it "should remove a specific rate from @rates" do
63
+ it "should remove a specific rate from cache" do
57
64
  @bank.get_rate('USD', 'EUR')
58
65
  @bank.get_rate('USD', 'JPY')
66
+ @bank.flush_rate('USD', 'EUR').should == true
67
+ @bank.cached_rates.should include('USD_TO_JPY')
68
+ @bank.cached_rates.should_not include('USD_TO_EUR')
69
+ end
70
+
71
+ it "should handle flushing a rate even if it was not previously stored" do
59
72
  @bank.flush_rate('USD', 'EUR')
60
- @bank.rates.should include('USD_TO_JPY')
61
- @bank.rates.should_not include('USD_TO_EUR')
73
+ @bank.cached_rates.should == nil
62
74
  end
63
75
  end
64
76
  end
@@ -32,7 +32,7 @@ describe "GoogleCurrency" do
32
32
 
33
33
  it "should store the rate for faster retreival" do
34
34
  @bank.get_rate('USD', 'EUR')
35
- @bank.rates.should include('USD_TO_EUR')
35
+ @bank.cached_rates.should include('USD_TO_EUR')
36
36
  end
37
37
 
38
38
  it "should handle complex rates" do
@@ -47,7 +47,14 @@ describe "GoogleCurrency" do
47
47
  it "should empty @rates" do
48
48
  @bank.get_rate('USD', 'EUR')
49
49
  @bank.flush_rates
50
- @bank.rates.should == {}
50
+ @bank.cached_rates.should == nil
51
+ @bank.should_receive(:fetch_rate)
52
+ @bank.get_rate('USD', 'EUR')
53
+ end
54
+
55
+ it "should handle flushing rates even if there are no rates" do
56
+ @bank.flush_rates
57
+ @bank.cached_rates.should == nil
51
58
  end
52
59
  end
53
60
 
@@ -55,9 +62,14 @@ describe "GoogleCurrency" do
55
62
  it "should remove a specific rate from @rates" do
56
63
  @bank.get_rate('USD', 'EUR')
57
64
  @bank.get_rate('USD', 'JPY')
65
+ @bank.flush_rate('USD', 'EUR').should == true
66
+ @bank.cached_rates.should include('USD_TO_JPY')
67
+ @bank.cached_rates.should_not include('USD_TO_EUR')
68
+ end
69
+
70
+ it "should handle flushing a rate even if it was not previously stored" do
58
71
  @bank.flush_rate('USD', 'EUR')
59
- @bank.rates.should include('USD_TO_JPY')
60
- @bank.rates.should_not include('USD_TO_EUR')
72
+ @bank.cached_rates.should == nil
61
73
  end
62
74
  end
63
75
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_currency_rails_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-06-29 00:00:00.000000000 Z
13
+ date: 2013-06-30 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec