lib/cuba.rb in cuba-3.9.2 vs lib/cuba.rb in cuba-3.9.3
- old
+ new
@@ -1,5 +1,6 @@
+require "delegate"
require "rack"
class Cuba
SLASH = "/".freeze
EMPTY = "".freeze
@@ -8,10 +9,16 @@
REGEXES = Hash.new { |h, pattern| h[pattern] = /\A\/(#{pattern})(\/|\z)/ }
class Response
LOCATION = "Location".freeze
+ module ContentType
+ HTML = "text/html".freeze # :nodoc:
+ TEXT = "text/plain".freeze # :nodoc:
+ JSON = "application/json".freeze # :nodoc:
+ end
+
attr_accessor :status
attr :body
attr :headers
@@ -34,9 +41,27 @@
s = str.to_s
@length += s.bytesize
@headers[Rack::CONTENT_LENGTH] = @length.to_s
@body << s
+ end
+
+ # Write response body as text/plain
+ def text(str)
+ @headers[Rack::CONTENT_TYPE] = ContentType::TEXT
+ write(str)
+ end
+
+ # Write response body as text/html
+ def html(str)
+ @headers[Rack::CONTENT_TYPE] = ContentType::HTML
+ write(str)
+ end
+
+ # Write response body as application/json
+ def json(str)
+ @headers[Rack::CONTENT_TYPE] = ContentType::JSON
+ write(str)
end
def redirect(path, status = 302)
@headers[LOCATION] = path
@status = status