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

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.

Parameters:

  • response (Object)
  • status (Integer)
  • message (String)
  • messages: (Array[String]? messages) (defaults to: nil)

Returns:

  • (Object)


34
35
36
37
38
# File 'lib/capsium/reactor/responses.rb', line 34

def respond_error(response, status, message, messages: nil)
  body = { error: message }
  body[:messages] = messages if messages
  respond_json(response, status, body)
end

#respond_forbidden(response) ⇒ Object

Parameters:

  • response (Object)

Returns:

  • (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

Parameters:

  • response (Object)
  • status (Integer)
  • body (Object)

Returns:

  • (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

Parameters:

  • response (Object)

Returns:

  • (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

Parameters:

  • response (Object)

Returns:

  • (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

Parameters:

  • response (Object)

Returns:

  • (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

Parameters:

  • response (Object)
  • status (Integer)
  • body (String)

Returns:

  • (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