Module: Capsium::Reactor::Responses
- Included in:
- Capsium::Reactor, ContentApi, DataApi, Endpoints, GraphqlApi, Serving
- Defined in:
- lib/capsium/reactor/responses.rb,
sig/capsium/reactor/responses.rbs
Overview
Plain HTTP response writers shared by the reactor dispatch, the route serving and the API handlers (data, GraphQL, save).
Instance Method Summary collapse
-
#respond_error(response, status, message, messages: nil) ⇒ Object
A JSON error body ("error" plus optional "messages" details) — the shape the data/write APIs answer client errors with.
- #respond_forbidden(response) ⇒ Object
- #respond_json(response, status, body) ⇒ Object
- #respond_method_not_allowed(response) ⇒ Object
- #respond_not_found(response) ⇒ Object
- #respond_not_implemented(response) ⇒ Object
- #respond_text(response, status, body) ⇒ Object
Instance Method Details
#respond_error(response, status, message, messages: nil) ⇒ Object
A JSON error body ("error" plus optional "messages" details) — the shape the data/write APIs answer client errors with.
34 35 36 37 38 |
# File 'lib/capsium/reactor/responses.rb', line 34 def respond_error(response, status, , messages: nil) body = { error: } body[:messages] = if respond_json(response, status, body) end |
#respond_forbidden(response) ⇒ Object
14 |
# File 'lib/capsium/reactor/responses.rb', line 14 def respond_forbidden(response) = respond_text(response, 403, "Forbidden") |
#respond_json(response, status, body) ⇒ Object
26 27 28 29 30 |
# File 'lib/capsium/reactor/responses.rb', line 26 def respond_json(response, status, body) response.status = status response["Content-Type"] = "application/json" response.body = JSON.generate(body) end |
#respond_method_not_allowed(response) ⇒ Object
18 |
# File 'lib/capsium/reactor/responses.rb', line 18 def respond_method_not_allowed(response) = respond_text(response, 405, "Method Not Allowed") |
#respond_not_found(response) ⇒ Object
12 |
# File 'lib/capsium/reactor/responses.rb', line 12 def respond_not_found(response) = respond_text(response, 404, "Not Found") |
#respond_not_implemented(response) ⇒ Object
16 |
# File 'lib/capsium/reactor/responses.rb', line 16 def respond_not_implemented(response) = respond_text(response, 501, "Not Implemented") |
#respond_text(response, status, body) ⇒ Object
20 21 22 23 24 |
# File 'lib/capsium/reactor/responses.rb', line 20 def respond_text(response, status, body) response.status = status response["Content-Type"] = "text/plain" response.body = body end |