DRY up initial values

This commit is contained in:
Aaron Suggs 2012-07-28 22:09:56 -04:00
parent c90a0182eb
commit cf49b00c00

View file

@ -11,15 +11,15 @@ module Rack::Attack
attr_accessor :blacklisted_response, :throttled_response
def whitelist(name, &block)
(@whitelists ||= {})[name] = Whitelist.new(name, block)
self.whitelists[name] = Whitelist.new(name, block)
end
def blacklist(name, &block)
(@blacklists ||= {})[name] = Blacklist.new(name, block)
self.blacklists[name] = Blacklist.new(name, block)
end
def throttle(name, options, &block)
(@throttles ||= {})[name] = Throttle.new(name, options, block)
self.throttles[name] = Throttle.new(name, options, block)
end
def whitelists; @whitelists ||= {}; end
@ -27,19 +27,20 @@ module Rack::Attack
def throttles; @throttles ||= {}; end
def new(app)
@app = app
# Set defaults
@cache ||= Cache.new
@notifier = ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
@blacklisted_response = lambda {|env| [503, {}, ['Blocked']] }
@throttled_response = lambda {|env|
@notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
@blacklisted_response ||= lambda {|env| [503, {}, ['Blocked']] }
@throttled_response ||= lambda {|env|
retry_after = env['rack.attack.throttled'][:period] rescue nil
[503, {'Retry-After' => retry_after}, ['Retry later']]
}
@app = app
self
end
def call(env)
req = Rack::Request.new(env)