Class: Rack::SimpleRackBouncer
- Inherits:
-
Object
- Object
- Rack::SimpleRackBouncer
- Defined in:
- lib/rack/simple_rack_bouncer.rb
Instance Attribute Summary collapse
-
#ip_address_checks ⇒ Object
readonly
Returns the value of attribute ip_address_checks.
-
#user_agent_checks ⇒ Object
readonly
Used for testing.
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #access_denied?(env) ⇒ Boolean
- #call(env) ⇒ Object
-
#configure(options = {}) ⇒ Object
Accepted options are: :deny_user_agents and :deny_ip_address Both accept a single or an array of String, Regexp or Proc objects If a string is given, IP address will be matched by prefix (i.e. ‘127.0’ will match ‘127.0.0.1’ and ‘127.0.2.1’).
-
#initialize(app, options = {}) ⇒ SimpleRackBouncer
constructor
See configure for available options.
- #ip_denied?(ip) ⇒ Boolean
- #match_ip?(check, ip) ⇒ Boolean
- #match_user_agent?(check, ua) ⇒ Boolean
- #user_agent_denied?(ua) ⇒ Boolean
Constructor Details
#initialize(app, options = {}) ⇒ SimpleRackBouncer
See configure for available options
5 6 7 8 |
# File 'lib/rack/simple_rack_bouncer.rb', line 5 def initialize(app, = {}) @app = app configure() end |
Instance Attribute Details
#ip_address_checks ⇒ Object (readonly)
Returns the value of attribute ip_address_checks.
72 73 74 |
# File 'lib/rack/simple_rack_bouncer.rb', line 72 def ip_address_checks @ip_address_checks end |
#user_agent_checks ⇒ Object (readonly)
Used for testing
71 72 73 |
# File 'lib/rack/simple_rack_bouncer.rb', line 71 def user_agent_checks @user_agent_checks end |
Instance Method Details
#_call(env) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/rack/simple_rack_bouncer.rb', line 23 def _call(env) if access_denied?(env) deny_access else @app.call(env) end end |
#access_denied?(env) ⇒ Boolean
31 32 33 |
# File 'lib/rack/simple_rack_bouncer.rb', line 31 def access_denied?(env) ip_denied?(env['REMOTE_ADDR']) || user_agent_denied?(env['HTTP_USER_AGENT']) end |
#call(env) ⇒ Object
19 20 21 |
# File 'lib/rack/simple_rack_bouncer.rb', line 19 def call(env) dup._call(env) end |
#configure(options = {}) ⇒ Object
Accepted options are: :deny_user_agents and :deny_ip_address Both accept a single or an array of String, Regexp or Proc objects If a string is given, IP address will be matched by prefix (i.e. ‘127.0’ will match ‘127.0.0.1’ and ‘127.0.2.1’)
13 14 15 16 17 |
# File 'lib/rack/simple_rack_bouncer.rb', line 13 def configure( = {}) @user_agent_checks = [[:deny_user_agent]].flatten.compact @ip_address_checks = [[:deny_ip_address]].flatten.compact @redirect_url = [[:redirect]].flatten.compact end |
#ip_denied?(ip) ⇒ Boolean
35 36 37 |
# File 'lib/rack/simple_rack_bouncer.rb', line 35 def ip_denied?(ip) ip && @ip_address_checks.any? { |check| match_ip?(check, ip) } end |
#match_ip?(check, ip) ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rack/simple_rack_bouncer.rb', line 39 def match_ip?(check, ip) case check when Regexp check =~ ip when String ip[0, check.size] == check when Proc check.call(ip) else false end end |
#match_user_agent?(check, ua) ⇒ Boolean
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rack/simple_rack_bouncer.rb', line 57 def match_user_agent?(check, ua) case check when Regexp check =~ ua when String ua == check when Proc check.call(ua) else false end end |
#user_agent_denied?(ua) ⇒ Boolean
52 53 54 55 |
# File 'lib/rack/simple_rack_bouncer.rb', line 52 def user_agent_denied?(ua) ua ||= '' @user_agent_checks.any? { |check| match_user_agent?(check, ua) } end |