Class: Rack::Restrictor

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

Constant Summary collapse

PAGE_CODE =
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n  <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n    <head>\n      <title> Anauthorized access</title>\n    </head>\n    <body>\n      <h1> Anauthorized access</h1>\n    </body>\n  </html>\n"

Instance Method Summary collapse

Constructor Details

#initialize(app, plage) ⇒ Restrictor

Returns a new instance of Restrictor.



17
18
19
20
# File 'lib/rack_restrictor.rb', line 17

def initialize(app, plage)
  @app   = app
   @plage = IPAddr.new( plage.to_s )    
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/rack_restrictor.rb', line 22

def call(env)
  if authorized?( env['REMOTE_ADDR'].to_s )
    status, headers, body = @app.call(env)
  else
    status, headers, body = 
     [401, {"Content-Type" => "text/html"}, PAGE_CODE]
  end

  [status, headers, body]
end