class Rack::RestrictAccess::Filter

Attributes

filter_type[R]

Public Class Methods

new(&blk) click to toggle source
# File lib/rack/restrict_access.rb, line 99
def initialize(&blk)
  @ips = []
  @resources = []
  @applies_to_all_resources = false

  if block_given?
    instance_eval(&blk)
  end
end

Public Instance Methods

all_resources!() click to toggle source
# File lib/rack/restrict_access.rb, line 136
def all_resources!
  @applies_to_all_resources = true
end
applies_to_ip?(remote_addr) click to toggle source
# File lib/rack/restrict_access.rb, line 118
def applies_to_ip?(remote_addr)
  raise TypeError, remote_addr unless remote_addr.respond_to? :to_str
  remote_addr = remote_addr.to_str
  @ips.any? do |ip|
    !(remote_addr =~ ip).nil?
  end
end
applies_to_resource?(requested_resource) click to toggle source
# File lib/rack/restrict_access.rb, line 109
def applies_to_resource?(requested_resource)
  return true if @applies_to_all_resources
  raise TypeError, requested_resource unless requested_resource.respond_to? :to_str
  requested_resource = requested_resource.to_str
  @resources.any? do |resource|
    !(requested_resource =~ resource).nil?
  end
end
ip_to_regexp(ip) click to toggle source
# File lib/rack/restrict_access.rb, line 155
def ip_to_regexp(ip)
  if ip.respond_to? :to_str
    /^#{Regexp.escape(ip)}$/
  elsif ip.respond_to? :match
    ip
  else
    raise TypeError, ip
  end
end
origin_ips(*ips) click to toggle source
# File lib/rack/restrict_access.rb, line 165
def origin_ips(*ips)
  options = ips.pop if ips.last.is_a? Hash
  options ||= {}
  delimiter = options.fetch(:delimiter, false)

  concat_new_attributes(args: ips, ivar: @ips) do |ip|
    ip = ip.split(delimiter) if delimiter && ip.is_a?(String)
    if ip.is_a? Array
      ip.map! { |addr| ip_to_regexp(addr) }
    else
      ip_to_regexp(ip)
    end
  end
end
path_to_regexp(path) click to toggle source
# File lib/rack/restrict_access.rb, line 126
def path_to_regexp(path)
  if path.respond_to? :to_str
    /^#{Regexp.escape(path)}\/?$/
  elsif path.respond_to? :match
    path
  else
    raise TypeError, path
  end
end
resources(*paths) click to toggle source
# File lib/rack/restrict_access.rb, line 140
def resources(*paths)
  options = paths.pop if paths.last.is_a? Hash
  options ||= {}
  delimiter = options.fetch(:delimiter, false)

  concat_new_attributes(args: paths, ivar: @resources) do |path|
    path = path.split(delimiter) if delimiter && path.is_a?(String)
    if path.is_a? Array
      path.map! { |pt| path_to_regexp(pt) }
    else
      path_to_regexp(path)
    end
  end
end

Private Instance Methods

concat_new_attributes(options, &blk) click to toggle source
# File lib/rack/restrict_access.rb, line 181
def concat_new_attributes(options, &blk)
  args = options.fetch(:args, [])
  ivar = options.fetch(:ivar, nil)
  raise ArgumentError, "Missing :ivar option" unless ivar
  values_to_save = args.flatten.map do |arg|
    blk.call(arg)
  end.flatten
  ivar.concat(values_to_save)
end