Class: Guaraci::Response
- Inherits:
-
Object
- Object
- Guaraci::Response
- Defined in:
- lib/guaraci/response.rb
Overview
HTTP response builder for the Guaraci web framework.
It handles the conversion between high-level Ruby objects and the low-level Protocol::HTTP objects required by the HTTP server.
Instance Attribute Summary collapse
-
#body ⇒ Protocol::HTTP::Body::Buffered
readonly
The HTTP response body containing the actual content.
-
#headers ⇒ Protocol::HTTP::Headers
readonly
The HTTP response headers collection.
-
#status ⇒ Integer
readonly
The HTTP status code for this response.
Class Method Summary collapse
-
.ok {|response| ... } ⇒ Response
Create a successful HTTP response (200 OK).
Instance Method Summary collapse
-
#html(content) ⇒ Response
Write HTML content to the response body.
-
#initialize(status) ⇒ Response
constructor
Initialize a new HTTP response with the specified status code.
-
#json(content) ⇒ Response
Write JSON content to the response body.
-
#render ⇒ Protocol::HTTP::Response
Convert the response to a Protocol::HTTP::Response object.
-
#text(content) ⇒ Response
Write plain text content to the response body.
-
#write(content, content_type: "application/json") ⇒ Response
Write content to the response body with specified content type.
Constructor Details
#initialize(status) ⇒ Response
Initialize a new HTTP response with the specified status code.
Creates a new response instance with empty headers and body. The headers are initialized as Protocol::HTTP::Headers and the body starts as an empty buffered body until content is written.
90 91 92 93 94 |
# File 'lib/guaraci/response.rb', line 90 def initialize(status) @status = status @headers = Protocol::HTTP::Headers.new @body = default_body end |
Instance Attribute Details
#body ⇒ Protocol::HTTP::Body::Buffered (readonly)
60 61 62 |
# File 'lib/guaraci/response.rb', line 60 def body @body end |
#headers ⇒ Protocol::HTTP::Headers (readonly)
The HTTP response headers collection.
Headers are stored as Protocol::HTTP::Headers objects
71 72 73 |
# File 'lib/guaraci/response.rb', line 71 def headers @headers end |
#status ⇒ Integer (readonly)
The HTTP status code for this response.
Common status codes:
-
200: OK (successful request)
-
201: Created (resource created successfully)
-
400: Bad Request (client error)
-
404: Not Found (resource not found)
-
500: Internal Server Error (server error)
48 49 50 |
# File 'lib/guaraci/response.rb', line 48 def status @status end |
Class Method Details
.ok {|response| ... } ⇒ Response
Create a successful HTTP response (200 OK).
This is a convenient factory method for creating successful responses. It automatically sets the status to 200 and yields the response instance to the provided block for content configuration.
118 119 120 121 122 |
# File 'lib/guaraci/response.rb', line 118 def self.ok res = new(200) yield(res) if block_given? res end |
Instance Method Details
#html(content) ⇒ Response
Write HTML content to the response body.
Sets the Content-Type to “text/html” and writes the provided HTML string. No HTML validation or processing is performed - the content is sent as-is.
173 174 175 |
# File 'lib/guaraci/response.rb', line 173 def html(content) write(content, content_type: "text/html") end |
#json(content) ⇒ Response
Write JSON content to the response body.
Automatically serializes the provided object to JSON using JSON.dump
150 151 152 |
# File 'lib/guaraci/response.rb', line 150 def json(content) write(JSON.dump(content)) end |
#render ⇒ Protocol::HTTP::Response
Convert the response to a Protocol::HTTP::Response object.
This method transforms the Guaraci::Response into the low-level response format required by the Async::HTTP server. It ensures that all components (status, headers, body) are properly formatted for HTTP transmission.
The returned object contains:
-
version: HTTP version (automatically determined by Protocol::HTTP)
-
status: Integer HTTP status code
-
headers: Protocol::HTTP::Headers instance with all response headers
-
body: Protocol::HTTP::Body::Buffered instance containing the response content
214 215 216 |
# File 'lib/guaraci/response.rb', line 214 def render Protocol::HTTP::Response.new(nil, @status, @headers, @body) end |
#text(content) ⇒ Response
Write plain text content to the response body.
Sets the Content-Type to “text/plain” and writes the provided text.
189 190 191 |
# File 'lib/guaraci/response.rb', line 189 def text(content) write(content, content_type: "text/plain") end |
#write(content, content_type: "application/json") ⇒ Response
Write content to the response body with specified content type.
This is the base method used by all other content methods (json, html, text). It automatically converts the content to Protocol::HTTP::Body::Buffered format required by Async::HTTP and sets the appropriate Content-Type header.
135 136 137 138 139 |
# File 'lib/guaraci/response.rb', line 135 def write(content, content_type: "application/json") @headers["content-type"] = content_type @body = Protocol::HTTP::Body::Buffered.wrap(content) self end |