Class: Rack::IpRestrictor::Restriction

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_ip_restrictor/restriction.rb

Overview

Handles restrictions

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Restriction

TODO:

Add other options, i.e. an array of IP groups :only => [:test1, :admins]

Inits a new restriction

Examples:

Path as String

restrict '/admin', :only => :test

Path as Regexp

restrict /^\/admin/, :only => :test

List of paths; Strings and Regexps can be combined

restrict /^\/admin/, '/internal', '/secret', :only => :test

Raises:

  • (Exception)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack_ip_restrictor/restriction.rb', line 18

def initialize(*args)
  @options = args.extract_options!

  raise Exception, "invalid argument" unless @options.has_key? :only and @options[:only].is_a? Symbol

  @paths = args
  @paths.each do |path|
    raise Exception, "invalid path argument" unless path.is_a? String or path.is_a? Regexp
  end

end

Instance Method Details

#validate(env, remote_addr) ⇒ Object

Validates, if a request (with a remote_address) is allowed to access the requested path

See Also:



32
33
34
35
36
37
38
39
40
# File 'lib/rack_ip_restrictor/restriction.rb', line 32

def validate(env, remote_addr)
  @paths.each do |path|
    if concerns_path?(env["PATH_INFO"]) and not concerns_ip?(remote_addr)
      return false
    end
  end

  true
end