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


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

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body


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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers


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

def headers
  @headers
end

#statusObject

Returns the value of attribute status


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

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object


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

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

#[]=(key, value) ⇒ Object


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

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

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

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

#finishObject


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

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

#redirect(path, status = 302) ⇒ Object


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

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

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

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

#write(str) ⇒ Object


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

def write(str)
  s = str.to_s

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