lib/cuba.rb in cuba-3.1.0.rc1 vs lib/cuba.rb in cuba-3.1.0.rc2
- old
+ new
@@ -5,11 +5,13 @@
class Response
attr_accessor :status
attr :headers
- def initialize(status = 200, headers = { "Content-Type" => "text/html; charset=utf-8" })
+ def initialize(status = 200,
+ headers = { "Content-Type" => "text/html; charset=utf-8" })
+
@status = status
@headers = headers
@body = []
@length = 0
end
@@ -110,19 +112,19 @@
dup.call!(env)
end
def call!(env)
@env = env
- @req = Rack::Request.new(env)
- @res = Cuba::Response.new
+ @req = settings[:req].new(env)
+ @res = settings[:res].new
# This `catch` statement will either receive a
# rack response tuple via a `halt`, or will
# fall back to issuing a 404.
#
# When it `catch`es a throw, the return value
- # of this whole `_call` method will be the
+ # of this whole `call!` method will be the
# rack response tuple, which is exactly what we want.
catch(:halt) do
instance_eval(&@blk)
res.status = 404
@@ -193,16 +195,18 @@
env["SCRIPT_NAME"], env["PATH_INFO"] = script, path
end
private :try
def consume(pattern)
- return unless match = env["PATH_INFO"].match(/\A\/(#{pattern})((?:\/|\z))/)
+ matchdata = env["PATH_INFO"].match(/\A\/(#{pattern})((?:\/|\z))/)
- path, *vars = match.captures
+ return false unless matchdata
+ path, *vars = matchdata.captures
+
env["SCRIPT_NAME"] += "/#{path}"
- env["PATH_INFO"] = "#{vars.pop}#{match.post_match}"
+ env["PATH_INFO"] = "#{vars.pop}#{matchdata.post_match}"
captures.push(*vars)
end
private :consume
@@ -262,12 +266,15 @@
# # automatically set to application/xml.
# res.write res["Content-Type"]
# end
def accept(mimetype)
lambda do
- String(env["HTTP_ACCEPT"]).split(",").any? { |s| s.strip == mimetype } and
+ accept = String(env["HTTP_ACCEPT"]).split(",")
+
+ if accept.any? { |s| s.strip == mimetype }
res["Content-Type"] = mimetype
+ end
end
end
# Syntactic sugar for providing catch-all matches.
#
@@ -323,5 +330,8 @@
def halt(response)
throw :halt, response
end
end
+
+Cuba.settings[:req] = Rack::Request
+Cuba.settings[:res] = Cuba::Response