Sha256: 85e9cb6373ab1293ce6fdbf2318b242cb3e5d21c174424f7e135d68bee777363

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

module Lamby
  class RackRest < Lamby::Rack
    
    class << self

      def handle?(event)
        event.key?('httpMethod')
      end

    end

    def response(handler)
      if handler.base64_encodeable?
        { isBase64Encoded: true, body: handler.body64 }
      else
        super
      end.tap do |r|
        if cookies = handler.set_cookies
          r[:multiValueHeaders] ||= {}
          r[:multiValueHeaders]['Set-Cookie'] = cookies
        end
      end
    end

    private

    def env_base
      { ::Rack::REQUEST_METHOD => event['httpMethod'],
        ::Rack::SCRIPT_NAME => '',
        ::Rack::PATH_INFO => event['path'] || '',
        ::Rack::QUERY_STRING => query_string,
        ::Rack::SERVER_NAME => headers['Host'],
        ::Rack::SERVER_PORT => headers['X-Forwarded-Port'],
        ::Rack::SERVER_PROTOCOL => event.dig('requestContext', 'protocol') || 'HTTP/1.1',
        ::Rack::RACK_VERSION => ::Rack::VERSION,
        ::Rack::RACK_URL_SCHEME => 'https',
        ::Rack::RACK_INPUT => StringIO.new(body || ''),
        ::Rack::RACK_ERRORS => $stderr,
        LAMBDA_EVENT => event,
        LAMBDA_CONTEXT => context
      }.tap do |env|
        ct = content_type
        cl = content_length
        env['CONTENT_TYPE'] = ct if ct
        env['CONTENT_LENGTH'] = cl if cl
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
lamby-5.2.2 lib/lamby/rack_rest.rb
lamby-5.2.1 lib/lamby/rack_rest.rb
lamby_updated-5.2.2 lib/lamby/rack_rest.rb