Module: Impression::RequestExtensions::Responses

Included in:
Qeweney::Request
Defined in:
lib/impression/request_extensions/responses.rb

Overview

Response extensions for ‘Qeweney::Request`

Constant Summary collapse

TEXT_HEADERS =
{ 'Content-Type' => Qeweney::MimeTypes[:txt] }.freeze
HTML_HEADERS =
{ 'Content-Type' => Qeweney::MimeTypes[:html] }.freeze
JSON_HEADERS =
{ 'Content-Type' => Qeweney::MimeTypes[:json] }.freeze

Instance Method Summary collapse

Instance Method Details

#respond_html(html, **headers) ⇒ Object

Send an HTTP response with HTML content. The content type is set to ‘text/html`.

Parameters:

  • html (String)

    response body

  • **headers (Hash)

    additional response headers



31
32
33
34
# File 'lib/impression/request_extensions/responses.rb', line 31

def respond_html(html, **headers)
  headers = headers.empty? ? HTML_HEADERS : headers.merge(HTML_HEADERS)
  respond(html, headers)
end

#respond_json(object, **headers) ⇒ Object

Send an JSON response. The given object is converted to JSON. The content type is set to ‘application/json`.

Parameters:

  • object (any)

    object to convert to JSON

  • **headers (Hash)

    additional response headers



41
42
43
44
# File 'lib/impression/request_extensions/responses.rb', line 41

def respond_json(object, **headers)
  headers = headers.empty? ? JSON_HEADERS : headers.merge(JSON_HEADERS)
  respond(object.to_json, headers)
end

#respond_text(text, **headers) ⇒ Object

Send an HTTP response with plain text content. The content type is set to ‘text/plain`.

Parameters:

  • text (String)

    response body

  • **headers (Hash)

    additional response headers



21
22
23
24
# File 'lib/impression/request_extensions/responses.rb', line 21

def respond_text(text, **headers)
  headers = headers.empty? ? TEXT_HEADERS : headers.merge(TEXT_HEADERS)
  respond(text, headers)
end