Sha256: 5e3ae7d9f1697167b15a7b8df5e65482e1b5a50816e207f6c2b2a67cb76f9d40

Contents?: true

Size: 1.75 KB

Versions: 27

Compression:

Stored size: 1.75 KB

Contents

require "rack/utils"

# Special renderer.  All the other renderers lead here
module Jets::Controller::Renderers
  class RackRenderer < BaseRenderer
    # Example response:
    #
    #   [200, {"my-header" = > "value" }, "my body" ]
    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

    # maps:
    #   :continue => 100
    #   :success => 200
    #   etc
    def map_status_code(code)
      if code.is_a?(Symbol)
        Rack::Utils::SYMBOL_TO_STATUS_CODE[code]
      else
        code
      end
    end

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

    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
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
jets-1.5.2 lib/jets/controller/renderers/rack_renderer.rb
jets-1.5.1 lib/jets/controller/renderers/rack_renderer.rb
jets-1.5.0 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.11 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.10 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.9 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.8 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.7 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.6 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.5 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.4 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.3 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.2 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.1 lib/jets/controller/renderers/rack_renderer.rb
jets-1.4.0 lib/jets/controller/renderers/rack_renderer.rb
jets-1.3.9 lib/jets/controller/renderers/rack_renderer.rb
jets-1.3.8 lib/jets/controller/renderers/rack_renderer.rb
jets-1.3.7 lib/jets/controller/renderers/rack_renderer.rb
jets-1.3.6 lib/jets/controller/renderers/rack_renderer.rb
jets-1.3.5 lib/jets/controller/renderers/rack_renderer.rb