Skip to content

Instantly share code, notes, and snippets.

@mr-rock
Created October 9, 2009 12:08
Show Gist options
  • Save mr-rock/205962 to your computer and use it in GitHub Desktop.
Save mr-rock/205962 to your computer and use it in GitHub Desktop.
Examples for the Cookie-based Sessions in Sinatra published by RubyLearning.com blog on Sep 30, 2009.
require 'rubygems'
require 'sinatra'
enable :sessions
get '/' do
session["value"] ||= "Hello world!"
"The cookie you've created contains the value: #{session["value"]}"
end
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
__END__
@@ layout
%html
%head
%title Introduce Yourself | 2nd Cookie-based sessions example in Sinatra
%body
= yield
@@ index
- name = session["user"].nil? ? "Stranger" : session["user"].capitalize
%h1= "Howdy #{name}!"
%p
- if session["user"].nil?
%a{:href => '/introduction'} What is your name?
- else
Man! Long time no see!!! :)
- unless session["user"].nil?
%p Well... I <a href="/bye">gotta go</a> now. See ya!
@@ introduction
%h1 Hi there!
%form{:method => 'post'}
My name is
%input{:type => 'text', :name => 'name'}
%input{:type => 'submit', :value => 'Introduce Myself'}
@@ bye
%h1 Bye!
%p See you <a href="/">next time</a>!
require 'rubygems'
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
__END__
@@ layout
%html
%head
%title Session Tests | 3rd Cookie-based sessions in Sinatra
%body
= yield
@@ index
%h1 Configuration Cookies
%p= request.cookies.to_s
%ul
- action = request.cookies.has_key?('some_options') ? "Modify" : "Create"
%li<
%a{:href => '/some_options'}= "#{action} some options"
- action = request.cookies.has_key?('other_options') ? "Modify" : "Create"
%li<
%a{:href => '/other_options'}= "#{action} other options"
@@ some_options
%h1 Some Options
%p Choose one or more options.
%form{:method => 'post'}
%ul
%li<
- value = @some_cookie.nil? ? false : @some_cookie['first_option']
%input{:type => 'checkbox', :name => 'first_option', :checked => value} First Option
%li<
- value = @some_cookie.nil? ? false : @some_cookie['second_option']
%input{:type => 'checkbox', :name => 'second_option', :checked => value} Second Option
%li<
- value = @some_cookie.nil? ? false : @some_cookie['third_option']
%input{:type => 'checkbox', :name => 'third_option', :checked => value} Third Option
%li<
- value = @some_cookie.nil? ? false : @some_cookie['fourth_option']
%input{:type => 'checkbox', :name => 'fourth_option', :checked => value} Fourth Option
%li<
- value = @some_cookie.nil? ? false : @some_cookie['fifth_option']
%input{:type => 'checkbox', :name => 'fifth_option', :checked => value} Fifth Option
- action = @some_cookie.nil? ? "Set" : "Modify"
%input{:type => 'submit', :value => "#{action} Options"}
@@ other_options
%h1 Other Options
%p Choose only one of the options available.
%form{:method => 'post'}
%ul
%li<
- value = @other_cookie.nil? ? false : @other_cookie['first_option']
%input{:type => 'radio', :name => 'options', :value => 'first_option', :checked => value} First Option
%li<
- value = @other_cookie.nil? ? false : @other_cookie['second_option']
%input{:type => 'radio', :name => 'options', :value => 'second_option', :checked => value} Second Option
%li<
- value = @other_cookie.nil? ? false : @other_cookie['third_option']
%input{:type => 'radio', :name => 'options', :value => 'third_option', :checked => value} Third Option
%li<
- value = @other_cookie.nil? ? false : @other_cookie['fourth_option']
%input{:type => 'radio', :name => 'options', :value => 'fourth_option', :checked => value} Fourth Option
%li<
- value = @other_cookie.nil? ? false : @other_cookie['fifth_option']
%input{:type => 'radio', :name => 'options', :value => 'fifth_option', :checked => value} Fifth Option
- action = @other_cookie.nil? ? "Set" : "Modify"
%input{:type => 'submit', :value => "#{action} Options"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment