Class: Ramaze::Session

Inherits:
Object show all
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

Classes: Flash, Hash

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
{ :path => '/' }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.cookies[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

#droppedObject

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

#flashObject

This variable holds the current SessionFlash



29
30
31
# File 'lib/ramaze/current/session.rb', line 29

def flash
  @flash
end

#session_idObject

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

.currentObject

Shortcut for Current.session



56
57
58
# File 'lib/ramaze/current/session.rb', line 56

def self.current
  Current.session
end

.random_keyObject

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

.startup(options = {}) ⇒ Object

called from Ramaze::startup and adds Cache.sessions if cookies are enabled



63
64
65
# File 'lib/ramaze/current/session.rb', line 63

def self.startup(options = {})
  Cache.add(:sessions) if Global.sessions
end

Instance Method Details

#currentObject

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

#finishObject

at the end of a request delete the current and assign it to current

this is needed so flash can iterate over requests and always just keep the current and previous key/value pairs.

finalizes the session and assigns the key to the response via set_cookie.



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.set_cookie(SESSION_KEY, hash)

  # set client side session cookie
  if val = request['session.client'] and
     (!val.empty? or request.cookies["#{SESSION_KEY}-client"])
    cookie = hash.merge(:value => marshal(val))
    response.set_cookie("#{SESSION_KEY}-client", cookie)
  end
end

#inspectObject

Inspect on Session.current



140
141
142
# File 'lib/ramaze/current/session.rb', line 140

def inspect
  current.inspect
end

#sessionsObject

shortcut to Cache.sessions



134
135
136
# File 'lib/ramaze/current/session.rb', line 134

def sessions
  Cache.sessions
end