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 =
{
  status: 403,
  text: 'Forbidden (Temporarily Blocked by Rules)'
}

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: {})


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

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



13
14
15
# File 'lib/rack/dedos/filters/base.rb', line 13

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/rack/dedos/filters/base.rb', line 23

def call(env)
  request = Rack::Request.new(env)
  ip = real_ip(request)
  if allowed?(request, ip)
    app.call(env)
  else
    warn("rack-dedos: request from #{ip} blocked by #{self.class} `#{@country_code.inspect}'")
    [options[:status], { 'Content-Type' => 'text/plain' }, [options[:text]]]
  end
end