Class: Jets::Controller::Renderers::RackRenderer

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

Instance Attribute Summary

Attributes inherited from BaseRenderer

#controller

Instance Method Summary collapse

Methods inherited from BaseRenderer

#initialize

Constructor Details

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

Instance Method Details

#cors_headersObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jets/controller/renderers/rack_renderer.rb', line 43

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


29
30
31
32
33
34
35
# File 'lib/jets/controller/renderers/rack_renderer.rb', line 29

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



37
38
39
40
41
# File 'lib/jets/controller/renderers/rack_renderer.rb', line 37

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

#renderObject

Example response:

[200, {"my-header" = > "value" }, "my body" ]


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jets/controller/renderers/rack_renderer.rb', line 9

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] || Jets::Controller::DEFAULT_CONTENT_TYPE
  # x-jets-base64 to convert this Rack triplet to a API Gateway hash structure later
  headers["x-jets-base64"] = base64 ? 'yes' : 'no' # headers values must be Strings
  body = StringIO.new(body)
  [status, headers, body] # triplet
end