syro 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +7 -0
  3. data/README.md +5 -14
  4. data/lib/syro.rb +26 -78
  5. data/syro.gemspec +1 -1
  6. data/test/all.rb +13 -0
  7. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d42da05d9e40f9dd247ca71a2f3b2514fdeedb0a
4
- data.tar.gz: 6019c98bff4e489bcd0f1f5975dc046a3ab94193
3
+ metadata.gz: 024518f1cb6e65935ea456dc18ac359d1b18eae4
4
+ data.tar.gz: 8834daf0ca51401e82b0fe1cd05fb2f71986a5ca
5
5
  SHA512:
6
- metadata.gz: 52a247d00e9b09cd152a65e8702150e8df91c50f66bc811f7ac94be90ba3dd3e9b93cb016a70474ac4c19005835ad62382c3d920edb49777b991cac7d4ab2106
7
- data.tar.gz: 15c8260bae6bf88ffbb1882b2c4a9cb50ab97b4d693ba4b98ba2a254d014175c6c1c10d48ec570767039081d76395aefb0f390c96d1b80730d7934e0418aaa37
6
+ metadata.gz: b5fc9a10dbc89d25bc24b75ab697c03d9f6bb4407daa07505c3967c8b762074ec1769d34f802c39cd37c2f5deb21f9f8a33e0e7b8d3eeb3c22e432e0d45a1a9f
7
+ data.tar.gz: 586fadba5f72bcec32fb1f5498f4a15f2a655bfb5dbed9a4b9f7b7702f5272f8e64de76cbb8d34347d5a09c626236900b6f4817d68f7db7ca779197630566225
data/CHANGELOG CHANGED
@@ -0,0 +1,7 @@
1
+ 0.0.3
2
+
3
+ * Remove request method overriding (@larrylv)
4
+
5
+ 0.0.2
6
+
7
+ * Defer method comparisons to Rack::Request (@pote)
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`, `get?`, `post?`, `patch?`, `delete?`,
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?`: Returns true if the `REQUEST_METHOD` is `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?`: Returns true if the `REQUEST_METHOD` is `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?`: Returns true if the `REQUEST_METHOD` is `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?`: Returns true if the `REQUEST_METHOD` is `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
@@ -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[Syro::CONTENT_LENGTH] = @length.to_s
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[Syro::LOCATION] = path
82
- @status = 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(Syro::PATH_INFO))
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[Syro::PATH_INFO] = @syro_path.curr
163
- env[Syro::SCRIPT_NAME] = @syro_path.prev
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)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "syro"
3
- s.version = "0.0.2"
3
+ s.version = "0.0.3"
4
4
  s.summary = "Simple router"
5
5
  s.description = "Simple router for web applications"
6
6
  s.authors = ["Michel Martens"]
@@ -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.2
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-24 00:00:00.000000000 Z
11
+ date: 2015-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: seg