sinatra-session 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,119 @@
1
+ Sinatra::Session
2
+ ================
3
+
4
+ Sinatra::Session is a small extension for Sinatra apps that provides several
5
+ convenience settings and methods when working with cookie-based sessions.
6
+
7
+ Settings
8
+ --------
9
+
10
+ session_fail -> A URL in your app that the client will be redirected to
11
+ when `session!` is called and the session is not valid.
12
+ Defaults to '/login'
13
+ session_name -> The name to use for the session cookie
14
+ session_path -> The session cookie path
15
+ session_domain -> The session cookie domain
16
+ session_expire -> The number of seconds from now the cookie will be valid
17
+ session_secret -> A secret key that is used to check the integrity of
18
+ session data
19
+
20
+ Helper Methods
21
+ --------------
22
+
23
+ session_start! -> Starts the session
24
+ session_end!(destroy=true) -> Ends the session. If `destroy` is false then
25
+ session data will be preserved, even though
26
+ future calls to `session?` will return false
27
+ session? -> Returns true if the session is valid
28
+ session! -> Redirects the client to the URL specified in
29
+ the `session_fail` option unless the session is
30
+ valid
31
+
32
+ Installation
33
+ ------------
34
+
35
+ Via Rubygems:
36
+
37
+ $ sudo gem install sinatra-session
38
+
39
+ From a local copy:
40
+
41
+ $ git clone git://github.com/mjijackson/sinatra-session.git
42
+ $ cd sinatra-session
43
+ $ rake package && sudo rake install
44
+
45
+ Usage
46
+ -----
47
+
48
+ As with all Sinatra extensions, Sinatra::Session may be used in both classic
49
+ and modular-style apps. First, classic.
50
+
51
+ set :session_fail, '/login'
52
+ set :session_secret, 'So0perSeKr3t!'
53
+
54
+ get '/' do
55
+ session!
56
+ 'Hello, ' + session[:name] + '! Click <a href="/logout">here</a> to logout.'
57
+ end
58
+
59
+ get '/login' do
60
+ if session?
61
+ redirect '/'
62
+ else
63
+ '<form method="POST" action="/login">' +
64
+ 'Please enter your name: <input type="text" name="name">' +
65
+ '<input type="submit" value="Submit">' +
66
+ '</form>'
67
+ end
68
+ end
69
+
70
+ post '/login' do
71
+ if params[:name]
72
+ session_start!
73
+ session[:name] = params[:name]
74
+ redirect '/'
75
+ else
76
+ redirect '/login'
77
+ end
78
+ end
79
+
80
+ get '/logout' do
81
+ session_end!
82
+ redirect '/'
83
+ end
84
+
85
+ Modular apps use very similar code but must also mixin Sinatra::Session via
86
+ Sinatra#register, like so:
87
+
88
+ require 'sinatra/base'
89
+ require 'sinatra/session'
90
+
91
+ class MyApp < Sinatra::Base
92
+ register Sinatra::Session
93
+
94
+ # insert settings and routes here, same as in
95
+ # classic example above
96
+ end
97
+
98
+ License
99
+ -------
100
+
101
+ Copyright 2010 Michael Jackson
102
+
103
+ Permission is hereby granted, free of charge, to any person obtaining a copy
104
+ of this software and associated documentation files (the "Software"), to deal
105
+ in the Software without restriction, including without limitation the rights
106
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
107
+ copies of the Software, and to permit persons to whom the Software is
108
+ furnished to do so, subject to the following conditions:
109
+
110
+ The above copyright notice and this permission notice shall be included in
111
+ all copies or substantial portions of the Software.
112
+
113
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
114
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
115
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
116
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
117
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
118
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
119
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ require 'rake/clean'
2
+
3
+ task :default => :package
4
+
5
+ CLEAN.include %w< dist >
6
+
7
+ # PACKAGING & INSTALLATION ####################################################
8
+
9
+ if defined?(Gem)
10
+ $spec = eval("#{File.read('sinatra-session.gemspec')}")
11
+
12
+ directory 'dist'
13
+
14
+ def package(ext='')
15
+ "dist/#{$spec.name}-#{$spec.version}" + ext
16
+ end
17
+
18
+ file package('.gem') => %w< dist > + $spec.files do |f|
19
+ sh "gem build sinatra-session.gemspec"
20
+ mv File.basename(f.name), f.name
21
+ end
22
+
23
+ file package('.tar.gz') => %w< dist > + $spec.files do |f|
24
+ sh "git archive --format=tar HEAD | gzip > #{f.name}"
25
+ end
26
+
27
+ desc "Build packages"
28
+ task :package => %w< .gem .tar.gz >.map {|e| package(e) }
29
+
30
+ desc "Build and install as local gem"
31
+ task :install => package('.gem') do |t|
32
+ sh "gem install #{package('.gem')}"
33
+ end
34
+
35
+ desc "Upload gem to rubygems.org"
36
+ task :release => package('.gem') do |t|
37
+ sh "gem push #{package('.gem')}"
38
+ end
39
+ end
@@ -0,0 +1,72 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Session
5
+
6
+ # Request-level helper methods for Sinatra routes.
7
+ module Helpers
8
+ # After this method is called, calls to #sesson? will return +true+.
9
+ def session_start!
10
+ session['sinatra.session'] = true
11
+ end
12
+
13
+ # After this method is called, calls to #session? will return +false+. If
14
+ # you want to keep the old session data around in the cookie for some
15
+ # reason, set +destroy+ to +false+.
16
+ def session_end!(destroy=true)
17
+ if destroy
18
+ session.clear
19
+ else
20
+ session['sinatra.session'] = false
21
+ end
22
+ end
23
+
24
+ # Returns +true+ if the current session is valid, false otherwise.
25
+ def session?
26
+ !! session['sinatra.session']
27
+ end
28
+
29
+ # Redirects the client to the URL given in the +session_fail+ setting
30
+ # if #session? returns +false+.
31
+ def session!
32
+ redirect(settings.session_fail) unless session?
33
+ end
34
+ end
35
+
36
+ # A wrapper for Rack::Session::Cookie middleware that allows an options
37
+ # hash to be returned from the block given to the `use` statement instead
38
+ # of being provided up front.
39
+ module Cookie
40
+ def self.new(app, options={})
41
+ options.merge!(yield) if block_given?
42
+ Rack::Session::Cookie.new(app, options)
43
+ end
44
+ end
45
+
46
+ def self.registered(app)
47
+ app.helpers Session::Helpers
48
+
49
+ # This should be set to the redirect URL the client will be sent to if
50
+ # the session is not valid.
51
+ app.set :session_fail, '/login'
52
+
53
+ # Parameters for the session cookie.
54
+ app.set :session_name, 'sinatra.session'
55
+ app.set :session_path, '/'
56
+ app.set :session_domain, nil
57
+ app.set :session_expire, nil
58
+ app.set :session_secret, nil
59
+
60
+ app.use(Session::Cookie) do
61
+ { :key => app.session_name,
62
+ :path => app.session_path,
63
+ :domain => app.session_domain,
64
+ :expire_after => app.session_expire,
65
+ :secret => app.session_secret
66
+ }
67
+ end
68
+ end
69
+ end
70
+
71
+ register Session
72
+ end
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'sinatra-session'
3
+ s.version = '0.1'
4
+ s.date = '2010-04-21'
5
+
6
+ s.summary = 'Simple session support for Sinatra apps'
7
+ s.description = 'Simple session support for Sinatra apps'
8
+
9
+ s.author = 'Michael Jackson'
10
+ s.email = 'mjijackson@gmail.com'
11
+
12
+ s.require_paths = %w< lib >
13
+
14
+ s.files = Dir['lib/**/*.rb'] +
15
+ %w< sinatra-session.gemspec Rakefile README >
16
+
17
+ s.add_dependency('sinatra', '>= 1.0')
18
+ s.add_development_dependency('rake')
19
+
20
+ s.has_rdoc = true
21
+ s.rdoc_options = %w< --line-numbers --inline-source --title Sinatra::Session --main Sinatra::Session >
22
+ s.extra_rdoc_files = %w< README >
23
+
24
+ s.homepage = 'http://github.com/mjijackson/sinatra-session'
25
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-session
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - Michael Jackson
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-04-21 00:00:00 -06:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: sinatra
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ segments:
27
+ - 1
28
+ - 0
29
+ version: "1.0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: rake
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
44
+ description: Simple session support for Sinatra apps
45
+ email: mjijackson@gmail.com
46
+ executables: []
47
+
48
+ extensions: []
49
+
50
+ extra_rdoc_files:
51
+ - README
52
+ files:
53
+ - lib/sinatra/session.rb
54
+ - sinatra-session.gemspec
55
+ - Rakefile
56
+ - README
57
+ has_rdoc: true
58
+ homepage: http://github.com/mjijackson/sinatra-session
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --line-numbers
64
+ - --inline-source
65
+ - --title
66
+ - Sinatra::Session
67
+ - --main
68
+ - Sinatra::Session
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.6
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Simple session support for Sinatra apps
92
+ test_files: []
93
+