Class: Lithium::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lithium/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' }) ⇒ Response

Returns a new instance of Response.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lithium/base.rb', line 19

def initialize(body = [], status = 200, headers = { 'Content-Type' => 'text/html; charset=utf-8' })
  @body, @headers, @status = [], headers, 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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



15
16
17
# File 'lib/lithium/base.rb', line 15

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



15
16
17
# File 'lib/lithium/base.rb', line 15

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



14
15
16
# File 'lib/lithium/base.rb', line 14

def status
  @status
end

Instance Method Details

#finishObject



32
33
34
35
36
37
# File 'lib/lithium/base.rb', line 32

def finish
  unless (100..199).include?(status) || status == 204
    headers['Content-Length'] = @length.to_s
  end
  [status, headers, body]
end

#redirect(target, status = 302) ⇒ Object



39
40
41
42
# File 'lib/lithium/base.rb', line 39

def redirect(target, status = 302)
  self.status = status
  headers['Location'] = target
end

#write(string) ⇒ Object



44
45
46
47
48
49
# File 'lib/lithium/base.rb', line 44

def write(string)
  s = string.to_s
  @length += s.bytesize

  body << s
end