Module: Async::Background::Web::Response

Defined in:
lib/async/background/web/response.rb

Constant Summary collapse

JSON_TYPE =
'application/json; charset=utf-8'
HTML_TYPE =
'text/html; charset=utf-8'
TEXT_TYPE =
'text/plain; charset=utf-8'
JAVASCRIPT_TYPE =
'application/javascript; charset=utf-8'
CSS_TYPE =
'text/css; charset=utf-8'
EVENT_STREAM_TYPE =
'text/event-stream; charset=utf-8'
NO_STORE =
'no-store'
ASSET_CACHE =
'public, max-age=31536000, immutable'
SSE_CACHE =
'no-cache, no-transform'
BASE_SECURITY_HEADERS =
{
  'x-content-type-options' => 'nosniff',
  'referrer-policy' => 'no-referrer',
  'cross-origin-resource-policy' => 'same-origin'
}.freeze
HTML_SECURITY_HEADERS =
BASE_SECURITY_HEADERS.merge(
  'x-frame-options' => 'DENY',
  'content-security-policy' =>
    "default-src 'none'; " \
    "script-src 'self'; " \
    "style-src 'self'; " \
    "img-src 'self' data:; " \
    "connect-src 'self'; " \
    "frame-ancestors 'none'; " \
    "base-uri 'none'; " \
    "form-action 'none'"
).freeze
ERRORS =
{
  unauthorized: [401, 'unauthorized'],
  not_found: [404, 'not_found'],
  bad_request: [400, 'invalid_request'],
  unavailable: [503, 'service_unavailable'],
  internal_error: [500, 'internal_error']
}.freeze
ERROR_BODY =
ERRORS.to_h { |name, (_, code)| [name, JSON.generate(error: code).freeze] }.freeze

Class Method Summary collapse

Class Method Details

.asset_headers(content_type) ⇒ Object



57
# File 'lib/async/background/web/response.rb', line 57

def asset_headers(content_type) = headers(content_type, ASSET_CACHE)

.bad_request(message = nil) ⇒ Object



81
82
83
84
85
# File 'lib/async/background/web/response.rb', line 81

def bad_request(message = nil)
  return error_response(:bad_request) if message.nil?

  error_response(:bad_request, JSON.generate(error: 'invalid_request', message: message))
end

.headers(content_type, cache_control, security = BASE_SECURITY_HEADERS) ⇒ Object



51
52
53
# File 'lib/async/background/web/response.rb', line 51

def headers(content_type, cache_control, security = BASE_SECURITY_HEADERS)
  {'content-type' => content_type, 'cache-control' => cache_control}.merge(security)
end

.html(body) ⇒ Object



68
# File 'lib/async/background/web/response.rb', line 68

def html(body) = [200, html_headers, [body]]

.html_headersObject



56
# File 'lib/async/background/web/response.rb', line 56

def html_headers = headers(HTML_TYPE, NO_STORE, HTML_SECURITY_HEADERS)

.internal_errorObject



79
# File 'lib/async/background/web/response.rb', line 79

def internal_error = error_response(:internal_error)

.javascript(body) ⇒ Object



69
# File 'lib/async/background/web/response.rb', line 69

def javascript(body) = [200, asset_headers(JAVASCRIPT_TYPE), [body]]

.json(payload, status: 200) ⇒ Object



72
73
74
# File 'lib/async/background/web/response.rb', line 72

def json(payload, status: 200)
  [status, no_store_headers(JSON_TYPE), [JSON.generate(payload)]]
end

.no_store_headers(content_type) ⇒ Object



55
# File 'lib/async/background/web/response.rb', line 55

def no_store_headers(content_type) = headers(content_type, NO_STORE)

.not_foundObject



77
# File 'lib/async/background/web/response.rb', line 77

def not_found = error_response(:not_found)

.sse(body) ⇒ Object



67
# File 'lib/async/background/web/response.rb', line 67

def sse(body) = [200, sse_headers, body]

.sse_headersObject



59
60
61
62
63
64
65
# File 'lib/async/background/web/response.rb', line 59

def sse_headers
  {
    'content-type' => EVENT_STREAM_TYPE,
    'cache-control' => SSE_CACHE,
    'x-accel-buffering' => 'no'
  }.merge(BASE_SECURITY_HEADERS)
end

.stylesheet(body) ⇒ Object



70
# File 'lib/async/background/web/response.rb', line 70

def stylesheet(body) = [200, asset_headers(CSS_TYPE), [body]]

.unauthorizedObject



76
# File 'lib/async/background/web/response.rb', line 76

def unauthorized = error_response(:unauthorized)

.unavailableObject



78
# File 'lib/async/background/web/response.rb', line 78

def unavailable = error_response(:unavailable)