Class: Cuba::Response

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

Defined Under Namespace

Modules: ContentType

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.



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

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Method Details

#[](key) ⇒ Object



33
34
35
# File 'lib/cuba.rb', line 33

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

#[]=(key, value) ⇒ Object



37
38
39
# File 'lib/cuba.rb', line 37

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


80
81
82
# File 'lib/cuba.rb', line 80

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

#finishObject



72
73
74
# File 'lib/cuba.rb', line 72

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

#html(str) ⇒ Object

Write response body as text/html



56
57
58
59
# File 'lib/cuba.rb', line 56

def html(str)
  @headers[Rack::CONTENT_TYPE] = ContentType::HTML
  write(str)
end

#json(str) ⇒ Object

Write response body as application/json



62
63
64
65
# File 'lib/cuba.rb', line 62

def json(str)
  @headers[Rack::CONTENT_TYPE] = ContentType::JSON
  write(str)
end

#redirect(path, status = 302) ⇒ Object



67
68
69
70
# File 'lib/cuba.rb', line 67

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


76
77
78
# File 'lib/cuba.rb', line 76

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

#text(str) ⇒ Object

Write response body as text/plain



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

def text(str)
  @headers[Rack::CONTENT_TYPE] = ContentType::TEXT
  write(str)
end

#write(str) ⇒ Object



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

def write(str)
  s = str.to_s

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