Skip to content

Instantly share code, notes, and snippets.

@coryodaniel
Created February 17, 2009 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coryodaniel/65565 to your computer and use it in GitHub Desktop.
Save coryodaniel/65565 to your computer and use it in GitHub Desktop.
##
# A succinct wrapper for before filters to create threshold
#
# @param *args [~Array]
# args array for handling array of action names and threshold options
#
# @param threshold_options [Array]
# Array of actions to apply threshold; passed to before filters :only option
#
# @param opts [Hash]
# * :mode [Symbol] :captcha, :wait, :halt
# Action to take when threshold is exceeded:
# :captcha - displays captcha
# :wait - display wait message / resource busy
# :halt - halts begin filter chain
# Default :captcha
#
# * :limit [Frequency] number of access per time interval before
# threshold constraints are applied
# Default 0.per(0.seconds) #Always
#
# * :halt_with [String,Symbol,Proc] Halts the filter chain instead of
# displaying a captcha
# This option is only used when :mode => :halt
# takes same params as before filter's throw :halt
# not specifying :halt_with when the mode is :halt
# will result in: throw(:halt)
#
# * :penalize [Boolean] When penalizing the most recent access time (now)
# will be the basis for the next limit test.
# Thus, if a resource can be accessed 1 time per 30 seconds
# and it has been 28 seconds, and it is accessed, when penalizing
# the user will have to wait 30 more seconds, when not penalizing the
# user has to wait 2 more seconds.
# Default: false
#
#
#
# * :if / :unless - Passed to :if / :unless on before filter
#
# @example
# Using threshold and the before filter it creates:
#
# class MyController < Application
# #Captcha every time :index is accessed
#
# threshold :index
# # Equivalent to:
# before nil, :only => [:index] do
# threshold_exceeded?
# end
#
# class MyController < Application
# #Captcha every time controller is accessed
# threshold
# # Equivalent to:
# before { threshold_exceeded? }
#
# class MyController < Application
# # Allow 3 uses per 2 minutes, beyond that tell the user they must
# # wait 2 minutes
#
# threshold :index, :create, :mode => :wait, :limit => 3.per(2.minutes)
#
# # Equivalent to:
# before nil, :only => [:index, :create] do
# threshold_exceeded? :mode => :wait, :limit => 3.per(2.minutes)
# end
#
# class MyController < Application
# # Allow the access of :create 1 per 30 seconds, beyond that captcha user
# threshold :create, :limit => 1.per(30.seconds)
#
# # Equivalent to:
# before nil, :only => [:create] do
# threshold_exceeded? :limit => 1.per(30.seconds)
# end
#
# class MyController < Application
# #captcha the user on every access of this controller except
# # for the given piece of logic
# threshold :unless => lambda{ ... cool logic here ... }
#
# # Equivalent to:
# before nil, :unless => lambda{... cool logic...} do
# threshold_exceeded?
# end
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment