picnic 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,123 @@
1
+ # == About camping/session.rb
2
+ #
3
+ # This file contains two modules which supply basic sessioning to your Camping app.
4
+ # Again, we're dealing with a pretty little bit of code: approx. 60 lines.
5
+ #
6
+ # * Camping::Models::Session is a module which adds a single <tt>sessions</tt> table
7
+ # to your database.
8
+ # * Camping::Session is a module which you will mix into your application (or into
9
+ # specific controllers which require sessions) to supply a <tt>@state</tt> variable
10
+ # you can use in controllers and views.
11
+ #
12
+ # For a basic tutorial, see the *Getting Started* section of the Camping::Session module.
13
+ require 'camping'
14
+
15
+ module Camping::Models
16
+ # A database table for storing Camping sessions. Contains a unique 32-character hashid, a
17
+ # creation timestamp, and a column of serialized data called <tt>ivars</tt>.
18
+ class Session < Base
19
+ serialize :ivars
20
+ def []=(k, v) # :nodoc:
21
+ self.ivars[k] = v
22
+ end
23
+ def [](k) # :nodoc:
24
+ self.ivars[k] rescue nil
25
+ end
26
+
27
+ RAND_CHARS = [*'A'..'Z'] + [*'0'..'9'] + [*'a'..'z']
28
+
29
+ # Generates a new session ID and creates a row for the new session in the database.
30
+ def self.generate cookies
31
+ rand_max = RAND_CHARS.size
32
+ sid = (0...32).inject("") { |ret,_| ret << RAND_CHARS[rand(rand_max)] }
33
+ sess = Session.create :hashid => sid, :ivars => Camping::H[]
34
+ cookies.camping_sid = sess.hashid
35
+ sess
36
+ end
37
+
38
+ # Gets the existing session based on the <tt>camping_sid</tt> available in cookies.
39
+ # If none is found, generates a new session.
40
+ def self.persist cookies
41
+ if cookies.camping_sid
42
+ session = Camping::Models::Session.find_by_hashid cookies.camping_sid
43
+ end
44
+ unless session
45
+ session = Camping::Models::Session.generate cookies
46
+ end
47
+ session
48
+ end
49
+
50
+ # Builds the session table in the database. To be used in your application's
51
+ # <tt>create</tt> method.
52
+ #
53
+ # Like so:
54
+ #
55
+ # def Blog.create
56
+ # Camping::Models::Session.create_schema
57
+ # unless Blog::Models::Post.table_exists?
58
+ # ActiveRecord::Schema.define(&Blog::Models.schema)
59
+ # end
60
+ # end
61
+ #
62
+ def self.create_schema
63
+ unless table_exists?
64
+ ActiveRecord::Schema.define do
65
+ create_table :sessions, :force => true do |t|
66
+ #t.column :id, :integer, :null => false
67
+ t.column :hashid, :string, :limit => 32
68
+ t.column :created_at, :datetime
69
+ t.column :ivars, :text
70
+ end
71
+ end
72
+ reset_column_information
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ module Camping
79
+ # The Camping::Session module is designed to be mixed into your application or into specific
80
+ # controllers which require sessions. This module defines a <tt>service</tt> method which
81
+ # intercepts all requests handed to those controllers.
82
+ #
83
+ # == Getting Started
84
+ #
85
+ # To get sessions working for your application:
86
+ #
87
+ # 1. <tt>require 'camping/session'</tt>
88
+ # 2. Mixin the module: <tt>module YourApp; include Camping::Session end</tt>
89
+ # 3. In your application's <tt>create</tt> method, add a call to <tt>Camping::Models::Session.create_schema</tt>
90
+ # 4. Throughout your application, use the <tt>@state</tt> var like a hash to store your application's data.
91
+ #
92
+ # If you are unfamiliar with the <tt>create</tt> method, see
93
+ # http://code.whytheluckystiff.net/camping/wiki/GiveUsTheCreateMethod.
94
+ #
95
+ # == A Few Notes
96
+ #
97
+ # * The session ID is stored in a cookie. Look in <tt>@cookies.camping_sid</tt>.
98
+ # * The session data is stored in the <tt>sessions</tt> table in your database.
99
+ # * All mounted Camping apps using this class will use the same database table.
100
+ # * However, your application's data is stored in its own hash.
101
+ # * Session data is only saved if it has changed.
102
+ module Session
103
+ # This <tt>service</tt> method, when mixed into controllers, intercepts requests
104
+ # and wraps them with code to start and close the session. If a session isn't found
105
+ # in the database it is created. The <tt>@state</tt> variable is set and if it changes,
106
+ # it is saved back into the database.
107
+ def service(*a)
108
+ session = Camping::Models::Session.persist @cookies
109
+ app = self.class.name.gsub(/^(\w+)::.+$/, '\1')
110
+ @state = (session[app] ||= Camping::H[])
111
+ hash_before = Marshal.dump(@state).hash
112
+ s = super(*a)
113
+ if session
114
+ hash_after = Marshal.dump(@state).hash
115
+ unless hash_before == hash_after
116
+ session[app] = @state
117
+ session.save
118
+ end
119
+ end
120
+ s
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,65 @@
1
+ # == About camping/webrick.rb
2
+ #
3
+ # For many who have Ruby installed, Camping and WEBrick is a great option.
4
+ # It's definitely the easiest configuration, however some performance is sacrificed.
5
+ # For better speed, check out Mongrel at http://mongrel.rubyforge.org/, which comes
6
+ # with Camping hooks and is supported by the Camping Tool.
7
+ require 'camping'
8
+ require 'webrick/httpservlet/abstract.rb'
9
+
10
+ module WEBrick
11
+ # WEBrick::CampingHandler is a very simple handle for hosting Camping apps in
12
+ # a WEBrick server. It's used much like any other WEBrick handler.
13
+ #
14
+ # == Mounting a Camping App
15
+ #
16
+ # Assuming Camping.goes(:Blog), the Blog application can be mounted alongside
17
+ # other WEBrick mounts.
18
+ #
19
+ # s = WEBrick::HTTPServer.new(:BindAddress => host, :Port => port)
20
+ # s.mount "/blog", WEBrick::CampingHandler, Blog
21
+ # s.mount_proc("/") { ... }
22
+ #
23
+ # == How Does it Compare?
24
+ #
25
+ # Compared to other handlers, WEBrick is well-equipped in terms of features.
26
+ #
27
+ # * The <tt>X-Sendfile</tt> header is supported, along with etags and
28
+ # modification time headers for the file served. Since this handler
29
+ # is a subclass of WEBrick::HTTPServlet::DefaultFileHandler, all of its
30
+ # logic is used.
31
+ # * IO is streaming up and down. When you upload a file, it is streamed to
32
+ # the server's filesystem. When you download a file, it is streamed to
33
+ # your browser.
34
+ #
35
+ # While WEBrick is a bit slower than Mongrel and FastCGI options, it's
36
+ # a decent choice, for sure!
37
+ class CampingHandler < WEBrick::HTTPServlet::DefaultFileHandler
38
+ # Creates a CampingHandler, which answers for the application within +klass+.
39
+ def initialize(server, klass)
40
+ super(server, klass)
41
+ @klass = klass
42
+ end
43
+ # Handler for WEBrick requests (also aliased as do_POST).
44
+ def service(req, resp)
45
+ controller = @klass.run((req.body and StringIO.new(req.body)), req.meta_vars)
46
+ resp.status = controller.status
47
+ @local_path = nil
48
+ controller.headers.each do |k, v|
49
+ if k =~ /^X-SENDFILE$/i
50
+ @local_path = v
51
+ else
52
+ [*v].each do |vi|
53
+ resp[k] = vi
54
+ end
55
+ end
56
+ end
57
+
58
+ if @local_path
59
+ do_GET(req, resp)
60
+ else
61
+ resp.body = controller.body.to_s
62
+ end
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: picnic
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.0
7
+ date: 2007-12-21 00:00:00 -05:00
8
+ summary: Camping for sissies
9
+ require_paths:
10
+ - lib
11
+ email: matt@roughest.net
12
+ homepage: http://picnic.rubyforge.org
13
+ rubyforge_project: picnic
14
+ description: Camping for sissies
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Matt Zukowski
31
+ files:
32
+ - CHANGELOG.txt
33
+ - LICENSE.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ - Rakefile
37
+ - lib/picnic.rb
38
+ - lib/picnic/authentication.rb
39
+ - lib/picnic/cli.rb
40
+ - lib/picnic/conf.rb
41
+ - lib/picnic/controllers.rb
42
+ - lib/picnic/postambles.rb
43
+ - lib/picnic/service_control.rb
44
+ - lib/picnic/utils.rb
45
+ - lib/picnic/version.rb
46
+ - setup.rb
47
+ - test/picnic_test.rb
48
+ - test/test_helper.rb
49
+ - vendor/camping-1.5.180/CHANGELOG
50
+ - vendor/camping-1.5.180/COPYING
51
+ - vendor/camping-1.5.180/README
52
+ - vendor/camping-1.5.180/Rakefile
53
+ - vendor/camping-1.5.180/lib/camping-unabridged.rb
54
+ - vendor/camping-1.5.180/lib/camping.rb
55
+ - vendor/camping-1.5.180/lib/camping/db.rb
56
+ - vendor/camping-1.5.180/lib/camping/fastcgi.rb
57
+ - vendor/camping-1.5.180/lib/camping/reloader.rb
58
+ - vendor/camping-1.5.180/lib/camping/session.rb
59
+ - vendor/camping-1.5.180/lib/camping/webrick.rb
60
+ test_files:
61
+ - test/picnic_test.rb
62
+ rdoc_options:
63
+ - --main
64
+ - README.txt
65
+ extra_rdoc_files:
66
+ - CHANGELOG.txt
67
+ - LICENSE.txt
68
+ - Manifest.txt
69
+ - README.txt
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ requirements: []
75
+
76
+ dependencies: []
77
+