Class: Wee::Application

Inherits:
Object show all
Defined in:
lib/wee/application.rb

Overview

A Wee::Application manages all Session’s of a single application. It dispatches the request to the correct handler by examining the request.

Defined Under Namespace

Classes: SessionCache

Instance Method Summary collapse

Constructor Details

#initialize(max_sessions = 10_000, &block) ⇒ Application

Creates a new application. The block, when called, must return a new Session instance.

Wee::Application.new { Wee::Session.new(root_component) }


25
26
27
28
29
30
# File 'lib/wee/application.rb', line 25

def initialize(max_sessions=10_000, &block)
  @session_factory = block || raise(ArgumentError)
  @session_ids ||= Wee::IdGenerator::Secure.new
  @sessions = SessionCache.new(max_sessions)
  @mutex = Mutex.new
end

Instance Method Details

#call(env) ⇒ Object

Handles a web request



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wee/application.rb', line 42

def call(env)
  request = Wee::Request.new(env)

  if request.session_id
    session = @mutex.synchronize { @sessions[request.session_id] }
    if session and session.alive?
      session.call(env)
    else
      url = request.build_url(:session_id => nil, :page_id => nil)
      Wee::RefreshResponse.new("Invalid or expired session", url).finish
    end
  else
    session = new_session()
    url = request.build_url(:session_id => session.id, :page_id => nil)
    Wee::RedirectResponse.new(url).finish
  end
end

#cleanup_sessionsObject

Garbage collect dead sessions



35
36
37
# File 'lib/wee/application.rb', line 35

def cleanup_sessions
  @mutex.synchronize { @sessions.garbage_collect }
end