syro 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +7 -0
- data/README.md +5 -14
- data/lib/syro.rb +26 -78
- data/syro.gemspec +1 -1
- data/test/all.rb +13 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 024518f1cb6e65935ea456dc18ac359d1b18eae4
|
4
|
+
data.tar.gz: 8834daf0ca51401e82b0fe1cd05fb2f71986a5ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5fc9a10dbc89d25bc24b75ab697c03d9f6bb4407daa07505c3967c8b762074ec1769d34f802c39cd37c2f5deb21f9f8a33e0e7b8d3eeb3c22e432e0d45a1a9f
|
7
|
+
data.tar.gz: 586fadba5f72bcec32fb1f5498f4a15f2a655bfb5dbed9a4b9f7b7702f5272f8e64de76cbb8d34347d5a09c626236900b6f4817d68f7db7ca779197630566225
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -37,8 +37,7 @@ app = Syro.new {
|
|
37
37
|
|
38
38
|
The block is evaluated in a sandbox where the following methods are
|
39
39
|
available: `env`, `req`, `res`, `inbox`, `call`, `run`, `halt`,
|
40
|
-
`match`, `on`, `root?`, `root`, `
|
41
|
-
`get`, `post`, `patch` and `delete`.
|
40
|
+
`match`, `on`, `root?`, `root`,`get`, `post`, `patch` and `delete`.
|
42
41
|
|
43
42
|
As a recommendation, user created variables should be instance
|
44
43
|
variables. That way they won't mix with the API methods defined in
|
@@ -78,24 +77,16 @@ executed only if the request is matched.
|
|
78
77
|
|
79
78
|
`root`: Receives a block and calls it only if `root?` is true.
|
80
79
|
|
81
|
-
`get
|
82
|
-
|
83
|
-
`get`: Receives a block and calls it only if `root?` and `get?` are
|
80
|
+
`get`: Receives a block and calls it only if `root?` and `req.get?` are
|
84
81
|
true.
|
85
82
|
|
86
|
-
`post
|
87
|
-
|
88
|
-
`post`: Receives a block and calls it only if `root?` and `post?`
|
83
|
+
`post`: Receives a block and calls it only if `root?` and `req.post?`
|
89
84
|
are true.
|
90
85
|
|
91
|
-
`patch
|
92
|
-
|
93
|
-
`patch`: Receives a block and calls it only if `root?` and `patch?`
|
86
|
+
`patch`: Receives a block and calls it only if `root?` and `req.patch?`
|
94
87
|
are true.
|
95
88
|
|
96
|
-
`delete
|
97
|
-
|
98
|
-
`delete`: Receives a block and calls it only if `root?` and `delete?`
|
89
|
+
`delete`: Receives a block and calls it only if `root?` and `req.delete?`
|
99
90
|
are true.
|
100
91
|
|
101
92
|
Examples
|
data/lib/syro.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
#
|
3
3
|
# Copyright (c) 2015 Michel Martens
|
4
|
-
#
|
4
|
+
#
|
5
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
# of this software and associated documentation files (the "Software"), to deal
|
7
7
|
# in the Software without restriction, including without limitation the rights
|
8
8
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
9
|
# copies of the Software, and to permit persons to whom the Software is
|
10
10
|
# furnished to do so, subject to the following conditions:
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# The above copyright notice and this permission notice shall be included in
|
13
13
|
# all copies or substantial portions of the Software.
|
14
|
-
#
|
14
|
+
#
|
15
15
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
16
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
17
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
@@ -24,31 +24,10 @@ require "rack"
|
|
24
24
|
require "seg"
|
25
25
|
|
26
26
|
class Syro
|
27
|
-
|
28
|
-
# Method override parameter
|
29
|
-
OVERRIDE = "_method".freeze
|
30
|
-
|
31
|
-
# HTTP environment variables
|
32
|
-
PATH_INFO = "PATH_INFO".freeze
|
33
|
-
SCRIPT_NAME = "SCRIPT_NAME".freeze
|
34
|
-
REQUEST_METHOD = "REQUEST_METHOD".freeze
|
35
|
-
|
36
|
-
# Response headers
|
37
|
-
LOCATION = "Location".freeze
|
38
|
-
CONTENT_TYPE = "Content-Type".freeze
|
39
|
-
CONTENT_LENGTH = "Content-Length".freeze
|
40
|
-
CONTENT_TYPE_DEFAULT = "text/html".freeze
|
41
|
-
|
42
|
-
# Request methods
|
43
|
-
GET = 'GET'.freeze
|
44
|
-
POST = 'POST'.freeze
|
45
|
-
PATCH = 'PATCH'.freeze
|
46
|
-
DELETE = 'DELETE'.freeze
|
47
|
-
|
48
|
-
# Available methods
|
49
|
-
METHODS = [GET, POST, PATCH, DELETE].freeze
|
50
|
-
|
51
27
|
class Response
|
28
|
+
LOCATION = "Location".freeze
|
29
|
+
DEFAULT = "text/html".freeze
|
30
|
+
|
52
31
|
attr_accessor :status
|
53
32
|
|
54
33
|
attr :body
|
@@ -73,16 +52,25 @@ class Syro
|
|
73
52
|
s = str.to_s
|
74
53
|
|
75
54
|
@length += s.bytesize
|
76
|
-
@headers[
|
55
|
+
@headers[Rack::CONTENT_LENGTH] = @length.to_s
|
77
56
|
@body << s
|
78
57
|
end
|
79
58
|
|
80
59
|
def redirect(path, status = 302)
|
81
|
-
@headers[
|
82
|
-
@status
|
60
|
+
@headers[LOCATION] = path
|
61
|
+
@status = status
|
83
62
|
end
|
84
63
|
|
85
64
|
def finish
|
65
|
+
if @status.nil?
|
66
|
+
if @body.empty?
|
67
|
+
@status = 404
|
68
|
+
else
|
69
|
+
@headers[Rack::CONTENT_TYPE] ||= DEFAULT
|
70
|
+
@status = 200
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
86
74
|
[@status, @headers, @body]
|
87
75
|
end
|
88
76
|
|
@@ -124,43 +112,19 @@ class Syro
|
|
124
112
|
@syro_env = env
|
125
113
|
@syro_req = Rack::Request.new(env)
|
126
114
|
@syro_res = Syro::Response.new
|
127
|
-
@syro_path = Seg.new(env.fetch(
|
115
|
+
@syro_path = Seg.new(env.fetch(Rack::PATH_INFO))
|
128
116
|
@syro_inbox = inbox
|
129
117
|
|
130
|
-
|
131
|
-
if env[Syro::REQUEST_METHOD] == Syro::POST
|
132
|
-
value = @syro_req.POST[Syro::OVERRIDE]
|
133
|
-
|
134
|
-
if value != nil
|
135
|
-
env[Syro::REQUEST_METHOD] = value.upcase
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
@syro_method = Syro::METHODS.index(env[Syro::REQUEST_METHOD])
|
140
|
-
|
141
|
-
result = catch(:halt) do
|
118
|
+
catch(:halt) do
|
142
119
|
instance_eval(&@syro_code)
|
143
120
|
|
144
|
-
@syro_res.status = 404
|
145
121
|
@syro_res.finish
|
146
122
|
end
|
147
|
-
|
148
|
-
if result[0].nil?
|
149
|
-
if result[2].empty?
|
150
|
-
result[0] = 404
|
151
|
-
else
|
152
|
-
result[1][Syro::CONTENT_TYPE] ||=
|
153
|
-
Syro::CONTENT_TYPE_DEFAULT
|
154
|
-
result[0] = 200
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
result
|
159
123
|
end
|
160
124
|
|
161
125
|
def run(app, inbox = {})
|
162
|
-
env[
|
163
|
-
env[
|
126
|
+
env[Rack::PATH_INFO] = @syro_path.curr
|
127
|
+
env[Rack::SCRIPT_NAME] = @syro_path.prev
|
164
128
|
|
165
129
|
halt(app.call(env, inbox))
|
166
130
|
end
|
@@ -198,24 +162,8 @@ class Syro
|
|
198
162
|
end
|
199
163
|
end
|
200
164
|
|
201
|
-
def get?
|
202
|
-
@syro_method == 0
|
203
|
-
end
|
204
|
-
|
205
|
-
def post?
|
206
|
-
@syro_method == 1
|
207
|
-
end
|
208
|
-
|
209
|
-
def patch?
|
210
|
-
@syro_method == 2
|
211
|
-
end
|
212
|
-
|
213
|
-
def delete?
|
214
|
-
@syro_method == 3
|
215
|
-
end
|
216
|
-
|
217
165
|
def get
|
218
|
-
if root? && get?
|
166
|
+
if root? && req.get?
|
219
167
|
yield
|
220
168
|
|
221
169
|
halt(res.finish)
|
@@ -223,7 +171,7 @@ class Syro
|
|
223
171
|
end
|
224
172
|
|
225
173
|
def post
|
226
|
-
if root? && post?
|
174
|
+
if root? && req.post?
|
227
175
|
yield
|
228
176
|
|
229
177
|
halt(res.finish)
|
@@ -231,7 +179,7 @@ class Syro
|
|
231
179
|
end
|
232
180
|
|
233
181
|
def patch
|
234
|
-
if root? && patch?
|
182
|
+
if root? && req.patch?
|
235
183
|
yield
|
236
184
|
|
237
185
|
halt(res.finish)
|
@@ -239,7 +187,7 @@ class Syro
|
|
239
187
|
end
|
240
188
|
|
241
189
|
def delete
|
242
|
-
if root? && delete?
|
190
|
+
if root? && req.delete?
|
243
191
|
yield
|
244
192
|
|
245
193
|
halt(res.finish)
|
data/syro.gemspec
CHANGED
data/test/all.rb
CHANGED
@@ -101,6 +101,10 @@ app = Syro.new {
|
|
101
101
|
get {
|
102
102
|
res.write(@one)
|
103
103
|
}
|
104
|
+
|
105
|
+
post {
|
106
|
+
res.redirect("/one")
|
107
|
+
}
|
104
108
|
}
|
105
109
|
}
|
106
110
|
|
@@ -181,3 +185,12 @@ test "leaks" do |f|
|
|
181
185
|
assert_equal "", f.last_response.body
|
182
186
|
assert_equal 200, f.last_response.status
|
183
187
|
end
|
188
|
+
|
189
|
+
test "redirect" do |f|
|
190
|
+
f.post("/two")
|
191
|
+
assert_equal 302, f.last_response.status
|
192
|
+
|
193
|
+
f.follow_redirect!
|
194
|
+
assert_equal "1", f.last_response.body
|
195
|
+
assert_equal 200, f.last_response.status
|
196
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: syro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Martens
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: seg
|