Class: Ramaze::Session
- Defined in:
- lib/ramaze/current/session.rb,
lib/ramaze/current/session/hash.rb,
lib/ramaze/current/session/flash.rb
Overview
The purpose of Session is to hold key/value pairs like a Hash for a series of # request/response cycles from the same client.
The persistence is achieved by setting a cookie with the session_id to the client, which is then passed back and forth until the cookie is either deleted or expires.
Defined Under Namespace
Constant Summary collapse
- SESSION_KEY =
the key used for the cookie
'_ramaze_session_id'
- IP_COUNT =
Holds counter for IPs
::Hash.new{|h,k| h[k] = OrderedSet.new}
- IP_COUNT_LIMIT =
Limit the number of sessions one IP is allowed to hold.
1000
- COOKIE =
Holds the default cookie used for sessions
{ :path => '/' }
Instance Attribute Summary collapse
-
#dropped ⇒ Object
Flag whether or not to drop this session on the floor.
-
#flash ⇒ Object
This variable holds the current SessionFlash.
-
#session_id ⇒ Object
The unique id for the current session which is also passed on to the cookie.
Class Method Summary collapse
-
.[](sess_id) ⇒ Object
retrieve a specific session given a session id.
-
.current ⇒ Object
Shortcut for Current.session.
-
.random_key ⇒ Object
generate a random (and hopefully unique) id for the current session.
-
.startup(options = {}) ⇒ Object
called from Ramaze::startup and adds Cache.sessions if cookies are enabled.
Instance Method Summary collapse
-
#current ⇒ Object
answers with the currently active session, which is set unless it is existing already, the session itself is an instance of SessionHash.
-
#drop! ⇒ Object
don’t finish this session.
- #finish ⇒ Object
-
#initialize(sess_or_request = Current.request) ⇒ Session
constructor
Initialize a new Session, requires the original Rack::Request instance given to us from Dispatcher#setup_environment.
-
#inspect ⇒ Object
Inspect on Session.current.
-
#method_missing(*args, &block) ⇒ Object
relay all messages we don’t understand to the currently active session.
-
#sessions ⇒ Object
shortcut to Cache.sessions.
Constructor Details
#initialize(sess_or_request = Current.request) ⇒ Session
Initialize a new Session, requires the original Rack::Request instance given to us from Dispatcher#setup_environment.
sets @session_id and @session_flash
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ramaze/current/session.rb', line 96 def initialize(sess_or_request = Current.request) return unless Global.sessions if sess_or_request.respond_to?(:cookies) request = sess_or_request @session_id = request.[SESSION_KEY] || Session.random_key else request = nil @session_id = sess_or_request end unless IP_COUNT.nil? or request.nil? ip = request.ip IP_COUNT[ip] << @session_id sessions.delete(IP_COUNT[ip].shift) if IP_COUNT[ip].size > IP_COUNT_LIMIT end @flash = Session::Flash.new(self) @current = nil @dropped = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
relay all messages we don’t understand to the currently active session
120 121 122 |
# File 'lib/ramaze/current/session.rb', line 120 def method_missing(*args, &block) current.send(*args, &block) end |
Instance Attribute Details
#dropped ⇒ Object
Flag whether or not to drop this session on the floor
33 34 35 |
# File 'lib/ramaze/current/session.rb', line 33 def dropped @dropped end |
#flash ⇒ Object
This variable holds the current SessionFlash
29 30 31 |
# File 'lib/ramaze/current/session.rb', line 29 def flash @flash end |
#session_id ⇒ Object
The unique id for the current session which is also passed on to the cookie.
25 26 27 |
# File 'lib/ramaze/current/session.rb', line 25 def session_id @session_id end |
Class Method Details
.[](sess_id) ⇒ Object
retrieve a specific session given a session id
69 70 71 |
# File 'lib/ramaze/current/session.rb', line 69 def self.[](sess_id) Session.new(sess_id) end |
.current ⇒ Object
Shortcut for Current.session
56 57 58 |
# File 'lib/ramaze/current/session.rb', line 56 def self.current Current.session end |
.random_key ⇒ Object
generate a random (and hopefully unique) id for the current session.
It consists of the current time, the current request, the current PID of ruby and object_id of this instance.
All this is joined by some calls to Kernel#rand and returned as a Digest::SHA256::hexdigest
81 82 83 84 85 86 87 88 89 |
# File 'lib/ramaze/current/session.rb', line 81 def self.random_key h = [ Time.now.to_f.to_s, rand, Current.request.hash, rand, Process.pid, rand, object_id, rand ].join Digest::SHA256.hexdigest(h) end |
Instance Method Details
#current ⇒ Object
answers with the currently active session, which is set unless it is existing already, the session itself is an instance of SessionHash
127 128 129 130 |
# File 'lib/ramaze/current/session.rb', line 127 def current return @current if @current @current = ( sessions[session_id] ||= Session::Hash.new(self) ) end |
#drop! ⇒ Object
don’t finish this session
146 147 148 |
# File 'lib/ramaze/current/session.rb', line 146 def drop! self.dropped = true end |
#finish ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/ramaze/current/session.rb', line 159 def finish return unless Global.sessions return if dropped old = current.delete(:FLASH) current[:FLASH_PREVIOUS] = old if old request, response = Current.request, Current.response hash = {:value => session_id}.merge(COOKIE) response.(SESSION_KEY, hash) # set client side session cookie if val = request['session.client'] and (!val.empty? or request.["#{SESSION_KEY}-client"]) = hash.merge(:value => marshal(val)) response.("#{SESSION_KEY}-client", ) end end |
#inspect ⇒ Object
Inspect on Session.current
140 141 142 |
# File 'lib/ramaze/current/session.rb', line 140 def inspect current.inspect end |