Class: Pause::Action
- Inherits:
-
Object
- Object
- Pause::Action
- Defined in:
- lib/pause/action.rb
Instance Attribute Summary collapse
-
#identifier ⇒ Object
Returns the value of attribute identifier.
Class Method Summary collapse
-
.check(*args) ⇒ Object
Action subclasses should define their checks as follows.
- .checks ⇒ Object
- .checks=(period_checks) ⇒ Object
- .disable ⇒ Object
- .disabled? ⇒ Boolean
-
.enable ⇒ Object
Actions can be globally disabled or re-enabled in a persistent way.
- .enabled? ⇒ Boolean
- .rate_limited_identifiers ⇒ Object
- .scope(scope_identifier = nil) ⇒ Object
- .tracked_identifiers ⇒ Object
- .unblock_all ⇒ Object
Instance Method Summary collapse
- #analyze ⇒ Object
- #checks ⇒ Object
- #increment!(count = 1, timestamp = Time.now.to_i) ⇒ Object
-
#initialize(identifier) ⇒ Action
constructor
A new instance of Action.
- #ok? ⇒ Boolean
- #rate_limited? ⇒ Boolean
-
#scope ⇒ Object
Action subclasses should define their scope as follows.
- #unblock ⇒ Object
Constructor Details
#initialize(identifier) ⇒ Action
Returns a new instance of Action.
5 6 7 8 |
# File 'lib/pause/action.rb', line 5 def initialize(identifier) @identifier = identifier self.class.checks = [] unless self.class.instance_variable_get(:@checks) end |
Instance Attribute Details
#identifier ⇒ Object
Returns the value of attribute identifier.
3 4 5 |
# File 'lib/pause/action.rb', line 3 def identifier @identifier end |
Class Method Details
.check(*args) ⇒ Object
Action subclasses should define their checks as follows
period_seconds - compare all activity by an identifier within the time period
max_allowed - if the number of actions by an identifier exceeds max_allowed for the time period marked
by period_seconds, it is no longer ok.
block_ttl - time to mark identifier as not ok
class MyAction < Pause::Action
check period_seconds: 60, max_allowed: 100, block_ttl: 3600
check period_seconds: 1800, max_allowed: 2000, block_ttl: 3600
end
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pause/action.rb', line 37 def self.check(*args) @checks ||= [] period_seconds, max_allowed, block_ttl = if args.first.is_a?(Hash) [args.first[:period_seconds], args.first[:max_allowed], args.first[:block_ttl]] else args end @checks << Pause::PeriodCheck.new(period_seconds, max_allowed, block_ttl) end |
.checks ⇒ Object
48 49 50 |
# File 'lib/pause/action.rb', line 48 def self.checks @checks end |
.checks=(period_checks) ⇒ Object
56 57 58 |
# File 'lib/pause/action.rb', line 56 def self.checks=(period_checks) @checks = period_checks end |
.disable ⇒ Object
110 111 112 |
# File 'lib/pause/action.rb', line 110 def self.disable adapter.disable(class_scope) end |
.disabled? ⇒ Boolean
118 119 120 |
# File 'lib/pause/action.rb', line 118 def self.disabled? ! enabled? end |
.enable ⇒ Object
Actions can be globally disabled or re-enabled in a persistent way.
MyAction.disable
MyAction.enabled? => false
MyAction.disabled? => true
MyAction.enable
MyAction.enabled? => true
MyAction.disabled? => false
106 107 108 |
# File 'lib/pause/action.rb', line 106 def self.enable adapter.enable(class_scope) end |
.enabled? ⇒ Boolean
114 115 116 |
# File 'lib/pause/action.rb', line 114 def self.enabled? adapter.enabled?(class_scope) end |
.rate_limited_identifiers ⇒ Object
83 84 85 |
# File 'lib/pause/action.rb', line 83 def self.rate_limited_identifiers adapter.rate_limited_keys(self.class_scope) end |
.scope(scope_identifier = nil) ⇒ Object
20 21 22 23 |
# File 'lib/pause/action.rb', line 20 def self.scope(scope_identifier = nil) class_variable_set(:@@class_scope, scope_identifier) define_method(:scope) { scope_identifier } end |
.tracked_identifiers ⇒ Object
79 80 81 |
# File 'lib/pause/action.rb', line 79 def self.tracked_identifiers adapter.all_keys(self.class_scope) end |
.unblock_all ⇒ Object
87 88 89 |
# File 'lib/pause/action.rb', line 87 def self.unblock_all adapter.delete_rate_limited_keys(self.class_scope) end |
Instance Method Details
#analyze ⇒ Object
75 76 77 |
# File 'lib/pause/action.rb', line 75 def analyze Pause.analyzer.check(self) end |
#checks ⇒ Object
52 53 54 |
# File 'lib/pause/action.rb', line 52 def checks self.class.checks end |
#increment!(count = 1, timestamp = Time.now.to_i) ⇒ Object
60 61 62 |
# File 'lib/pause/action.rb', line 60 def increment!(count = 1, = Time.now.to_i) adapter.increment(scope, identifier, , count) end |
#ok? ⇒ Boolean
68 69 70 71 72 73 |
# File 'lib/pause/action.rb', line 68 def ok? Pause.analyzer.check(self).nil? rescue ::Redis::CannotConnectError => e Pause::Logger.fatal "Error connecting to redis: #{e.inspect} #{e.} #{e.backtrace.join("\n")}" false end |
#rate_limited? ⇒ Boolean
64 65 66 |
# File 'lib/pause/action.rb', line 64 def rate_limited? ! ok? end |
#scope ⇒ Object
16 17 18 |
# File 'lib/pause/action.rb', line 16 def scope raise "Should implement scope. (Ex: ipn:follow)" end |
#unblock ⇒ Object
91 92 93 |
# File 'lib/pause/action.rb', line 91 def unblock adapter.delete_rate_limited_key(scope, identifier) end |