rate_limiter 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## v0.0.3
2
+
3
+ * Added more if and unless conditions for rate limiting.
4
+ * Added ability to enable or disable rate limiting on a controller or model.
5
+ * Added RateLimiter source for determining the current user.
6
+
7
+ ## v0.0.2
8
+
9
+ * RateLimiter.config.timestamp_field actually does something now.
10
+
1
11
  ## v0.0.1
2
12
 
3
13
  * Initial release.
data/lib/rate_limiter.rb CHANGED
@@ -1,10 +1,27 @@
1
1
  require 'singleton'
2
2
 
3
3
  require 'rate_limiter/config'
4
+ require 'rate_limiter/controller'
4
5
  require 'rate_limiter/model'
5
6
 
6
7
  module RateLimiter
7
- def self.timestamp_field= field_name
8
+ def self.enabled=(value)
9
+ RateLimiter.config.enabled = value
10
+ end
11
+
12
+ def self.enabled?
13
+ !!RateLimiter.config.enabled
14
+ end
15
+
16
+ def self.enabled_for_controller=(value)
17
+ RateLimiter.store[:request_enabled_for_controller] = value
18
+ end
19
+
20
+ def self.enabled_for_controller?
21
+ !!RateLimiter.store[:request_enabled_for_controller]
22
+ end
23
+
24
+ def self.timestamp_field=(field_name)
8
25
  RateLimiter.config.timestamp_field = field_name
9
26
  end
10
27
 
@@ -12,8 +29,28 @@ module RateLimiter
12
29
  RateLimiter.config.timestamp_field
13
30
  end
14
31
 
32
+ def self.source=(value)
33
+ RateLimiter.store[:source] = value
34
+ end
35
+
36
+ def self.source
37
+ RateLimiter.store[:source]
38
+ end
39
+
40
+ def self.controller_info=(value)
41
+ RateLimiter.store[:controller_info] = value
42
+ end
43
+
44
+ def self.controller_info
45
+ RateLimiter.store[:controller_info]
46
+ end
47
+
15
48
  private
16
49
 
50
+ def self.store
51
+ Thread.current[:rate_limiter] || { :request_enabled_for_controller => true }
52
+ end
53
+
17
54
  def self.config
18
55
  @@config ||= RateLimiter::Config.instance
19
56
  end
@@ -22,3 +59,7 @@ end
22
59
  ActiveSupport.on_load(:active_record) do
23
60
  include RateLimiter::Model
24
61
  end
62
+
63
+ ActiveSupport.on_load(:action_controller) do
64
+ include RateLimiter::Controller
65
+ end
@@ -1,9 +1,10 @@
1
1
  module RateLimiter
2
2
  class Config
3
3
  include Singleton
4
- attr_accessor :timestamp_field
4
+ attr_accessor :enabled, :timestamp_field
5
5
 
6
6
  def initialize
7
+ @enabled = true
7
8
  @timestamp_field = :created_at
8
9
  end
9
10
  end
@@ -0,0 +1,37 @@
1
+ module RateLimiter
2
+ module Controller
3
+ def self.included(base)
4
+ base.before_filter :set_rate_limiter_source
5
+ base.before_filter :set_rate_limiter_controller_info
6
+ base.before_filter :set_rate_limiter_enabled_for_controller
7
+ end
8
+
9
+ protected
10
+
11
+ def user_for_rate_limiter
12
+ current_user rescue nil
13
+ end
14
+
15
+ def info_for_rate_limiter
16
+ {}
17
+ end
18
+
19
+ def rate_limiter_enabled_for_controller
20
+ true
21
+ end
22
+
23
+ private
24
+
25
+ def set_rate_limiter_source
26
+ ::RateLimiter.source = user_for_rate_limiter
27
+ end
28
+
29
+ def set_rate_limiter_controller_info
30
+ ::RateLimiter.controller_info = info_for_rate_limiter
31
+ end
32
+
33
+ def set_rate_limiter_enabled_for_controller
34
+ ::RateLimiter.enabled_for_controller = rate_limiter_enabled_for_controller
35
+ end
36
+ end
37
+ end
@@ -20,13 +20,24 @@ module RateLimiter
20
20
  class_attribute :rate_limit_unless_condition
21
21
  self.rate_limit_unless_condition = options[:unless]
22
22
 
23
+ class_attribute :rate_limit_enabled_for_model
24
+ self.rate_limit_enabled_for_model = true
25
+
26
+ def rate_limit_off
27
+ self.rate_limit_enabled_for_model = false
28
+ end
29
+
30
+ def rate_limit_on
31
+ self.rate_limit_enabled_for_model = true
32
+ end
33
+
23
34
  self.before_create :check_rate_limit
24
35
  end
25
36
  end
26
37
 
27
38
  module InstanceMethods
28
39
  def check_rate_limit
29
- if rate_limit?
40
+ if switched_on? && rate_limit?
30
41
  klass = self.class
31
42
 
32
43
  others = klass.where("#{klass.rate_limit_on.to_s} = ? AND #{RateLimiter.config.timestamp_field.to_s} >= ?", self.send(klass.rate_limit_on), Time.now - klass.rate_limit_interval)
@@ -44,6 +55,10 @@ module RateLimiter
44
55
  end
45
56
  end
46
57
 
58
+ def switched_on?
59
+ RateLimiter.enabled? && RateLimiter.enabled_for_controller? && self.class.rate_limit_enabled_for_model
60
+ end
61
+
47
62
  def rate_limit?
48
63
  (rate_limit_if_condition.blank? || rate_limit_if_condition.call(self)) && !rate_limit_unless_condition.try(:call, self)
49
64
  end
@@ -1,3 +1,3 @@
1
1
  module RateLimiter
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rate_limiter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-12 00:00:00.000000000 Z
12
+ date: 2012-12-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -76,6 +76,7 @@ files:
76
76
  - Rakefile
77
77
  - lib/rate_limiter.rb
78
78
  - lib/rate_limiter/config.rb
79
+ - lib/rate_limiter/controller.rb
79
80
  - lib/rate_limiter/model.rb
80
81
  - lib/rate_limiter/version.rb
81
82
  - rate_limiter.gemspec