From 5c78f84be2ff444f59b7a50d34c93ad723e8ef7b Mon Sep 17 00:00:00 2001 From: Zach Millman Date: Tue, 17 Jun 2014 11:37:25 -0700 Subject: [PATCH 1/2] Improve documentation of Rack::Attack::Request https://github.com/kickstarter/rack-attack/issues/73 --- README.md | 6 +++++- lib/rack/attack/request.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7703b1a..faaf8e6 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ The algorithm is actually more concise in code: See [Rack::Attack.call](https:// ```ruby def call(env) - req = Rack::Request.new(env) + req = Rack::Attack::Request.new(env) if whitelisted?(req) @app.call(env) @@ -72,6 +72,10 @@ def call(env) end ``` +Note: `Rack::Attack::Request` is just a subclass of `Rack::Attack` so that you +can cleanly [monkey patch helper methods] onto the +[request object](https://github.com/kickstarter/rack-attack/blob/master/lib/rack/attack/request.rb). + ## About Tracks `Rack::Attack.track` doesn't affect request processing. Tracks are an easy way to log and measure requests matching arbitrary attributes. diff --git a/lib/rack/attack/request.rb b/lib/rack/attack/request.rb index 603a126..0dc9681 100644 --- a/lib/rack/attack/request.rb +++ b/lib/rack/attack/request.rb @@ -1,3 +1,16 @@ +# Rack::Attack::Request is the same as ::Rack::Request by default. +# +# This is a safe place to add custom helper methods to the request object +# through monkey patching: +# +# class Rack::Attack::Request < ::Rack::Request +# def localhost? +# ip == "127.0.0.1" +# end +# end +# +# Rack::Attack.whitelist("localhost") {|req| req.localhost? } +# module Rack class Attack class Request < ::Rack::Request From 882221212aad4f069b0902c020cc0a1222a34960 Mon Sep 17 00:00:00 2001 From: Zach Millman Date: Tue, 17 Jun 2014 11:38:50 -0700 Subject: [PATCH 2/2] Fix formatting of note on Rack::Attack::Request --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index faaf8e6..2617d21 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ end ``` Note: `Rack::Attack::Request` is just a subclass of `Rack::Attack` so that you -can cleanly [monkey patch helper methods] onto the +can cleanly monkey patch helper methods onto the [request object](https://github.com/kickstarter/rack-attack/blob/master/lib/rack/attack/request.rb). ## About Tracks