Method: Yake::API::DSL#respond

Defined in:
lib/yake/api.rb

#respond(status_code, body = nil, **headers) ⇒ Object

Transform to API Gateway response



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/yake/api.rb', line 34

def respond(status_code, body = nil, **headers)
  # Log response
  log = "RESPONSE [#{ status_code }] #{ body }"
  Yake.logger&.send(status_code.to_i >= 400 ? :error : :info, log)

  # Set headers
  content_length = (body&.bytesize || 0).to_s
  to_s_downcase  = -> (key) { key.to_s.downcase }
  headers = {
    'content-length' => content_length,
    **(@headers || {}),
    **headers,
  }.transform_keys(&to_s_downcase).compact

  # Send response
  { statusCode: status_code, headers: headers, body: body }.compact
end