class Rack::IpRestrictor::Restriction

Handles restrictions

Public Class Methods

new(*args) click to toggle source

Inits a new restriction

@example Path as String

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

@example Path as Regexp

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

@example List of paths; Strings and Regexps can be combined

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

@param [Array<String, Regexp, Hash>] *args

@todo Add other options, i.e. an array of IP groups

:only => [:test1, :admins]
# 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

Public Instance Methods

validate(env, remote_addr) click to toggle source

Validates, if a request (with a remote_address) is allowed to access the requested path @see Middleware#call

# 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

Private Instance Methods

concerns_ip?(remote_addr) click to toggle source

@return [Boolean] Is the remote_addr included in a configured IP range?

# File lib/rack_ip_restrictor/restriction.rb, line 45
def concerns_ip?(remote_addr)
  Rack::IpRestrictor.config.ips_for(@options[:only]).include?(remote_addr)
end
concerns_path?(request_path) click to toggle source

@return [Boolean] Does the request concern a configured path?

# File lib/rack_ip_restrictor/restriction.rb, line 50
def concerns_path?(request_path)
  @paths.each do |path|
    return true if path.is_a? String and path == request_path
    return true if path.is_a? Regexp and path =~ request_path
  end
  false
end