throttling 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,14 @@
1
+ ## 0.3.1 (April 13, 2012)
2
+
3
+ Features:
4
+
5
+ - When limit is omitted, no limits will be enforced (`check` always returns true)
6
+
7
+ Bugfixes:
8
+
9
+ - Fixed bug when action was allowed `limit + 1` times
10
+ - When limit is 0, `check` should always return `false`
11
+
1
12
  ## 0.3.0 (April 13, 2012)
2
13
 
3
14
  Features:
@@ -35,11 +35,11 @@ module Throttling
35
35
 
36
36
  limits.each do |period_name, params|
37
37
  period = params[:period].to_i
38
- limit = params[:limit].to_i
38
+ limit = params[:limit].nil? ? nil : params[:limit].to_i
39
39
  values = params[:values]
40
40
 
41
- raise ArgumentError, "Invalid or no 'period' parameter in the limits[#{period_name}] config" if period < 1
42
- raise ArgumentError, "Invalid or no 'limit' parameter in the limits[#{period_name}] config" if limit < 1 && !values
41
+ raise ArgumentError, "Invalid or no 'period' parameter in the limits[#{action}][#{period_name}] config: #{limit.inspect}" if period < 1
42
+ raise ArgumentError, "Invalid 'limit' parameter in the limits[#{action}][#{period_name}] config: #{limit.inspect}" if !limit.nil? && limit < 0
43
43
 
44
44
  key = hits_store_key(check_type, check_value, period_name, period)
45
45
 
@@ -56,7 +56,7 @@ module Throttling
56
56
  end
57
57
  else
58
58
  # Over limit?
59
- return false if hits > limit
59
+ return false if !limit.nil? && hits >= limit
60
60
  end
61
61
 
62
62
  Throttling.storage.increment(key) if auto_increment
@@ -1,3 +1,3 @@
1
1
  module Throttling
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
@@ -18,9 +18,16 @@ describe Throttling do
18
18
  @t.send(check_method, nil).should be_true
19
19
  end
20
20
 
21
- it 'should raise an exception if no limit specified in configs' do
21
+ it 'should return true if no limit specified in configs' do
22
22
  Throttling.limits['foo']['limit'] = nil
23
- lambda { @t.send(check_method, valid_value) }.should raise_error(ArgumentError)
23
+ @storage.should_receive(:fetch).and_return(1000)
24
+ @t.send(check_method, valid_value).should be_true
25
+ end
26
+
27
+ it 'should return false if limit is 0' do
28
+ Throttling.limits['foo']['limit'] = 0
29
+ @storage.should_receive(:fetch).and_return(0)
30
+ @t.send(check_method, valid_value).should be_false
24
31
  end
25
32
 
26
33
  it 'should raise an exception if no period specified in configs' do
@@ -53,17 +60,23 @@ describe Throttling do
53
60
  @t.send(check_method, valid_value)
54
61
  end
55
62
 
56
- it 'should increase hit counter when values equals to limit' do
63
+ it 'should not increase hit counter when values equals to limit' do
57
64
  @storage.should_receive(:fetch).and_return(Throttling.limits['foo']['limit'])
58
- @storage.should_receive(:increment)
65
+ @storage.should_not_receive(:increment)
59
66
  @t.send(check_method, valid_value)
60
67
  end
61
68
 
62
- it 'should increase hit counter when values equals to limit + 1' do
69
+ it 'should not increase hit counter when values equals to limit + 1' do
63
70
  @storage.should_receive(:fetch).and_return(Throttling.limits['foo']['limit'] + 1)
64
71
  @storage.should_not_receive(:increment)
65
72
  @t.send(check_method, valid_value)
66
73
  end
74
+
75
+ it 'should allow exactly limit actions' do
76
+ 5.times { @t.send(check_method, valid_value).should be_true }
77
+ @storage.should_not_receive(:increment)
78
+ @t.send(check_method, valid_value).should be_false
79
+ end
67
80
  end
68
81
  end
69
82
  end
@@ -8,8 +8,9 @@ class TestStorage
8
8
  def fetch(key, options = {}, &block)
9
9
  @values ||= {}
10
10
  value = @values.fetch(key, &block)
11
- @values[key] = options.merge(:value => value.to_s)
12
- value
11
+ value = { :value => value.to_s } unless Hash === value
12
+ @values[key] = value.merge(options)
13
+ value[:value]
13
14
  end
14
15
 
15
16
  def increment(key)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: throttling
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2012-04-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
17
- requirement: &70192665062020 !ruby/object:Gem::Requirement
17
+ requirement: &70097466944080 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70192665062020
25
+ version_requirements: *70097466944080
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70192665061600 !ruby/object:Gem::Requirement
28
+ requirement: &70097466943660 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70192665061600
36
+ version_requirements: *70097466943660
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: timecop
39
- requirement: &70192665061180 !ruby/object:Gem::Requirement
39
+ requirement: &70097466943240 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70192665061180
47
+ version_requirements: *70097466943240
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: guard-rspec
50
- requirement: &70192665060760 !ruby/object:Gem::Requirement
50
+ requirement: &70097466942820 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70192665060760
58
+ version_requirements: *70097466942820
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rb-fsevent
61
- requirement: &70192665060340 !ruby/object:Gem::Requirement
61
+ requirement: &70097466942400 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *70192665060340
69
+ version_requirements: *70097466942400
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: growl
72
- requirement: &70192665059920 !ruby/object:Gem::Requirement
72
+ requirement: &70097466941980 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,7 +77,7 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *70192665059920
80
+ version_requirements: *70097466941980
81
81
  description: Throttling gem provides basic, but very powerful way to throttle various
82
82
  user actions in your application
83
83
  email: