mosquito 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/public/bare.rb ADDED
@@ -0,0 +1,71 @@
1
+ #!/usr/local/bin/ruby -rubygems
2
+ require 'camping'
3
+
4
+ Camping.goes :Bare
5
+
6
+ module Bare::Controllers
7
+ class Index < R '/'
8
+ def get
9
+ render :index
10
+ end
11
+ end
12
+
13
+ # class SendAFile < R '/file'
14
+ # def get
15
+ # # Send this file back
16
+ #
17
+ # end
18
+ # end
19
+
20
+ class ThisOneWillError < R '/error'
21
+ def get
22
+ raise "An error for testing only!"
23
+ end
24
+ end
25
+
26
+ class ThisOneWillError404 < R '/error404'
27
+ def get
28
+ @status = 404
29
+ end
30
+ end
31
+
32
+ class ThisOneWillRedirect < R '/redirect'
33
+ def get
34
+ redirect R(Page, 'faq')
35
+ end
36
+ end
37
+
38
+ class Page < R '/(\w+)'
39
+ def get(page_name)
40
+ render page_name
41
+ end
42
+ end
43
+ end
44
+
45
+ module Bare::Views
46
+ def layout
47
+ html do
48
+ title { 'My Bare' }
49
+ body { self << yield }
50
+ end
51
+ end
52
+ def index
53
+ p 'Hi my name is Charles.'
54
+ p 'Here are some links:'
55
+ ul do
56
+ li { a 'Google', :href => 'http://google.com' }
57
+ li { a 'A sample page', :href => '/sample' }
58
+ end
59
+ end
60
+ def sample
61
+ p 'A sample page'
62
+ end
63
+ end
64
+
65
+ if __FILE__ == $0
66
+ require 'mongrel/camping'
67
+
68
+ server = Mongrel::Camping::start("0.0.0.0",3002,"/homepage",Bare)
69
+ puts "** Bare example is running at http://localhost:3002/homepage"
70
+ server.run.join
71
+ end
data/public/blog.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'rubygems'
4
- require_gem 'camping', '>=1.4'
4
+ gem 'camping', '>=1.4'
5
5
  require 'camping'
6
6
  require 'camping/session'
7
7
 
@@ -11,10 +11,6 @@ require File.dirname(__FILE__) + '/blog/models'
11
11
  require File.dirname(__FILE__) + '/blog/views'
12
12
  require File.dirname(__FILE__) + '/blog/controllers'
13
13
 
14
- module Blog
15
- include Camping::Session
16
- end
17
-
18
14
  Blog::Models.schema do
19
15
  create_table :blog_posts, :force => true do |t|
20
16
  t.column :id, :integer, :null => false
@@ -37,7 +33,6 @@ Blog::Models.schema do
37
33
  end
38
34
 
39
35
  def Blog.create
40
- Camping::Models::Session.create_schema
41
36
  unless Blog::Models::Post.table_exists?
42
37
  ActiveRecord::Schema.define(&Blog::Models.schema)
43
38
  end
@@ -1,116 +1,163 @@
1
1
 
2
2
  module Blog::Controllers
3
- class Index < R '/'
4
- def get
5
- @posts = Post.find :all
6
- render :index
7
- end
8
- end
9
-
10
- class Add
11
- def get
12
- unless @state.user_id.blank?
13
- @user = User.find @state.user_id
14
- @post = Post.new
15
- end
16
- render :add
17
- end
18
- def post
19
- post = Post.create :title => input.post_title,
20
- :body => input.post_body,
21
- :user_id => @state.user_id
22
- redirect View, post
23
- end
24
- end
25
-
26
- class Info < R '/info/(\d+)', '/info/(\w+)/(\d+)', '/info', '/info/(\d+)/(\d+)/(\d+)/([\w-]+)'
27
- def get(*args)
28
- div do
29
- code args.inspect; br; br
30
- code ENV.inspect; br
31
- code "Link: #{R(Info, 1, 2)}"
32
- end
33
- end
34
- end
35
-
36
- class View < R '/view/(\d+)'
37
- def get post_id
38
- @post = Post.find post_id
39
- @comments = Models::Comment.find_all_by_post_id post_id
40
- render :view
41
- end
42
- end
43
-
44
- class Edit < R '/edit/(\d+)', '/edit'
45
- def get post_id
46
- unless @state.user_id.blank?
47
- @user = User.find @state.user_id
48
- end
49
- @post = Post.find post_id
50
- render :edit
51
- end
52
-
53
- def post
54
- @post = Post.find input.post_id
55
- @post.update_attributes :title => input.post_title, :body => input.post_body
56
- redirect View, @post
57
- end
58
- end
59
-
60
- class Comment
61
- def post
62
- Models::Comment.create( :username => input.post_username,
63
- :body => input.post_body,
64
- :post_id => input.post_id)
65
- redirect View, input.post_id
66
- end
67
- end
68
-
69
- class Login
70
- def post
71
- @user = User.find :first, :conditions => ['username = ? AND password = ?', input.username, input.password]
72
-
73
- if @user
74
- @login = 'login success !'
75
- @state.user_id = @user.id
76
- else
77
- @login = 'wrong user name or password'
78
- end
79
- render :login
80
- end
81
- end
82
-
83
- class Cookies < R '/cookies'
84
- def get
85
- @cookies.awesome_cookie = 'camping for good'
86
- @state.awesome_data = 'camping for good'
87
- @posts = Post.find(:all)
88
- render :index
3
+ class Index < R '/'
4
+ def get
5
+ @posts = Post.find :all
6
+ render :index
7
+ end
8
+ end
9
+
10
+ class Add
11
+ def get
12
+ unless @state.user_id.blank?
13
+ @user = User.find @state.user_id
14
+ @post = Post.new
15
+ end
16
+ render :add
17
+ end
18
+ def post
19
+ post = Post.create({
20
+ :title => input.post_title,
21
+ :body => input.post_body,
22
+ :user_id => @state.user_id
23
+ })
24
+ redirect View, post
25
+ end
26
+ end
27
+
28
+ class Info < R '/info/(\d+)', '/info/(\w+)/(\d+)', '/info', '/info/(\d+)/(\d+)/(\d+)/([\w-]+)'
29
+ def get(*args)
30
+ div do
31
+ code args.inspect; br; br
32
+ code ENV.inspect; br
33
+ code "Link: #{R(Info, 1, 2)}"
34
+ end
35
+ end
36
+ end
37
+
38
+ class View < R '/view/(\d+)'
39
+ def get post_id
40
+ @post = Post.find post_id
41
+ @comments = Models::Comment.find_all_by_post_id post_id
42
+ render :view
43
+ end
44
+ end
45
+
46
+ class Edit < R '/edit/(\d+)', '/edit'
47
+ def get post_id
48
+ unless @state.user_id.blank?
49
+ @user = User.find @state.user_id
50
+ end
51
+ @post = Post.find post_id
52
+ render :edit
53
+ end
54
+
55
+ def post
56
+ @post = Post.find input.post_id
57
+ @post.update_attributes :title => input.post_title, :body => input.post_body
58
+ redirect View, @post
59
+ end
60
+ end
61
+
62
+ class Comment
63
+ def post
64
+ Models::Comment.create({
65
+ :username => input.post_username,
66
+ :body => input.post_body,
67
+ :post_id => input.post_id
68
+ })
69
+ redirect View, input.post_id
70
+ end
71
+ end
72
+
73
+ class Login
74
+ def post
75
+ @user = User.find(:first, {
76
+ :conditions => [
77
+ 'username = ? AND password = ?',
78
+ input.username,
79
+ input.password
80
+ ]
81
+ })
82
+
83
+ if @user
84
+ @login = 'login success !'
85
+ @state.user_id = @user.id
86
+ else
87
+ @login = 'wrong user name or password'
89
88
  end
89
+ render :login
90
90
  end
91
-
92
- class Logout
93
- def get
94
- @state.user_id = nil
95
- render :logout
96
- end
97
- end
98
-
99
- class Style < R '/styles.css'
100
- def get
101
- @headers["Content-Type"] = "text/css; charset=utf-8"
102
- @body = %{
103
- body {
104
- font-family: Utopia, Georgia, serif;
105
- }
106
- h1.header {
107
- background-color: #fef;
108
- margin: 0; padding: 10px;
109
- }
110
- div.content {
111
- padding: 10px;
112
- }
113
- }
114
- end
91
+ end
92
+
93
+ class Cookies < R '/cookies'
94
+ def get
95
+ @cookies.awesome_cookie = 'camping for good'
96
+ @state.awesome_data = 'camping for good'
97
+ @posts = Post.find(:all)
98
+ render :index
99
+ end
100
+ end
101
+
102
+ class Logout
103
+ def get
104
+ @state.user_id = nil
105
+ render :logout
106
+ end
107
+ end
108
+
109
+ class Style < R '/styles.css'
110
+ def get
111
+ @headers["Content-Type"] = "text/css; charset=utf-8"
112
+ @body = %{
113
+ body {
114
+ font-family: Utopia, Georgia, serif;
115
+ }
116
+ h1.header {
117
+ background-color: #fef;
118
+ margin: 0; padding: 10px;
119
+ }
120
+ div.content {
121
+ padding: 10px;
122
+ }
123
+ }
124
+ end
125
+ end
126
+
127
+ # The following is introduced as a means to quickly test roundtrips
128
+ class SessionRoundtrip < R('/session-roundtrip')
129
+ def get
130
+ @state[:flag_in_session] = "This is a flag"
131
+ end
132
+
133
+ def post
134
+ if @state[:flag_in_session]
135
+ @state[:second_flag] = "This is a second flag"
136
+ end
137
+ return ''
138
+ end
139
+ end
140
+
141
+ class Redirector < R('/redirector')
142
+ def get
143
+ redirect '/blog/sniffer?one=two'
144
+ end
145
+ end
146
+
147
+ class Sniffer < R('/sniffer')
148
+ def get
149
+ input.to_hash.to_yaml
150
+ end
151
+ alias_method :post, :get
152
+ end
153
+
154
+ class Restafarian < R('/rest')
155
+ def delete
156
+ return "Called delete"
157
+ end
158
+
159
+ def put
160
+ return "Called put"
115
161
  end
162
+ end
116
163
  end
@@ -1,12 +1,11 @@
1
1
 
2
2
  module Blog::Models
3
- def self.schema(&block)
4
- @@schema = block if block_given?
5
- @@schema
6
- end
7
-
8
- class Post < Base; belongs_to :user; end
9
- class Comment < Base; belongs_to :user; end
10
- class User < Base; validates_presence_of :username; end
11
- end
3
+ def self.schema(&block)
4
+ @@schema = block if block_given?
5
+ @@schema
6
+ end
12
7
 
8
+ class Post < Base; belongs_to :user; end
9
+ class Comment < Base; belongs_to :user; end
10
+ class User < Base; validates_presence_of :username; end
11
+ end
data/public/blog/views.rb CHANGED
@@ -1,76 +1,75 @@
1
1
 
2
2
  module Blog::Views
3
3
 
4
- def layout
5
- html do
6
- head do
7
- title 'blog'
8
- link :rel => 'stylesheet', :type => 'text/css',
9
- :href => '/styles.css', :media => 'screen'
10
- end
11
- body do
12
- h1.header { a 'blog', :href => R(Index) }
13
- div.content do
14
- self << yield
15
- end
4
+ def layout
5
+ html do
6
+ head do
7
+ title 'blog'
8
+ link :rel => 'stylesheet', :type => 'text/css', :href => '/styles.css', :media => 'screen'
9
+ end
10
+ body do
11
+ h1.header { a 'blog', :href => R(Index) }
12
+ div.content do
13
+ self << yield
16
14
  end
17
15
  end
18
16
  end
19
-
20
- def index
21
- if @posts.empty?
22
- p 'No posts found.'
23
- p { a 'Add', :href => R(Add) }
24
- else
25
- for post in @posts
26
- _post(post)
27
- end
17
+ end
18
+
19
+ def index
20
+ if @posts.empty?
21
+ p 'No posts found.'
22
+ p { a 'Add', :href => R(Add) }
23
+ else
24
+ for post in @posts
25
+ _post(post)
28
26
  end
29
27
  end
30
-
31
- def login
32
- p { b @login }
33
- p { a 'Continue', :href => R(Add) }
28
+ end
29
+
30
+ def login
31
+ p { b @login }
32
+ p { a 'Continue', :href => R(Add) }
33
+ end
34
+
35
+ def logout
36
+ p "You have been logged out."
37
+ p { a 'Continue', :href => R(Index) }
38
+ end
39
+
40
+ def add
41
+ if @user
42
+ _form(post, :action => R(Add))
43
+ else
44
+ _login
34
45
  end
46
+ end
35
47
 
36
- def logout
37
- p "You have been logged out."
38
- p { a 'Continue', :href => R(Index) }
48
+ def edit
49
+ if @user
50
+ _form(post, :action => R(Edit))
51
+ else
52
+ _login
39
53
  end
54
+ end
40
55
 
41
- def add
42
- if @user
43
- _form(post, :action => R(Add))
44
- else
45
- _login
46
- end
47
- end
56
+ def view
57
+ _post(post)
48
58
 
49
- def edit
50
- if @user
51
- _form(post, :action => R(Edit))
52
- else
53
- _login
54
- end
59
+ p "Comment for this post:"
60
+ for c in @comments
61
+ h1 c.username
62
+ p c.body
55
63
  end
56
64
 
57
- def view
58
- _post(post)
59
-
60
- p "Comment for this post:"
61
- for c in @comments
62
- h1 c.username
63
- p c.body
64
- end
65
-
66
- form :action => R(Comment), :method => 'post' do
67
- label 'Name', :for => 'post_username'; br
68
- input :name => 'post_username', :type => 'text'; br
69
- label 'Comment', :for => 'post_body'; br
70
- textarea :name => 'post_body' do; end; br
71
- input :type => 'hidden', :name => 'post_id', :value => post.id
72
- input :type => 'submit'
73
- end
65
+ form :action => R(Comment), :method => 'post' do
66
+ label 'Name', :for => 'post_username'; br
67
+ input :name => 'post_username', :type => 'text'; br
68
+ label 'Comment', :for => 'post_body'; br
69
+ textarea :name => 'post_body' do; end; br
70
+ input :type => 'hidden', :name => 'post_id', :value => post.id
71
+ input :type => 'submit'
72
+ end
74
73
  end
75
74
 
76
75
  # partials
@@ -102,8 +101,7 @@ module Blog::Views
102
101
  end
103
102
  form({:method => 'post'}.merge(opts)) do
104
103
  label 'Title', :for => 'post_title'; br
105
- input :name => 'post_title', :type => 'text',
106
- :value => post.title; br
104
+ input :name => 'post_title', :type => 'text', :value => post.title; br
107
105
 
108
106
  label 'Body', :for => 'post_body'; br
109
107
  textarea post.body, :name => 'post_body'; br
@@ -112,4 +110,4 @@ module Blog::Views
112
110
  input :type => 'submit'
113
111
  end
114
112
  end
115
- end
113
+ end