Class: Rack::Dedos::Filters::UserAgent

Inherits:
Base
  • Object
show all
Defined in:
lib/rack/dedos/filters/user_agent.rb

Constant Summary

Constants inherited from Base

Base::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Base

#app, #cache, #details, #logger, #options

Instance Method Summary collapse

Methods inherited from Base

#call

Constructor Details

#initializeUserAgent

Returns a new instance of UserAgent.

Parameters:

  • options (Hash)

    a customizable set of options



14
15
16
17
# File 'lib/rack/dedos/filters/user_agent.rb', line 14

def initialize(...)
  super
  @cache_period = options[:cache_period] || 900
end

Instance Method Details

#allowed?(request, ip) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/dedos/filters/user_agent.rb', line 19

def allowed?(request, ip)
  case cache.get(ip)
  when nil              # first contact
    cache.set(ip, request.user_agent, expires_in: @cache_period)
    true
  when 'BLOCKED'        # already blocked
    false
  when request.user_agent   # user agent hasn't changed
    true
  else   # user agent has changed
    cache.set(ip, 'BLOCKED', expires_in: @cache_period)
    false
  end
rescue => error
  logger.error("request from #{ip} allowed due to error: #{error.message}")
  true
end

#nameObject



8
9
10
# File 'lib/rack/dedos/filters/user_agent.rb', line 8

def name
  :user_agent
end