Module: Utopia::Response
- Defined in:
- lib/utopia/response.rb
Overview
Response helpers for Utopia applications.
The canonical transport response remains Protocol::HTTP::Response. This module provides convenience constructors and normalization at the application boundary.
Constant Summary collapse
- CONTENT_TYPE =
"content-type".freeze
- LOCATION =
"location".freeze
- NotFound =
Protocol::HTTP::Middleware::NotFound
Class Method Summary collapse
-
.[](status, headers = nil, body = nil, **options) ⇒ Object
Build a protocol HTTP response.
-
.html(content, status = 200, headers = {}) ⇒ Object
Build an HTML response.
-
.redirect(location, status = 302, headers = {}) ⇒ Object
Build a redirect response.
-
.text(content, status = 200, headers = {}) ⇒ Object
Build a plain text response.
-
.wrap(response) ⇒ Object
Normalize a response-like value to a protocol response.
Class Method Details
.[](status, headers = nil, body = nil, **options) ⇒ Object
Build a protocol HTTP response.
27 28 29 |
# File 'lib/utopia/response.rb', line 27 def self.[](status, headers = nil, body = nil, **) Protocol::HTTP::Response[status, headers, body, **] end |
.html(content, status = 200, headers = {}) ⇒ Object
Build an HTML response.
69 70 71 |
# File 'lib/utopia/response.rb', line 69 def self.html(content, status = 200, headers = {}) self[status, {CONTENT_TYPE => "text/html; charset=utf-8"}.merge(headers), [content]] end |
.redirect(location, status = 302, headers = {}) ⇒ Object
Build a redirect response.
51 52 53 |
# File 'lib/utopia/response.rb', line 51 def self.redirect(location, status = 302, headers = {}) self[status, headers.merge(LOCATION => location), []] end |
.text(content, status = 200, headers = {}) ⇒ Object
Build a plain text response.
60 61 62 |
# File 'lib/utopia/response.rb', line 60 def self.text(content, status = 200, headers = {}) self[status, {CONTENT_TYPE => "text/plain; charset=utf-8"}.merge(headers), [content]] end |
.wrap(response) ⇒ Object
Normalize a response-like value to a protocol response.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/utopia/response.rb', line 35 def self.wrap(response) return response if response.is_a?(Protocol::HTTP::Response) if response.respond_to?(:to_response) response = response.to_response return response if response.is_a?(Protocol::HTTP::Response) end raise TypeError, "Expected a Protocol::HTTP::Response, but got #{response.class}!" end |