Class: Pause::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/pause/action.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#identifierObject

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

.checksObject



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

.disableObject



110
111
112
# File 'lib/pause/action.rb', line 110

def self.disable
  adapter.disable(class_scope)
end

.disabled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/pause/action.rb', line 118

def self.disabled?
  ! enabled?
end

.enableObject

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

Returns:

  • (Boolean)


114
115
116
# File 'lib/pause/action.rb', line 114

def self.enabled?
  adapter.enabled?(class_scope)
end

.rate_limited_identifiersObject



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_identifiersObject



79
80
81
# File 'lib/pause/action.rb', line 79

def self.tracked_identifiers
  adapter.all_keys(self.class_scope)
end

.unblock_allObject



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

#analyzeObject



75
76
77
# File 'lib/pause/action.rb', line 75

def analyze
  Pause.analyzer.check(self)
end

#checksObject



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, timestamp = Time.now.to_i)
  adapter.increment(scope, identifier, timestamp, count)
end

#ok?Boolean

Returns:

  • (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.message} #{e.backtrace.join("\n")}"
  false
end

#rate_limited?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/pause/action.rb', line 64

def rate_limited?
  ! ok?
end

#scopeObject

Action subclasses should define their scope as follows

class MyAction < Pause::Action
  scope "my:scope"
end


16
17
18
# File 'lib/pause/action.rb', line 16

def scope
  raise "Should implement scope. (Ex: ipn:follow)"
end

#unblockObject



91
92
93
# File 'lib/pause/action.rb', line 91

def unblock
  adapter.delete_rate_limited_key(scope, identifier)
end