Class: Cuba::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/cuba.rb

Constant Summary collapse

LOCATION =
"Location".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers = {}) ⇒ Response

Returns a new instance of Response.



17
18
19
20
21
22
# File 'lib/cuba.rb', line 17

def initialize(headers = {})
  @status  = nil
  @headers = headers
  @body    = []
  @length  = 0
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



12
13
14
# File 'lib/cuba.rb', line 12

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
# File 'lib/cuba.rb', line 24

def [](key)
  @headers[key]
end

#[]=(key, value) ⇒ Object



28
29
30
# File 'lib/cuba.rb', line 28

def []=(key, value)
  @headers[key] = value
end


53
54
55
# File 'lib/cuba.rb', line 53

def delete_cookie(key, value = {})
  Rack::Utils.delete_cookie_header!(@headers, key, value)
end

#finishObject



45
46
47
# File 'lib/cuba.rb', line 45

def finish
  [@status, @headers, @body]
end

#redirect(path, status = 302) ⇒ Object



40
41
42
43
# File 'lib/cuba.rb', line 40

def redirect(path, status = 302)
  @headers[LOCATION] = path
  @status  = status
end


49
50
51
# File 'lib/cuba.rb', line 49

def set_cookie(key, value)
  Rack::Utils.set_cookie_header!(@headers, key, value)
end

#write(str) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/cuba.rb', line 32

def write(str)
  s = str.to_s

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