Class: Cuba::Response
- Inherits:
-
Object
- Object
- Cuba::Response
- Defined in:
- lib/cuba.rb
Defined Under Namespace
Modules: ContentType
Constant Summary collapse
- LOCATION =
"Location".freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
Returns the value of attribute status.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete_cookie(key, value = {}) ⇒ Object
- #finish ⇒ Object
-
#html(str) ⇒ Object
Write response body as text/html.
-
#initialize(headers = {}) ⇒ Response
constructor
A new instance of Response.
-
#json(str) ⇒ Object
Write response body as application/json.
- #redirect(path, status = 302) ⇒ Object
- #set_cookie(key, value) ⇒ Object
-
#text(str) ⇒ Object
Write response body as text/plain.
- #write(str) ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
23 24 25 |
# File 'lib/cuba.rb', line 23 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
24 25 26 |
# File 'lib/cuba.rb', line 24 def headers @headers end |
#status ⇒ Object
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 |
#delete_cookie(key, value = {}) ⇒ Object
80 81 82 |
# File 'lib/cuba.rb', line 80 def (key, value = {}) Rack::Utils.(@headers, key, value) end |
#finish ⇒ Object
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 |
#set_cookie(key, value) ⇒ Object
76 77 78 |
# File 'lib/cuba.rb', line 76 def (key, value) Rack::Utils.(@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 |