Class: Grape::Attack::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/attack/counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, adapter) ⇒ Counter

Returns a new instance of Counter.



7
8
9
10
# File 'lib/grape/attack/counter.rb', line 7

def initialize(request, adapter)
  @request = request
  @adapter = adapter
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



5
6
7
# File 'lib/grape/attack/counter.rb', line 5

def adapter
  @adapter
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/grape/attack/counter.rb', line 5

def request
  @request
end

Instance Method Details

#updateObject



20
21
22
23
24
25
26
# File 'lib/grape/attack/counter.rb', line 20

def update
  adapter.atomically do
    adapter.incr(key)
    adapter.expire(key, ttl_in_seconds)
  end
rescue ::Grape::Attack::StoreError
end

#valueObject



12
13
14
15
16
17
18
# File 'lib/grape/attack/counter.rb', line 12

def value
  @value ||= begin
    adapter.get(key).to_i
  rescue ::Grape::Attack::StoreError
    1
  end
end