cuba 3.8.0 → 3.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gems +4 -4
- data/CHANGELOG +8 -0
- data/README.md +51 -2
- data/cuba.gemspec +1 -1
- data/lib/cuba.rb +28 -3
- data/test/accept.rb +45 -0
- data/test/composition.rb +36 -2
- data/test/cookie.rb +5 -5
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0dd5ceaab614b795ad92e63b73c2d1d3359455a7970c703a366c781fc2b983ea
|
4
|
+
data.tar.gz: aa14c451bcfff884a710dd41105f1fc1ec24d03ab2dfb3ec25b264f3a136f2c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 970cab90c32a0c39862e79087f5740debd0d5d264b6bca0c6d94d60fee73645dc34e9c634459d9c952316f6000328f23f137d2f908a4e6e08a7040fecf1f667d
|
7
|
+
data.tar.gz: cbc70d253a2951d31d6f82c0fb8bb25e687817d687a0508d8ddd960bec9f43d062b680bd9221503a58f60fb3d594b51ff51668f594e4022b04c416148169d5c1
|
data/.gems
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
rack -v 2.2.3
|
2
|
+
rack-test -v 1.1.0
|
3
|
+
cutest -v 1.2.3
|
4
|
+
tilt -v 2.0.10
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -360,7 +360,7 @@ Cuba.define do
|
|
360
360
|
csrf.reset!
|
361
361
|
|
362
362
|
res.status = 403
|
363
|
-
res.write("
|
363
|
+
res.write("Forbidden")
|
364
364
|
|
365
365
|
halt(res.finish)
|
366
366
|
end
|
@@ -416,6 +416,14 @@ on get, "api" do ... end
|
|
416
416
|
Actually, `get` is syntax sugar for `req.get?`, which in turn is syntax sugar
|
417
417
|
for `env["REQUEST_METHOD"] == "GET"`.
|
418
418
|
|
419
|
+
Headers
|
420
|
+
-------
|
421
|
+
|
422
|
+
You can set the headers by assigning values to the hash `req.headers`.
|
423
|
+
If you want to inspect the incoming headers, you have to read from
|
424
|
+
the `env` hash. For example, if you want to know the referrer you
|
425
|
+
can check `env["HTTP_REFERER"]`.
|
426
|
+
|
419
427
|
Request and Response
|
420
428
|
--------------------
|
421
429
|
|
@@ -514,6 +522,47 @@ Cuba.define do
|
|
514
522
|
end
|
515
523
|
```
|
516
524
|
|
525
|
+
## Embedding routes from other modules
|
526
|
+
|
527
|
+
While the `run` command allows you to handle over the control to a
|
528
|
+
sub app, sometimes you may want to just embed routes defined in
|
529
|
+
another module. There's no built-in method to do it, but if you are
|
530
|
+
willing to experiment you can try the following.
|
531
|
+
|
532
|
+
Let's say you have defined routes in modules `A` and `B`, and you
|
533
|
+
want to mount those routes in your application.
|
534
|
+
|
535
|
+
First, you will have to extend Cuba with this code:
|
536
|
+
|
537
|
+
```ruby
|
538
|
+
class Cuba
|
539
|
+
def mount(app)
|
540
|
+
result = app.call(req.env)
|
541
|
+
halt result if result[0] != 404
|
542
|
+
end
|
543
|
+
end
|
544
|
+
```
|
545
|
+
|
546
|
+
It doesn't matter where you define it as long as Cuba has already
|
547
|
+
been required. For instance, you could extract that to a plugin and
|
548
|
+
it would work just fine.
|
549
|
+
|
550
|
+
Then, in your application, you can use it like this:
|
551
|
+
|
552
|
+
```ruby
|
553
|
+
Cuba.define do
|
554
|
+
on default do
|
555
|
+
mount A
|
556
|
+
mount B
|
557
|
+
end
|
558
|
+
end
|
559
|
+
```
|
560
|
+
|
561
|
+
It should halt the request only if the resulting status from calling
|
562
|
+
the mounted app is not 404. If you run into some unexpected behavior,
|
563
|
+
let me know by creating an issue and we'll look at how to workaround
|
564
|
+
any difficulties.
|
565
|
+
|
517
566
|
Testing
|
518
567
|
-------
|
519
568
|
|
@@ -712,7 +761,7 @@ Contributing
|
|
712
761
|
------------
|
713
762
|
|
714
763
|
A good first step is to meet us on IRC and discuss ideas. If that's
|
715
|
-
not possible, you can create an issue
|
764
|
+
not possible, you can create an issue explaining the proposed change
|
716
765
|
and a use case. We pay a lot of attention to use cases, because our
|
717
766
|
goal is to keep the code base simple. In many cases, the result of
|
718
767
|
a conversation will be the creation of another tool, instead of the
|
data/cuba.gemspec
CHANGED
data/lib/cuba.rb
CHANGED
@@ -1,15 +1,22 @@
|
|
1
|
+
require "delegate"
|
1
2
|
require "rack"
|
2
|
-
require "time"
|
3
3
|
|
4
4
|
class Cuba
|
5
5
|
SLASH = "/".freeze
|
6
6
|
EMPTY = "".freeze
|
7
7
|
SEGMENT = "([^\\/]+)".freeze
|
8
8
|
DEFAULT = "text/html; charset=utf-8".freeze
|
9
|
+
REGEXES = Hash.new { |h, pattern| h[pattern] = /\A\/(#{pattern})(\/|\z)/ }
|
9
10
|
|
10
11
|
class Response
|
11
12
|
LOCATION = "Location".freeze
|
12
13
|
|
14
|
+
module ContentType
|
15
|
+
HTML = "text/html".freeze # :nodoc:
|
16
|
+
TEXT = "text/plain".freeze # :nodoc:
|
17
|
+
JSON = "application/json".freeze # :nodoc:
|
18
|
+
end
|
19
|
+
|
13
20
|
attr_accessor :status
|
14
21
|
|
15
22
|
attr :body
|
@@ -38,6 +45,24 @@ class Cuba
|
|
38
45
|
@body << s
|
39
46
|
end
|
40
47
|
|
48
|
+
# Write response body as text/plain
|
49
|
+
def text(str)
|
50
|
+
@headers[Rack::CONTENT_TYPE] = ContentType::TEXT
|
51
|
+
write(str)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Write response body as text/html
|
55
|
+
def html(str)
|
56
|
+
@headers[Rack::CONTENT_TYPE] = ContentType::HTML
|
57
|
+
write(str)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Write response body as application/json
|
61
|
+
def json(str)
|
62
|
+
@headers[Rack::CONTENT_TYPE] = ContentType::JSON
|
63
|
+
write(str)
|
64
|
+
end
|
65
|
+
|
41
66
|
def redirect(path, status = 302)
|
42
67
|
@headers[LOCATION] = path
|
43
68
|
@status = status
|
@@ -212,7 +237,7 @@ class Cuba
|
|
212
237
|
private :try
|
213
238
|
|
214
239
|
def consume(pattern)
|
215
|
-
matchdata = env[Rack::PATH_INFO].match(
|
240
|
+
matchdata = env[Rack::PATH_INFO].match(REGEXES[pattern])
|
216
241
|
|
217
242
|
return false unless matchdata
|
218
243
|
|
@@ -262,7 +287,7 @@ class Cuba
|
|
262
287
|
# # If not provided, username == "guest"
|
263
288
|
# end
|
264
289
|
def param(key, default = nil)
|
265
|
-
value = req[key] || default
|
290
|
+
value = req.params[key.to_s] || default
|
266
291
|
|
267
292
|
lambda { captures << value unless value.to_s.empty? }
|
268
293
|
end
|
data/test/accept.rb
CHANGED
@@ -30,3 +30,48 @@ test "tests don't fail when you don't specify an accept type" do
|
|
30
30
|
|
31
31
|
assert_response body, ["Default action"]
|
32
32
|
end
|
33
|
+
|
34
|
+
test "accept HTML mimetype" do
|
35
|
+
Cuba.define do
|
36
|
+
on accept("text/html") do
|
37
|
+
res.write Cuba::Response::ContentType::HTML
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
env = { "HTTP_ACCEPT" => "text/html",
|
42
|
+
"SCRIPT_NAME" => "/", "PATH_INFO" => "/post" }
|
43
|
+
|
44
|
+
_, _, body = Cuba.call(env)
|
45
|
+
|
46
|
+
assert_response body, ["text/html"]
|
47
|
+
end
|
48
|
+
|
49
|
+
test "accept TEXT mimetype" do
|
50
|
+
Cuba.define do
|
51
|
+
on accept("text/plain") do
|
52
|
+
res.write Cuba::Response::ContentType::TEXT
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
env = { "HTTP_ACCEPT" => "text/plain",
|
57
|
+
"SCRIPT_NAME" => "/", "PATH_INFO" => "/post" }
|
58
|
+
|
59
|
+
_, _, body = Cuba.call(env)
|
60
|
+
|
61
|
+
assert_response body, ["text/plain"]
|
62
|
+
end
|
63
|
+
|
64
|
+
test "accept JSON mimetype" do
|
65
|
+
Cuba.define do
|
66
|
+
on accept("application/json") do
|
67
|
+
res.write Cuba::Response::ContentType::JSON
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
env = { "HTTP_ACCEPT" => "application/json",
|
72
|
+
"SCRIPT_NAME" => "/", "PATH_INFO" => "/get" }
|
73
|
+
|
74
|
+
_, _, body = Cuba.call(env)
|
75
|
+
|
76
|
+
assert_response body, ["application/json"]
|
77
|
+
end
|
data/test/composition.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
require File.expand_path("helper", File.dirname(__FILE__))
|
2
2
|
|
3
3
|
test "composing on top of a PATH" do
|
4
|
-
Services = Cuba.new
|
4
|
+
Services = Cuba.new do
|
5
5
|
on "services/:id" do |id|
|
6
6
|
res.write "View #{id}"
|
7
7
|
end
|
8
|
-
|
8
|
+
end
|
9
9
|
|
10
10
|
Cuba.define do
|
11
11
|
on "provider" do
|
@@ -67,3 +67,37 @@ test "redefining not_found" do
|
|
67
67
|
assert_response resp, ["Error 404"]
|
68
68
|
assert_equal status, 404
|
69
69
|
end
|
70
|
+
|
71
|
+
test "multi mount" do
|
72
|
+
A = Cuba.new do
|
73
|
+
on "a" do
|
74
|
+
res.write "View a"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
B = Cuba.new do
|
79
|
+
on "b" do
|
80
|
+
res.write "View b"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class Cuba
|
85
|
+
def mount(app)
|
86
|
+
result = app.call(req.env)
|
87
|
+
halt result if result[0] != 404
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
Cuba.define do
|
92
|
+
on default do
|
93
|
+
mount A
|
94
|
+
mount B
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/b" }
|
99
|
+
|
100
|
+
_, _, resp = Cuba.call(env)
|
101
|
+
|
102
|
+
assert_response resp, ["View b"]
|
103
|
+
end
|
data/test/cookie.rb
CHANGED
@@ -11,9 +11,9 @@ test "set cookie" do
|
|
11
11
|
|
12
12
|
env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/" }
|
13
13
|
|
14
|
-
|
14
|
+
_, headers, body = Cuba.call(env)
|
15
15
|
|
16
|
-
|
16
|
+
assert_equal "foo=bar\nbar=baz", headers["Set-Cookie"]
|
17
17
|
end
|
18
18
|
|
19
19
|
test "delete cookie" do
|
@@ -27,8 +27,8 @@ test "delete cookie" do
|
|
27
27
|
|
28
28
|
env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/" }
|
29
29
|
|
30
|
-
|
30
|
+
_, headers, body = Cuba.call(env)
|
31
31
|
|
32
|
-
|
33
|
-
|
32
|
+
assert_equal "foo=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 GMT",
|
33
|
+
headers["Set-Cookie"]
|
34
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cuba
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michel Martens
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -137,7 +137,7 @@ homepage: https://github.com/soveran/cuba
|
|
137
137
|
licenses:
|
138
138
|
- MIT
|
139
139
|
metadata: {}
|
140
|
-
post_install_message:
|
140
|
+
post_install_message:
|
141
141
|
rdoc_options: []
|
142
142
|
require_paths:
|
143
143
|
- lib
|
@@ -152,9 +152,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
152
|
- !ruby/object:Gem::Version
|
153
153
|
version: '0'
|
154
154
|
requirements: []
|
155
|
-
|
156
|
-
|
157
|
-
signing_key:
|
155
|
+
rubygems_version: 3.1.2
|
156
|
+
signing_key:
|
158
157
|
specification_version: 4
|
159
158
|
summary: Microframework for web applications.
|
160
159
|
test_files: []
|