class Lamby::Handler

Public Class Methods

call(app, event, context, options = {}) click to toggle source
# File lib/lamby/handler.rb, line 6
def call(app, event, context, options = {})
  new(app, event, context, options).call.response
end
new(app, event, context, options = {}) click to toggle source
# File lib/lamby/handler.rb, line 12
def initialize(app, event, context, options = {})
  @app = app
  @event = event
  @context = context
  @options = options
  @called = false
end

Public Instance Methods

base64_encodeable?(hdrs = @headers) click to toggle source
# File lib/lamby/handler.rb, line 56
def base64_encodeable?(hdrs = @headers)
  hdrs && (
    hdrs['Content-Transfer-Encoding'] == 'binary' ||
    content_encoding_compressed?(hdrs) ||
    hdrs['X-Lamby-Base64'] == '1'
  )
end
body() click to toggle source
# File lib/lamby/handler.rb, line 41
def body
  @rbody ||= ''.tap do |rbody|
    @body.each { |part| rbody << part if part }
    @body.close if @body.respond_to? :close
  end
end
body64() click to toggle source
# File lib/lamby/handler.rb, line 64
def body64
  Base64.strict_encode64(body)
end
call() click to toggle source
# File lib/lamby/handler.rb, line 48
def call
  return self if @called
  @status, @headers, @body = call_app
  set_cookies if rack?
  @called = true
  self
end
headers() click to toggle source
# File lib/lamby/handler.rb, line 30
def headers
  @headers
end
response() click to toggle source
# File lib/lamby/handler.rb, line 20
def response
  { statusCode: status,
    headers: headers,
    body: body }.merge(rack_response)
end
set_cookies() click to toggle source
# File lib/lamby/handler.rb, line 34
def set_cookies
  return @set_cookies if defined?(@set_cookies)
  @set_cookies = if @headers && @headers['Set-Cookie']
    @headers.delete('Set-Cookie').split("\n")
  end
end
status() click to toggle source
# File lib/lamby/handler.rb, line 26
def status
  @status
end

Private Instance Methods

call_app() click to toggle source
# File lib/lamby/handler.rb, line 88
def call_app
  if Debug.on?(@event)
    Debug.call @event, @context, rack.env
  elsif rack?
    @app.call rack.env
  elsif runner?
    Runner.call(@event)
  elsif lambdakiq?
    Lambdakiq.handler(@event)
  elsif event_bridge?
    Lamby.config.event_bridge_handler.call @event, @context
    [200, {}, StringIO.new('')]
  else
    [404, {}, StringIO.new('')]
  end
end
content_encoding_compressed?(hdrs) click to toggle source
# File lib/lamby/handler.rb, line 105
def content_encoding_compressed?(hdrs)
  content_encoding_header = hdrs['Content-Encoding'] || ''
  content_encoding_header.split(', ').any? { |h| ['br', 'gzip'].include?(h) }
end
event_bridge?() click to toggle source
# File lib/lamby/handler.rb, line 114
def event_bridge?
  Lamby.config.event_bridge_handler &&
    @event.key?('source') && @event.key?('detail') && @event.key?('detail-type')
end
lambdakiq?() click to toggle source
# File lib/lamby/handler.rb, line 119
def lambdakiq?
  defined?(::Lambdakiq) && ::Lambdakiq.jobs?(@event)
end
rack() click to toggle source
# File lib/lamby/handler.rb, line 70
def rack
  return @rack if defined?(@rack)
  @rack = begin
    type = rack_option
    klass = Lamby::Rack.lookup type, @event
    (klass && klass.handle?(@event)) ? klass.new(@event, @context) : false
  end
end
rack?() click to toggle source
# File lib/lamby/handler.rb, line 110
def rack?
  rack
end
rack_option() click to toggle source
# File lib/lamby/handler.rb, line 79
def rack_option
  return if ENV['LAMBY_TEST_DYNAMIC_HANDLER']
  @options[:rack]
end
rack_response() click to toggle source
# File lib/lamby/handler.rb, line 84
def rack_response
  rack? ? rack.response(self) : {}
end
runner?() click to toggle source
# File lib/lamby/handler.rb, line 123
def runner?
  Runner.handle?(@event)
end