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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c41182a06c40d94e076e5cd07b77a1b6b09c2129
4
- data.tar.gz: 33cedd76ce5cea5a70a0db7ec33b405e5b0153ba
2
+ SHA256:
3
+ metadata.gz: 0dd5ceaab614b795ad92e63b73c2d1d3359455a7970c703a366c781fc2b983ea
4
+ data.tar.gz: aa14c451bcfff884a710dd41105f1fc1ec24d03ab2dfb3ec25b264f3a136f2c7
5
5
  SHA512:
6
- metadata.gz: 7093091a3b5e98389553d608bf4e7cedb9818141d0e09fbd6132c27e5783f04b97e9d151ba7234e5dd81a41d28d9e9dd80547e4d878bf4a098a6ebdfd886740a
7
- data.tar.gz: 829c737f9dc612a532a0650a8a84a3a46671e153f74dfb10faead95de4e378afc6778c2ac8b3371d1e72a6c250f392f0f10dc5e30aa945c04d0083afdbf1abc7
6
+ metadata.gz: 970cab90c32a0c39862e79087f5740debd0d5d264b6bca0c6d94d60fee73645dc34e9c634459d9c952316f6000328f23f137d2f908a4e6e08a7040fecf1f667d
7
+ data.tar.gz: cbc70d253a2951d31d6f82c0fb8bb25e687817d687a0508d8ddd960bec9f43d062b680bd9221503a58f60fb3d594b51ff51668f594e4022b04c416148169d5c1
data/.gems CHANGED
@@ -1,4 +1,4 @@
1
- cutest -v 1.2.2
2
- tilt -v 2.0.1
3
- rack-test -v 0.6.3
4
- rack -v 2.0.1
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
@@ -1,3 +1,11 @@
1
+ 3.9.0
2
+
3
+ * Cache regular expressions
4
+
5
+ 3.8.1
6
+
7
+ * Remove requirement on Time
8
+
1
9
  3.8.0
2
10
 
3
11
  * Change gemspec to allow Rack 2.
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("Not authorized")
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 explaning the proposed change
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cuba"
3
- s.version = "3.8.0"
3
+ s.version = "3.9.3"
4
4
  s.summary = "Microframework for web applications."
5
5
  s.description = "Cuba is a microframework for web applications."
6
6
  s.authors = ["Michel Martens"]
@@ -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(/\A\/(#{pattern})(\/|\z)/)
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
@@ -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
@@ -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
@@ -11,9 +11,9 @@ test "set cookie" do
11
11
 
12
12
  env = { "SCRIPT_NAME" => "/", "PATH_INFO" => "/" }
13
13
 
14
- _, headers, body = Cuba.call(env)
14
+ _, headers, body = Cuba.call(env)
15
15
 
16
- assert_equal "foo=bar\nbar=baz", headers["Set-Cookie"]
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
- _, headers, body = Cuba.call(env)
30
+ _, headers, body = Cuba.call(env)
31
31
 
32
- assert_equal "foo=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000",
33
- headers["Set-Cookie"]
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.8.0
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: 2016-07-01 00:00:00.000000000 Z
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
- rubyforge_project:
156
- rubygems_version: 2.4.5.1
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: []