class Yaframework::Response

Constants

LOCATION

Attributes

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

Public Class Methods

new(body = [], status = 200, headers = { "Content-Type" => "text/html; charset=utf-8" }) click to toggle source
# File lib/yaframework/response.rb, line 16
def initialize(body = [], status = 200, headers = { "Content-Type" => "text/html; charset=utf-8" })
  @body = []
  @headers = headers
  @status = status
  @length = 0

  if body.respond_to? :to_str
    write body.to_str
  elsif body.respond_to? :each
    body.each { |i| write i.to_s }
  else
    raise TypeError, "Body must #respond_to? #to_str or #each"
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/yaframework/response.rb, line 48
def [](key)
  @headers[key]
end
[]=(key, value) click to toggle source
# File lib/yaframework/response.rb, line 52
def []=(key, value)
  @headers[key] = value
end
finish() click to toggle source
# File lib/yaframework/response.rb, line 31
def finish
  @headers[Rack::CONTENT_LENGTH] = @length.to_s unless (100..199).include?(status) || status == 204
  [status, headers, body]
end
html(str) click to toggle source
# File lib/yaframework/response.rb, line 56
def html(str)
  @headers[Rack::CONTENT_TYPE] = ContentType::HTML
  write(str)
end
json(str) click to toggle source
# File lib/yaframework/response.rb, line 66
def json(str)
  @headers[Rack::CONTENT_TYPE] = ContentType::JSON
  write(str)
end
redirect(target, status = 302) click to toggle source
# File lib/yaframework/response.rb, line 36
def redirect(target, status = 302)
  @status = status
  @headers[LOCATION] = target
end
text(str) click to toggle source
# File lib/yaframework/response.rb, line 61
def text(str)
  @headers[Rack::CONTENT_TYPE] = ContentType::TEXT
  write(str)
end
write(string) click to toggle source
# File lib/yaframework/response.rb, line 41
def write(string)
  s = string.to_s
  @length += s.bytesize
  @body << s
  nil
end