[~/sinatra/sinatra-session/sinatra-cookie-based-sessions(master)]$ pwd -P /Users/casiano/local/src/ruby/sinatra/sinatra-session/sinatra-cookie-based-sessions [~/sinatra/sinatra-session/sinatra-cookie-based-sessions(master)]$ ls first_example.rb second_example.rb third_example.rb
[~/sinatra/sinatra-cookies(master)]$ git remote -v old https://gist.github.com/205962.git (fetch) old https://gist.github.com/205962.git (push) origin https://gist.github.com/3efe043301bb2c785bff.git (fetch) origin https://gist.github.com/3efe043301bb2c785bff.git (push)
enable :sessions get '/' do "value = " << session[:value].inspect end get '/:value' do session[:value] = params[:value] end
enable :sessions
actually stores all data in a cookie
enable :sessions
, but instead pull in your middleware of choice as you would any other middleware:
use Rack::Session::Pool, :expire_after => 2592000 get '/' do "value = " << session[:value].inspect end get '/:value' do session[:value] = params[:value] end
set :session_secret, 'super secret'If you want to configure it further, you may also store a hash with options in the sessions setting:
set :sessions, :domain => 'foo.com'
session.clear
to destroy the session.
get '/login' do session[:username] = params[:username] "logged in as #{session[:username]}" end get '/logout' do old_user = session[:username] session.clear "logged out #{old_user}" end
Wdy, DD-Mon-YYYY HH:MM:SS GMT
format
path
and/or a domain
defined as a string
Cookie: key0=value0; ...; keyX=valueX; expires=Wed, 23-Sep-2009 23:59:59 GMT; path=/; domain=.yoursite.com
expiration
date is defined, your cookie will be persistent as it will reoccur in different sessions until the set expiration
date has been reached
expiration
date has not been defined in the cookie, it will occur until the end of your current session or when you close your web browser
path
and/or the domain
attributes have been defined in your cookie, then the web server limits the scope of the cookie to that specific domain
, sub-domain
or path
require 'rubygems' require 'sinatra' require 'haml' enable :sessions get '/' do session["user"] ||= nil haml :index end get '/introduction' do haml :introduction end post '/introduction' do session["user"] = params[:name] redirect '/' end get '/bye' do session["user"] = nil haml :bye end
request
and response
singletons provided by Sinatra
require 'sinatra' require 'haml' get '/' do @@expiration_date = Time.now + (60 * 2) \ unless request.cookies.key?('some_options') && request.cookies.key?('other_options') haml :index end get '/some_options' do @some_cookie = request.cookies["some_options"] haml :some_options end post '/some_options' do response.set_cookie('some_options', :value => cookie_values(params), :expires => @@expiration_date) redirect '/' end get '/other_options' do @other_cookie = request.cookies["other_options"] haml :other_options end post '/other_options' do response.set_cookie('other_options', :value => cookie_values(params),:expires => @@expiration_date) redirect '/' end helpers do def cookie_values(parameters) values = {} parameters.each do |key, value| case key when 'options' values[value] = true else values[key] = true end end values end end
set :session_secret, 'super secret'When it's not set sinatra generates random one on application start and shotgun restarts application before every request.