Sha256: 2ee7dadb8ce2d2c1e5cce55c5d152b6b18dc359315bd3b031142e8c488129cb7

Contents?: true

Size: 1.46 KB

Versions: 9

Compression:

Stored size: 1.46 KB

Contents

module Lamby
  class Handler

    class << self

      def call(app, event, context, options = {})
        new(app, event, context, options).call.response
      end

    end

    def initialize(app, event, context, options = {})
      @app = app
      @event = event
      @context = context
      @options = options
      @called = false
    end

    def response
      { statusCode: status,
        headers: headers,
        body: body }.merge(rack_response)
    end

    def status
      @status
    end

    def headers
      @headers
    end

    def body
      @rbody ||= ''.tap do |rbody|
        @body.each { |part| rbody << part }
      end
    end

    def call
      return self if @called
      @status, @headers, @body = call_app
      @called = true
      self
    end

    def base64_encodeable?
      @headers && (
        @headers['Content-Transfer-Encoding'] == 'binary' ||
        @headers['X-Lamby-Base64'] == '1'
      )
    end

    def body64
      Base64.strict_encode64(body)
    end

    private

    def rack
      @rack ||= case @options[:rack]
      when :rest, :api
        Lamby::RackRest.new @event, @context
      when :alb
        Lamby::RackAlb.new @event, @context
      else
        Lamby::RackHttp.new @event, @context
      end
    end

    def rack_response
      rack.response(self)
    end

    def call_app
      if Debug.on?(@event)
        Debug.call @event, @context, rack.env
      else
        @app.call rack.env
      end
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lamby-2.5.2 lib/lamby/handler.rb
lamby-2.5.1 lib/lamby/handler.rb
lamby-2.5.0 lib/lamby/handler.rb
lamby-2.2.2 lib/lamby/handler.rb
lamby-2.2.1 lib/lamby/handler.rb
lamby-2.2.0 lib/lamby/handler.rb
lamby-2.1.0 lib/lamby/handler.rb
lamby-2.0.1 lib/lamby/handler.rb
lamby-2.0.0 lib/lamby/handler.rb