class Cuba::Response

Constants

LOCATION

Attributes

body[R]
headers[R]
status[RW]

Public Class Methods

new(headers = {}) click to toggle source
# File lib/cuba.rb, line 25
def initialize(headers = {})
  @status  = nil
  @headers = headers
  @body    = []
  @length  = 0
end

Public Instance Methods

[](key) click to toggle source
# File lib/cuba.rb, line 32
def [](key)
  @headers[key]
end
[]=(key, value) click to toggle source
# File lib/cuba.rb, line 36
def []=(key, value)
  @headers[key] = value
end
finish() click to toggle source
# File lib/cuba.rb, line 71
def finish
  [@status, @headers, @body]
end
html(str) click to toggle source

Write response body as text/html

# File lib/cuba.rb, line 55
def html(str)
  @headers[Rack::CONTENT_TYPE] = ContentType::HTML
  write(str)
end
json(str) click to toggle source

Write response body as application/json

# File lib/cuba.rb, line 61
def json(str)
  @headers[Rack::CONTENT_TYPE] = ContentType::JSON
  write(str)
end
redirect(path, status = 302) click to toggle source
# File lib/cuba.rb, line 66
def redirect(path, status = 302)
  @headers[LOCATION] = path
  @status  = status
end
text(str) click to toggle source

Write response body as text/plain

# File lib/cuba.rb, line 49
def text(str)
  @headers[Rack::CONTENT_TYPE] = ContentType::TEXT
  write(str)
end
write(str) click to toggle source
# File lib/cuba.rb, line 40
def write(str)
  s = str.to_s

  @length += s.bytesize
  @headers[Rack::CONTENT_LENGTH] = @length.to_s
  @body << s
end