cuba 3.1.0.rc1 → 3.1.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,9 +1,11 @@
1
- 3.1.0.rc1
1
+ 3.1.0.rc2
2
2
 
3
3
  * Do a deep clone of the settings object during inheritance.
4
4
  * Start namespacing plugins (i.e. settings[:render]).
5
- * Use rack/test when doing equire 'cuba/test'.
6
- * Capybara available via equire 'cuba/capybara'.
5
+ * Use rack/test when doing `require 'cuba/test'`.
6
+ * Capybara available via `require 'cuba/capybara'`.
7
+ * Use a default hash for the render plugin.
8
+ * Allow the use of custom Request and Response objects.
7
9
 
8
10
  3.0.0
9
11
 
data/README.md CHANGED
@@ -198,6 +198,30 @@ on get, "api" do ... end
198
198
  Actually, `get` is syntax sugar for `req.get?`, which in turn is syntax sugar
199
199
  for `env["REQUEST_METHOD"] == "GET"`.
200
200
 
201
+ Request and Response
202
+ --------------------
203
+
204
+ You may have noticed we use `req` and `res` a lot. Those variables are
205
+ instances of [Rack::Request][request] and `Cuba::Response` respectively, and
206
+ `Cuba::Response` is just an optimized version of
207
+ [Rack::Response][response].
208
+
209
+ [request]: http://rack.rubyforge.org/doc/classes/Rack/Request.html
210
+ [response]: http://rack.rubyforge.org/doc/classes/Rack/Response.html
211
+
212
+ Those objects are helpers for accessing the request and for building
213
+ the response. Most of the time, you will just use `req.write`.
214
+
215
+ If you want to use custom `Request` or `Response` objects, you can
216
+ set the new values as follows:
217
+
218
+ ``` ruby
219
+ Cuba.settings[:req] = MyRequest
220
+ Cuba.settings[:res] = MyResponse
221
+ ```
222
+
223
+ Make sure to provide classes compatible with those from Rack.
224
+
201
225
  Captures
202
226
  --------
203
227
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cuba"
3
- s.version = "3.1.0.rc1"
3
+ s.version = "3.1.0.rc2"
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"]
@@ -7,7 +7,9 @@ class Cuba
7
7
 
8
8
  attr :headers
9
9
 
10
- def initialize(status = 200, headers = { "Content-Type" => "text/html; charset=utf-8" })
10
+ def initialize(status = 200,
11
+ headers = { "Content-Type" => "text/html; charset=utf-8" })
12
+
11
13
  @status = status
12
14
  @headers = headers
13
15
  @body = []
@@ -112,15 +114,15 @@ class Cuba
112
114
 
113
115
  def call!(env)
114
116
  @env = env
115
- @req = Rack::Request.new(env)
116
- @res = Cuba::Response.new
117
+ @req = settings[:req].new(env)
118
+ @res = settings[:res].new
117
119
 
118
120
  # This `catch` statement will either receive a
119
121
  # rack response tuple via a `halt`, or will
120
122
  # fall back to issuing a 404.
121
123
  #
122
124
  # When it `catch`es a throw, the return value
123
- # of this whole `_call` method will be the
125
+ # of this whole `call!` method will be the
124
126
  # rack response tuple, which is exactly what we want.
125
127
  catch(:halt) do
126
128
  instance_eval(&@blk)
@@ -195,12 +197,14 @@ class Cuba
195
197
  private :try
196
198
 
197
199
  def consume(pattern)
198
- return unless match = env["PATH_INFO"].match(/\A\/(#{pattern})((?:\/|\z))/)
200
+ matchdata = env["PATH_INFO"].match(/\A\/(#{pattern})((?:\/|\z))/)
201
+
202
+ return false unless matchdata
199
203
 
200
- path, *vars = match.captures
204
+ path, *vars = matchdata.captures
201
205
 
202
206
  env["SCRIPT_NAME"] += "/#{path}"
203
- env["PATH_INFO"] = "#{vars.pop}#{match.post_match}"
207
+ env["PATH_INFO"] = "#{vars.pop}#{matchdata.post_match}"
204
208
 
205
209
  captures.push(*vars)
206
210
  end
@@ -264,8 +268,11 @@ class Cuba
264
268
  # end
265
269
  def accept(mimetype)
266
270
  lambda do
267
- String(env["HTTP_ACCEPT"]).split(",").any? { |s| s.strip == mimetype } and
271
+ accept = String(env["HTTP_ACCEPT"]).split(",")
272
+
273
+ if accept.any? { |s| s.strip == mimetype }
268
274
  res["Content-Type"] = mimetype
275
+ end
269
276
  end
270
277
  end
271
278
 
@@ -325,3 +332,6 @@ class Cuba
325
332
  throw :halt, response
326
333
  end
327
334
  end
335
+
336
+ Cuba.settings[:req] = Rack::Request
337
+ Cuba.settings[:res] = Cuba::Response
@@ -85,3 +85,30 @@ test "reset and use" do
85
85
  assert "2" == env["m.second"]
86
86
  assert "3" == env["m.block"]
87
87
  end
88
+
89
+ test "custom response" do
90
+ class MyResponse < Cuba::Response
91
+ def foobar
92
+ write "Default"
93
+ end
94
+ end
95
+
96
+ Cuba.settings[:res] = MyResponse
97
+
98
+ Cuba.define do
99
+ on get do
100
+ on "hello" do
101
+ res.foobar
102
+ end
103
+ end
104
+ end
105
+
106
+ env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/hello",
107
+ "SCRIPT_NAME" => "/" }
108
+
109
+ status, headers, resp = Cuba.call(env)
110
+
111
+ assert 200 == status
112
+ assert "text/html; charset=utf-8" == headers["Content-Type"]
113
+ assert_response resp, ["Default"]
114
+ end
@@ -1,8 +1,8 @@
1
1
  require File.expand_path("helper", File.dirname(__FILE__))
2
2
 
3
- test "settings is an empty hash by default" do
4
- assert Cuba.settings.kind_of?(Hash)
5
- assert Cuba.settings.empty?
3
+ test "settings contains request and response classes by default" do
4
+ assert_equal Cuba.settings[:req], Rack::Request
5
+ assert_equal Cuba.settings[:res], Cuba::Response
6
6
  end
7
7
 
8
8
  test "is inheritable and allows overriding" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuba
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0.rc1
4
+ version: 3.1.0.rc2
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-03 00:00:00.000000000 Z
12
+ date: 2012-07-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &2152007660 !ruby/object:Gem::Requirement
16
+ requirement: &2151834200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152007660
24
+ version_requirements: *2151834200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: cutest
27
- requirement: &2152007040 !ruby/object:Gem::Requirement
27
+ requirement: &2151832280 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2152007040
35
+ version_requirements: *2151832280
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: capybara
38
- requirement: &2152006120 !ruby/object:Gem::Requirement
38
+ requirement: &2151830340 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152006120
46
+ version_requirements: *2151830340
47
47
  description: Cuba is a microframework for web applications.
48
48
  email:
49
49
  - michel@soveran.com