Class: Rack::Dedos::Filters::Base

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

Direct Known Subclasses

Country, UserAgent

Constant Summary collapse

DEFAULT_OPTIONS =
{
  only_paths: [],
  except_paths: [],
  status: 403,
  text: 'Forbidden (Temporarily Blocked by Rules)'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Base

Returns a new instance of Base.

Parameters:

  • app (#call)
  • options (Hash{Symbol => Object}) (defaults to: {})


21
22
23
24
25
# File 'lib/rack/dedos/filters/base.rb', line 21

def initialize(app, options = {})
  @app = app
  @options = DEFAULT_OPTIONS.merge(options)
  @details = nil
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#detailsObject (readonly)

Returns the value of attribute details.



17
18
19
# File 'lib/rack/dedos/filters/base.rb', line 17

def details
  @details
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/rack/dedos/filters/base.rb', line 16

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rack/dedos/filters/base.rb', line 27

def call(env)
  request = Rack::Request.new(env)
  ip = real_ip(request)
  if !apply?(request) || allowed?(request, ip)
    app.call(env)
  else
    message = "rack-dedos: request #{request.path} from #{ip} blocked by #{self.class}"
    warn([message, details].compact.join(": "))
    [options[:status], { 'Content-Type' => 'text/plain' }, [options[:text]]]
  end
end