Class: Limit

Inherits:
Object
  • Object
show all
Defined in:
lib/better_rate_limit/limit.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max, controller_path, options) ⇒ Limit

Returns a new instance of Limit.



18
19
20
21
22
# File 'lib/better_rate_limit/limit.rb', line 18

def initialize(max, controller_path, options)
  @max = max
  @controller_path = controller_path
  @options = options
end

Instance Attribute Details

#controller_pathObject (readonly)

Returns the value of attribute controller_path.



16
17
18
# File 'lib/better_rate_limit/limit.rb', line 16

def controller_path
  @controller_path
end

#maxObject (readonly)

Returns the value of attribute max.



16
17
18
# File 'lib/better_rate_limit/limit.rb', line 16

def max
  @max
end

Class Method Details

.build(max, controller_path, options) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/better_rate_limit/limit.rb', line 2

def self.build(max, controller_path, options)
  options.assert_valid_keys(:if, :unless, :every, :name, :scope, :only, :except, :clear_if)
  new(max, controller_path, {
    if: options[:if],
    unless: options[:unless],
    every: options[:every],
    name: options[:name],
    scope: options[:scope],
    only: options[:only],
    except: options[:except],
    clear_if: options[:clear_if]
  })
end

Instance Method Details

#_ifObject



24
25
26
# File 'lib/better_rate_limit/limit.rb', line 24

def _if
  @options[:if]
end

#_unlessObject



36
37
38
# File 'lib/better_rate_limit/limit.rb', line 36

def _unless
  @options[:unless]
end

#clear_ifObject



52
53
54
# File 'lib/better_rate_limit/limit.rb', line 52

def clear_if
  @options[:clear_if]
end

#clear_if_present?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/better_rate_limit/limit.rb', line 56

def clear_if_present?
  @options[:clear_if].present?
end

#controller_path_is?(controller_path) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/better_rate_limit/limit.rb', line 68

def controller_path_is?(controller_path)
  self.controller_path == controller_path
end

#everyObject



32
33
34
# File 'lib/better_rate_limit/limit.rb', line 32

def every
  @options[:every]
end

#exceptObject



40
41
42
# File 'lib/better_rate_limit/limit.rb', line 40

def except
  @options[:except]
end

#has_if_condition?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/better_rate_limit/limit.rb', line 60

def has_if_condition?
  _if.present?
end

#has_unless_condition?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/better_rate_limit/limit.rb', line 64

def has_unless_condition?
  _unless.present?
end

#key(key_scope) ⇒ Object



72
73
74
# File 'lib/better_rate_limit/limit.rb', line 72

def key(key_scope)
  ['controller_throttle', name, max, every, key_scope].join(':')
end

#nameObject



28
29
30
# File 'lib/better_rate_limit/limit.rb', line 28

def name
  @options[:name]
end

#onlyObject



44
45
46
# File 'lib/better_rate_limit/limit.rb', line 44

def only
  @options[:only]
end

#scopeObject



48
49
50
# File 'lib/better_rate_limit/limit.rb', line 48

def scope
  @options[:scope]
end