Class: Jets::Controller::Renderers::AwsProxyRenderer

Inherits:
BaseRenderer
  • Object
show all
Defined in:
lib/jets/controller/renderers/aws_proxy_renderer.rb

Instance Attribute Summary

Attributes inherited from BaseRenderer

#controller

Instance Method Summary collapse

Methods inherited from BaseRenderer

#initialize, #render_aws_proxy

Constructor Details

This class inherits a constructor from Jets::Controller::Renderers::BaseRenderer

Instance Method Details

#cors_headersObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jets/controller/renderers/aws_proxy_renderer.rb', line 55

def cors_headers
  case Jets.config.cors
  when true
    {
      "Access-Control-Allow-Origin" => "*", # Required for CORS support to work
      "Access-Control-Allow-Credentials" => "true" # Required for cookies, authorization headers with HTTPS
    }
  when Hash
    Jets.config.cors # contains Hash with Access-Control-Allow-* values
  else
    {}
  end
end

#map_status_code(code) ⇒ Object

maps:

:continue => 100
:success => 200
etc


41
42
43
44
45
46
47
# File 'lib/jets/controller/renderers/aws_proxy_renderer.rb', line 41

def map_status_code(code)
  if code.is_a?(Symbol)
    Rack::Utils::SYMBOL_TO_STATUS_CODE[code]
  else
    code
  end
end

#normalized_base64_option(options) ⇒ Object



49
50
51
52
53
# File 'lib/jets/controller/renderers/aws_proxy_renderer.rb', line 49

def normalized_base64_option(options)
  base64 = @options[:base64] if options.key?(:base64)
  base64 = @options[:isBase64Encoded] if options.key?(:isBase64Encoded)
  base64
end

#renderObject

Transform the structure to AWS_PROXY compatiable structure docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format Example response:

{
  "statusCode" => status,
  "headers" => headers,
  "body" => body,
  "isBase64Encoded" => base64,
}


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jets/controller/renderers/aws_proxy_renderer.rb', line 16

def render
  # we do some normalization here
  status = map_status_code(@options[:status]) || 200
  status = status.to_s # API Gateway requires a string but rack is okay with either
  body = @options[:body]
  base64 = normalized_base64_option(@options)

  headers = @options[:headers] || {}
  headers = cors_headers.merge(headers)
  headers["Content-Type"] ||= @options[:content_type] || "text/html; charset=utf-8"

  # Compatiable Lambda Proxy response Hash.
  # Additional extra keys results in compatiability. Explictly assign keys.
  {
    "statusCode" => status,
    "headers" => headers,
    "body" => body,
    "isBase64Encoded" => base64,
  }
end