mosquito 0.1.2 → 0.1.3
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.
- data/CHANGELOG +22 -0
- data/Manifest.txt +10 -16
- data/README.txt +54 -29
- data/Rakefile +29 -2
- data/lib/mosquito.rb +444 -87
- data/public/bare.rb +71 -0
- data/public/blog.rb +1 -6
- data/public/blog/controllers.rb +157 -110
- data/public/blog/models.rb +8 -9
- data/public/blog/views.rb +59 -61
- data/test/sage_advice_cases/parsing_arrays.rb +25 -0
- data/test/test_bare.rb +66 -0
- data/test/test_blog.rb +140 -26
- data/test/test_helpers.rb +20 -0
- data/test/test_mock_request.rb +292 -0
- data/test/test_mock_upload.rb +55 -0
- metadata +31 -18
- data/public/homepage.rb +0 -64
- data/test/test_homepage.rb +0 -23
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
|
-
|
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
|
data/public/blog/controllers.rb
CHANGED
@@ -1,116 +1,163 @@
|
|
1
1
|
|
2
2
|
module Blog::Controllers
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
@
|
86
|
-
|
87
|
-
@
|
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
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
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
|
data/public/blog/models.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
|
2
2
|
module Blog::Models
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
48
|
+
def edit
|
49
|
+
if @user
|
50
|
+
_form(post, :action => R(Edit))
|
51
|
+
else
|
52
|
+
_login
|
39
53
|
end
|
54
|
+
end
|
40
55
|
|
41
|
-
|
42
|
-
|
43
|
-
_form(post, :action => R(Add))
|
44
|
-
else
|
45
|
-
_login
|
46
|
-
end
|
47
|
-
end
|
56
|
+
def view
|
57
|
+
_post(post)
|
48
58
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|