Default

class
Superclass
Strelka::Session
Included Modules
Strelka::DataUtilities
Extended With
Configurability
Forwardable
Strelka::MethodUtilities
Strelka::Delegation

Default session class – simple in-memory, cookie-based base-bones session.

Constants

CONFIG_DEFAULTS

Default configuration

Attributes

namespace[R]

The namespace that determines which subhash of the session is exposed to the application.

Public Class Methods

anchor
configure( config=nil )

Configure the session class with the given options, which should be a Hash or an object that has a Hash-like interface.

Valid keys:

cookie_name

The name of the cookie that will be used for persisting the session key.

cookie_options

A hash of options for the session cookie; see Strelka::Cookie for available options.

# File lib/strelka/session/default.rb, line 116
def self::configure( config=nil )
        if config
                self.cookie_name = config[:cookie_name]
                self.cookie_options = config[:cookie_options]
        else
                self.cookie_name = CONFIG_DEFAULTS[:cookie_name]
                self.cookie_options = CONFIG_DEFAULTS[:cookie_options].dup
        end
end
anchor
delete_session_data( session_id )

Delete the data associated with the given session_id from memory.

# File lib/strelka/session/default.rb, line 68
def self::delete_session_data( session_id )
        self.sessions.delete( session_id )
end
anchor
get_existing_session_id( request )

Try to fetch a session ID from the specified request, returning nil if there isn't one.

# File lib/strelka/session/default.rb, line 75
def self::get_existing_session_id( request )
        cookie = request.cookies[ self.cookie_name ] or return nil

        if cookie.value =~ /^([[:xdigit:]]+)$/i
                return $1.untaint
        else
                self.log.warn "Request with a malformed session cookie: %p" % [ request ]
                return nil
        end
end
anchor
get_session_id( request=nil )

Fetch the session ID from the given request, or create a new one if the request doesn't have the necessary attributes.

# File lib/strelka/session/default.rb, line 89
def self::get_session_id( request=nil )
        id = self.get_existing_session_id( request ) if request
        return id || SecureRandom.hex
end
anchor
has_session_for?( request )

Return true if the given request has a session token which corresponds to an existing session key.

# File lib/strelka/session/default.rb, line 97
def self::has_session_for?( request )
        self.log.debug "Checking request (%s/%d) for session." % [ request.sender_id, request.conn_id ]
        id = self.get_existing_session_id( request ) or return false
        self.log.debug "  got a session ID: %p" % [ id ]
        return @sessions.key?( id )
end
anchor
load_session_data( session_id )

Load a session instance from storage using the given session_id and return it. Returns nil if no session could be loaded.

# File lib/strelka/session/default.rb, line 56
def self::load_session_data( session_id )
        return Strelka::DataUtilities.deep_copy( self.sessions[session_id] )
end
anchor
new( session_id=nil, initial_values={} )

Create a new session store using the given hash for initial values.

# File lib/strelka/session/default.rb, line 132
def initialize( session_id=nil, initial_values={} )
        @hash       = Hash.new {|h,k| h[k] = {} }
        @hash.merge!( initial_values )

        @namespace  = nil

        super
end
anchor
save_session_data( session_id, data )

Save the given data to memory associated with the given session_id.

# File lib/strelka/session/default.rb, line 62
def self::save_session_data( session_id, data )
        self.sessions[ session_id ] = Strelka::DataUtilities.deep_copy( data )
end

Public Instance Methods

anchor anchor anchor
defaultsession()

Configurability API – use the 'defaultsession' section of the config

# File lib/strelka/session/default.rb, line 24
config_key :defaultsession
anchor
destroy( response )

Delete the session from storage and expire the session cookie from the given response.

# File lib/strelka/session/default.rb, line 192
def destroy( response )
        self.log.debug "Destroying session %s" % [ self.session_id ]
        self.class.delete_session_data( self.session_id )

        self.log.debug "Adding expired session cookie to the response"
        session_cookie = Strelka::Cookie.new(
                self.class.cookie_name,
                self.session_id,
                self.class.cookie_options || {}
        )
        session_cookie.expire!

        response.cookies << session_cookie
end
anchor
namespace=( namespace )

Set the current namespace for session values to namespace. Setting namespace to nil exposes the toplevel namespace (keys are named namespaces)

# File lib/strelka/session/default.rb, line 170
def namespace=( namespace )
        @namespace = namespace.nil? ? nil : namespace.to_sym
end
anchor
namespaced_hash()

Delegate Hash methods to whichever Hash is the current namespace

# File lib/strelka/session/default.rb, line 164
def_method_delegators :namespaced_hash, :[], :[]=, :delete, :key?
anchor
save( response )

Save the session to storage and add the session cookie to the given response.

# File lib/strelka/session/default.rb, line 176
def save( response )
        self.log.debug "Saving session %s" % [ self.session_id ]
        self.class.save_session_data( self.session_id, @hash )
        self.log.debug "Adding session cookie to the response."

        session_cookie = Strelka::Cookie.new(
                self.class.cookie_name,
                self.session_id,
                self.class.cookie_options || {}
        )

        response.cookies << session_cookie
end
anchor
sessions()

In-memory session store

# File lib/strelka/session/default.rb, line 42
singleton_attr_reader :sessions

Protected Instance Methods

anchor
make_getter( key )

Create a Proc that will act as a getter for the given key

# File lib/strelka/session/default.rb, line 254
def make_getter( key )
        return Proc.new { self.namespaced_hash[key.to_sym] }
end
anchor
make_setter( key )

Create a Proc that will act as a setter for the given key

# File lib/strelka/session/default.rb, line 248
def make_setter( key )
        return Proc.new {|new_value| self.namespaced_hash[ key.to_sym ] = new_value }
end
anchor
method_missing( sym, *args )

Proxy method: handle getting/setting headers via methods instead of the index operator.

# File lib/strelka/session/default.rb, line 228
def method_missing( sym, *args )
        # work magic
        return super unless sym.to_s =~ /^([a-z]\w+)(=)?$/

        # If it's an assignment, the (=)? will have matched
        key, assignment = $1, $2

        method_body = nil
        if assignment
                method_body = self.make_setter( key )
        else
                method_body = self.make_getter( key )
        end

        self.class.send( :define_method, sym, &method_body )
        return self.method( sym ).call( *args )
end