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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 12a5714cb11c9589f34960ce325c4a095f4d5c8dbeb15d95e8f9b9be560d15d8
4
- data.tar.gz: f182eb9db7eb3205a003379fd937864e2dda69b615be63e37ad4cfbc8a523402
3
+ metadata.gz: 0a36fa3c38406718e9fd535b91fbc0db685c6dd5de5ec08b07f7046711bfc8f2
4
+ data.tar.gz: 18f984a1fafcf11c2bbd6fe67cdd01fbfaf9e5b8508aa3aca3d54ac34ebc5c1f
5
5
  SHA512:
6
- metadata.gz: '0885bcb1e09323e8d3e4753a04a2fd192d726c68d4546ba820fa29ae9d9492c847c296022b6e34cb4123808fa87633c5728b2042467d0e1e79ec1d9b679b200a'
7
- data.tar.gz: 928d8dfd4b82989f196f0a0f25fcf734953c8b77f0884697f756382117de1aaa51f7049e832dc627d1a6dabea43143046ebf08e4e842497a899bd253cee45372
6
+ metadata.gz: 44648973cf5a942840484fbca8129c0d144a15948792923dee5ee63df19da4a273bda50bb28e1806b79e62930c23521284cd2b56506386dd00f745dd1752a783
7
+ data.tar.gz: 659a337b9f92478556992319ed2aed38f51e912a891004a765790d06a45a9bfac67c54b4987fc43b9f5c51412948803335d45e694eb15308803ea777bc442434
@@ -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:, as: nil, constraints: nil)
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:, as: nil, constraints: nil|
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
 
@@ -49,18 +49,18 @@ module Rutter
49
49
  end
50
50
 
51
51
  # @see Rutter::Builder#add
52
- def add(verb, path, to:, as: nil, constraints: nil)
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:, as: nil, constraints: nil|
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
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Rutter
4
4
  # Current version number.
5
- VERSION = "0.3.0"
5
+ VERSION = "0.3.1"
6
6
  end
@@ -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
@@ -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.0
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-16 00:00:00.000000000 Z
11
+ date: 2020-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mustermann