Sha256: 6e13e5b27c4998a68e3fc6e48ab103ea19894d4141b5697f75f1cc7a432b2a9f

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 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

    private

    def rack
      @rack ||= case @options[:rack]
      when :api
        Lamby::RackApi.new @event, @context
      else
        Lamby::RackAlb.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

4 entries across 4 versions & 2 rubygems

Version Path
lamby-no-rails-1.0.3 lib/lamby/handler.rb
lamby-1.0.2 lib/lamby/handler.rb
lamby-1.0.1 lib/lamby/handler.rb
lamby-1.0.0 lib/lamby/handler.rb