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