Class: Grape::Attack::Limiter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, adapter = ::Grape::Attack.config.adapter) ⇒ Limiter

Returns a new instance of Limiter.



10
11
12
13
14
# File 'lib/grape/attack/limiter.rb', line 10

def initialize(env, adapter = ::Grape::Attack.config.adapter)
  @request = ::Grape::Attack::Request.new(env)
  @adapter = adapter
  @counter = ::Grape::Attack::Counter.new(@request, @adapter)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#counterObject (readonly)

Returns the value of attribute counter.



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

def counter
  @counter
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

Instance Method Details

#call!Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/grape/attack/limiter.rb', line 16

def call!
  return if disable?
  return unless throttle?

  if allowed?
    update_counter
    set_rate_limit_headers
  else
    fail ::Grape::Attack::RateLimitExceededError.new("API rate limit exceeded for #{request.client_identifier}.")
  end
end