Class: Rack::Auth::IP

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/rack/auth/ip.rb

Overview

# you can use block

# ip is IPAddr instance.
use Rack::Auth::IP do |ip|
  Your::Model::IP.count({ :ip => ip.to_s }) != 0 
end

Defined Under Namespace

Modules: Util

Instance Method Summary collapse

Methods included from Util

detect_ip

Constructor Details

#initialize(app, ip_list = nil, &block) ⇒ IP

Returns a new instance of IP.



29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/auth/ip.rb', line 29

def initialize app, ip_list=nil, &block
  @app = app
  @ip_list = ip_list

  if @ip_list 
    @ip_list = @ip_list.map {|ip| IPAddr.new(ip) }
  end

  @cond = block
end

Instance Method Details

#call(env) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rack/auth/ip.rb', line 40

def call env
  req_ip = IPAddr.new(detect_ip(env))

  if @ip_list
    if @ip_list.find {|ip| ip.include? req_ip }
      return @app.call(env)
    end
  else
    if @cond && @cond.call(req_ip)
      return @app.call(env)
    end
  end
  return [403, {'Content-Type' => 'text/plain' }, 'Forbidden' ]
end