Class: Rack::Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/private.rb,
lib/rack/private/version.rb

Constant Summary collapse

VERSION =
"0.1.8"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Private

Returns a new instance of Private.



4
5
6
7
# File 'lib/rack/private.rb', line 4

def initialize(app, options = {})
  @app = app
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rack/private.rb', line 9

def call(env)
  request = Rack::Request.new(env)
  
  # Check code in session and return Rails call if is valid
  return @app.call(env) if (already_auth?(request) || public_page?(request))
  
  # If post method check :code_param value
  if request.post? && code_valid?(request.params["private_code"])
    request.session[:private_code] = request.params["private_code"]
    body = "<html><body>Secret code is valid.</body></html>"
    [303, { 'Content-Type' => 'text/html', 'Location' => '/' }, [body]] # Redirect if code is valid
  else
    render_private_form
  end
end