rutter 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rutter/builder.rb +12 -3
- data/lib/rutter/scope.rb +4 -4
- data/lib/rutter/version.rb +1 -1
- data/spec/unit/builder_spec.rb +18 -0
- data/spec/unit/scope_spec.rb +14 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a36fa3c38406718e9fd535b91fbc0db685c6dd5de5ec08b07f7046711bfc8f2
|
4
|
+
data.tar.gz: 18f984a1fafcf11c2bbd6fe67cdd01fbfaf9e5b8508aa3aca3d54ac34ebc5c1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44648973cf5a942840484fbca8129c0d144a15948792923dee5ee63df19da4a273bda50bb28e1806b79e62930c23521284cd2b56506386dd00f745dd1752a783
|
7
|
+
data.tar.gz: 659a337b9f92478556992319ed2aed38f51e912a891004a765790d06a45a9bfac67c54b4987fc43b9f5c51412948803335d45e694eb15308803ea777bc442434
|
data/lib/rutter/builder.rb
CHANGED
@@ -195,14 +195,23 @@ module Rutter
|
|
195
195
|
# Route name/identifier.
|
196
196
|
# @param constraints [Hash]
|
197
197
|
# Route segment constraints.
|
198
|
+
# @param &block [Proc]
|
199
|
+
# Endpoint as a block.
|
200
|
+
# @yieldparam env [Hash]
|
201
|
+
# Rack's environment hash.
|
198
202
|
#
|
199
203
|
# @return [Rutter::Route]
|
200
204
|
#
|
201
205
|
# @raise [ArgumentError]
|
202
206
|
# If verb is unsupported.
|
207
|
+
# @raise [ArgumentError]
|
208
|
+
# If endpoint is missing.
|
203
209
|
#
|
204
210
|
# @private
|
205
|
-
def add(verb, path, to
|
211
|
+
def add(verb, path, to: nil, as: nil, constraints: nil, &block)
|
212
|
+
to = block if block_given?
|
213
|
+
raise "Missing endpoint" unless to
|
214
|
+
|
206
215
|
verb = verb.to_s.upcase
|
207
216
|
|
208
217
|
unless VERBS.include?(verb)
|
@@ -231,8 +240,8 @@ module Rutter
|
|
231
240
|
|
232
241
|
# @see #add
|
233
242
|
VERBS.each do |verb|
|
234
|
-
define_method verb.downcase do |path, to
|
235
|
-
add verb, path, to: to, as: as, constraints: constraints
|
243
|
+
define_method verb.downcase do |path, to: nil, as: nil, constraints: nil, &block|
|
244
|
+
add verb, path, to: to, as: as, constraints: constraints, &block
|
236
245
|
end
|
237
246
|
end
|
238
247
|
|
data/lib/rutter/scope.rb
CHANGED
@@ -49,18 +49,18 @@ module Rutter
|
|
49
49
|
end
|
50
50
|
|
51
51
|
# @see Rutter::Builder#add
|
52
|
-
def add(verb, path, to
|
52
|
+
def add(verb, path, to: nil, as: nil, constraints: nil, &block)
|
53
53
|
path = Naming.join(@path, path)
|
54
54
|
to = Naming.join(@namespace, to) if to.is_a?(String)
|
55
55
|
as = Naming.join(@as, as) if as
|
56
56
|
|
57
|
-
@router.add verb, path, to: to, as: as, constraints: constraints
|
57
|
+
@router.add verb, path, to: to, as: as, constraints: constraints, &block
|
58
58
|
end
|
59
59
|
|
60
60
|
# @see Rutter::Builder#add
|
61
61
|
VERBS.each do |verb|
|
62
|
-
define_method verb.downcase do |path, to
|
63
|
-
add verb, path, to: to, as: as, constraints: constraints
|
62
|
+
define_method verb.downcase do |path, to: nil, as: nil, constraints: nil, &block|
|
63
|
+
add verb, path, to: to, as: as, constraints: constraints, &block
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
data/lib/rutter/version.rb
CHANGED
data/spec/unit/builder_spec.rb
CHANGED
@@ -107,6 +107,24 @@ module Rutter
|
|
107
107
|
expect(route.match?(env_for("/books/pickaxe")))
|
108
108
|
.to be(false)
|
109
109
|
end
|
110
|
+
|
111
|
+
it "support block as endpoint" do
|
112
|
+
router.get "/" do |env|
|
113
|
+
[200, {}, [env["message"]]]
|
114
|
+
end
|
115
|
+
|
116
|
+
_, _, body = router.call("REQUEST_METHOD" => "GET",
|
117
|
+
"PATH_INFO" => "/",
|
118
|
+
"message" => "Hello World")
|
119
|
+
|
120
|
+
expect(body.join)
|
121
|
+
.to eq("Hello World")
|
122
|
+
end
|
123
|
+
|
124
|
+
it "raises an error if no endpoint is given" do
|
125
|
+
expect { router.get "/" }
|
126
|
+
.to raise_error("Missing endpoint")
|
127
|
+
end
|
110
128
|
end
|
111
129
|
|
112
130
|
describe "verbs" do
|
data/spec/unit/scope_spec.rb
CHANGED
@@ -34,6 +34,20 @@ module Rutter
|
|
34
34
|
expect(route.match?(env_for("/books/pickaxe")))
|
35
35
|
.to be(false)
|
36
36
|
end
|
37
|
+
|
38
|
+
it "support block as endpoint" do
|
39
|
+
scope = router.scope path: "/books"
|
40
|
+
scope.get "/" do |env|
|
41
|
+
[200, {}, [env["message"]]]
|
42
|
+
end
|
43
|
+
|
44
|
+
_, _, body = router.call("REQUEST_METHOD" => "GET",
|
45
|
+
"PATH_INFO" => "/books",
|
46
|
+
"message" => "Hello World")
|
47
|
+
|
48
|
+
expect(body.join)
|
49
|
+
.to eq("Hello World")
|
50
|
+
end
|
37
51
|
end
|
38
52
|
|
39
53
|
describe "#mount" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rutter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tobias Sandelius
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mustermann
|