Module: Strelka::App::Sessions
- Extended by:
- Configurability, Loggability, MethodUtilities, Plugin
- Includes:
- Constants
- Defined in:
- lib/strelka/app/sessions.rb
Overview
Sessions for Strelka apps
This plugin adds a persistant storage mechanism to Strelka applications that can be used to store data about a user's session.
Examples
class HelloWorld < Strelka::App
# Use the default session store and id-generator
plugins :routing, :sessions
session_namespace :foo # defaults to the app's ID
get '' do
finish_with HTTP::FORBIDDEN, "no session!" unless req.session?
username = req.session[:username]
end
end # class HelloWorld
Components
This plugin is split up into four parts:
[Strelka::Session] The abstract base class for the session object; provides the interface for getting and setting session data, writing the resulting data structure to permanent storage (if necessary), and generating the token that associates the request with the session. [Strelka::HTTPRequest::Session] A mixin module that's added to HTTPRequest when the :sessions plugin is installed. Provides the API on HTTPRequest for fetching and interacting with the Session object. [Strelka::HTTPResponse::Session] A mixin module that's added to HTTPResponse when the :sessions plugin is installed. Provides the API on HTTPResponse for fetching and interacting with the Session object. [Strelka::App::Sessions] This module; stitches the whole system together in your application.
Configuration
To specify which Session class to use with your application, add a ':sessions' section with at least the 'session_class' key to your config.yml:
# Use the default session class, but change the name of the cookie
# it uses
sessions:
session_class: default
defaultsession:
cookie_name: acme-session
# Use the database-backed session type and point it
# at a database
sessions:
session_class: db
dbsession:
connect: "postgres://pg.example.com/db01"
table_name: sessions
The session_class value will be used to look up the class (see
Strelka::Session for more information about how this works), and the
options section is passed to the session class's ::configure method
(if it has one).
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- CONFIG_DEFAULTS =
Default options to pass to the session object
{ session_class: 'default', }
Instance Attribute Summary
Attributes included from Plugin
Class Method Summary collapse
-
.configure(config = nil) ⇒ Object
Configurability API -- set up session type and options with values from the
config. -
.included(object) ⇒ Object
Extension callback -- extend the HTTPRequest classes with Session support when this plugin is loaded.
-
.session_class=(newclass) ⇒ Object
Get the configured session class (Strelka::Session subclass).
Instance Method Summary collapse
-
#fixup_request(request) ⇒ Object
Set the session namespace on the HTTPRequest before running the application.
-
#fixup_response(response) ⇒ Object
Save the session after the app and plugins are done with the HTTPResponse.
-
#handle_request ⇒ Object
This is just here for logging.
-
#session_class ⇒ Object
What session class to use (String, Symbol, or Class); passed to Strelka::Session.create.
Methods included from MethodUtilities
attr_predicate, attr_predicate_accessor, singleton_attr_accessor, singleton_attr_reader, singleton_attr_writer, singleton_method_alias, singleton_predicate_accessor, singleton_predicate_reader
Methods included from Plugin
extended, plugin_name, run_inside, run_outside
Class Method Details
.configure(config = nil) ⇒ Object
Configurability API -- set up session type and options with values from
the config.
152 153 154 155 156 157 158 159 160 |
# File 'lib/strelka/app/sessions.rb', line 152 def self::configure( config=nil ) # Figure out which session class is going to be used, or choose a default one if config self.session_class = config[:session_class] else self.session_class = CONFIG_DEFAULTS[:session_class] end end |
.included(object) ⇒ Object
Extension callback -- extend the HTTPRequest classes with Session support when this plugin is loaded.
165 166 167 168 169 170 171 |
# File 'lib/strelka/app/sessions.rb', line 165 def self::included( object ) self.log.debug "Extending Request with Session mixin" Strelka::HTTPRequest.class_eval { include Strelka::HTTPRequest::Session } self.log.debug "Extending Response with Session mixin" Strelka::HTTPResponse.class_eval { include Strelka::HTTPResponse::Session } super end |
Instance Method Details
#fixup_request(request) ⇒ Object
Set the session namespace on the HTTPRequest before running the application.
175 176 177 178 |
# File 'lib/strelka/app/sessions.rb', line 175 def fixup_request( request ) request.session_namespace = self.class.session_namespace return super end |
#fixup_response(response) ⇒ Object
Save the session after the app and plugins are done with the HTTPResponse.
182 183 184 185 186 |
# File 'lib/strelka/app/sessions.rb', line 182 def fixup_response( response ) self.log.debug "Saving the session in the response." response.save_session return super end |
#handle_request ⇒ Object
This is just here for logging.
190 191 192 193 |
# File 'lib/strelka/app/sessions.rb', line 190 def handle_request( * ) # :nodoc: self.log.debug "[:sessions] Adding sessions to the transaction." super end |
#session_class ⇒ Object
What session class to use (String, Symbol, or Class); passed to Strelka::Session.create.
141 |
# File 'lib/strelka/app/sessions.rb', line 141 singleton_attr_reader :session_class |