Module: Belt::Helpers::Response

Included in:
LambdaHandler, BeltController::Base
Defined in:
lib/belt/helpers/response.rb

Constant Summary collapse

MAX_REQUEST_BODY_SIZE =

Maximum request body size (10 MB). Bodies larger than this are rejected to prevent memory exhaustion attacks on Lambda functions.

10 * 1024 * 1024

Instance Method Summary collapse

Instance Method Details

#cors_headers(event = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/belt/helpers/response.rb', line 14

def cors_headers(event = nil)
  event = @event if event.nil? && instance_variable_defined?(:@event)
  origin = CorsOrigin.resolve_origin(CorsOrigin.origin_from_event(event))
  allow_headers = 'Content-Type,Accept,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'
  headers = {
    'Access-Control-Allow-Headers' => allow_headers,
    'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,PATCH,OPTIONS',
    'Access-Control-Max-Age' => '300',
    'Content-Type' => 'application/json',
    'X-Content-Type-Options' => 'nosniff',
    'Vary' => 'Origin'
  }
  headers['Access-Control-Allow-Origin'] = origin if origin
  headers
end

#error_response(message, status_code = 400, error_details = nil) ⇒ Object

JSON error. Status accepts Integer or Symbol (:unprocessable_entity, :not_found, …).



36
37
38
39
40
41
42
# File 'lib/belt/helpers/response.rb', line 36

def error_response(message, status_code = 400, error_details = nil)
  body = { error: message }
  if error_details
    body[:details] = error_details.is_a?(Hash) ? error_details : { message: error_details.to_s }
  end
  { statusCode: resolve_status(status_code), headers: cors_headers, body: JSON.generate(body) }
end

#handle_error_and_respond(error, message, context = {}, status_code = 500) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/belt/helpers/response.rb', line 80

def handle_error_and_respond(error, message, context = {}, status_code = 500)
  log_error(message, error, context)
  if verbose_errors_enabled?
    error_details = {
      type: error.class.name, message: error.message,
      backtrace: ErrorLogging.filter_backtrace(error.backtrace || []),
      environment: ENV.fetch('ENVIRONMENT', nil)
    }
    error_details[:context] = context unless context.empty?
    error_response(message, status_code, error_details)
  else
    error_response(message, status_code)
  end
end

#head(status = :no_content) ⇒ Object

Empty-body response (Rails-style). Useful for destroy / create-without-body.

head :created      # 201, empty body
head :no_content   # 204
head 201


63
64
65
# File 'lib/belt/helpers/response.rb', line 63

def head(status = :no_content)
  { statusCode: resolve_status(status), headers: cors_headers, body: '' }
end

#html_response(html, status_code = 200) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/belt/helpers/response.rb', line 44

def html_response(html, status_code = 200)
  event = @event if instance_variable_defined?(:@event)
  origin = CorsOrigin.resolve_origin(CorsOrigin.origin_from_event(event))
  headers = {
    'Content-Type' => 'text/html; charset=utf-8',
    'X-Content-Type-Options' => 'nosniff',
    'X-Frame-Options' => 'DENY',
    'Referrer-Policy' => 'strict-origin-when-cross-origin'
  }
  headers['Access-Control-Allow-Origin'] = origin if origin
  { statusCode: resolve_status(status_code), headers: headers, body: html }
end

#response_status(status) ⇒ Object

Set the status used by implicit assigns / HTML render (when you don't call success_response / render yourself).

def create
@post = Post.create!(...)
response_status :created
end
# → 201 + { post: {...} }


76
77
78
# File 'lib/belt/helpers/response.rb', line 76

def response_status(status)
  @__response_status = resolve_status(status)
end

#success_response(body, status_code = 200) ⇒ Object

JSON success. Status accepts Integer or Symbol (:created, :ok, …).



31
32
33
# File 'lib/belt/helpers/response.rb', line 31

def success_response(body, status_code = 200)
  { statusCode: resolve_status(status_code), headers: cors_headers, body: JSON.generate(body) }
end