class Lamby::Rack

Constants

HTTP_X_REQUESTID
HTTP_X_REQUEST_START
LAMBDA_CONTEXT
LAMBDA_EVENT

Attributes

context[R]
event[R]

Public Class Methods

lookup(type, event) click to toggle source
# File lib/lamby/rack.rb, line 14
def lookup(type, event)
  types[type] || types.values.detect { |t| t.handle?(event) }
end
new(event, context) click to toggle source
# File lib/lamby/rack.rb, line 30
def initialize(event, context)
  @event = event
  @context = context
end
types() click to toggle source

Order is important. REST is hardest to isolated with handle? method.

# File lib/lamby/rack.rb, line 19
def types
  { alb:  RackAlb,
    http: RackHttp,
    rest: RackRest,
    api:  RackRest }
end

Public Instance Methods

env() click to toggle source
# File lib/lamby/rack.rb, line 35
def env
  @env ||= env_base.merge!(env_headers)
end
multi_value?() click to toggle source
# File lib/lamby/rack.rb, line 43
def multi_value?
  false
end
response(_handler) click to toggle source
# File lib/lamby/rack.rb, line 39
def response(_handler)
  {}
end

Private Instance Methods

base64_encoded?() click to toggle source
# File lib/lamby/rack.rb, line 106
def base64_encoded?
  event['isBase64Encoded']
end
body() click to toggle source
# File lib/lamby/rack.rb, line 71
def body
  @body ||= if event['body'] && base64_encoded?
    Base64.decode64 event['body']
  else
    event['body']
  end
end
build_query_string() click to toggle source
# File lib/lamby/rack.rb, line 96
def build_query_string
  return if event['queryStringParameters'].nil?

  Rack::Utils.build_nested_query(
    event.fetch('queryStringParameters')
  )
    .gsub('[', '%5B')
    .gsub(']', '%5D')
end
content_length() click to toggle source
# File lib/lamby/rack.rb, line 66
def content_length
  bytesize = body.bytesize.to_s if body
  headers.delete('Content-Length') || headers.delete('content-length') || headers.delete('CONTENT_LENGTH') || bytesize
end
content_type() click to toggle source
# File lib/lamby/rack.rb, line 62
def content_type
  headers.delete('Content-Type') || headers.delete('content-type') || headers.delete('CONTENT_TYPE')
end
env_base() click to toggle source
# File lib/lamby/rack.rb, line 49
def env_base
  raise NotImplementedError
end
env_headers() click to toggle source
# File lib/lamby/rack.rb, line 53
def env_headers
  headers.transform_keys do |key|
    "HTTP_#{key.to_s.upcase.tr '-', '_'}"
  end.tap do |hdrs|
    hdrs[HTTP_X_REQUESTID] = request_id
    hdrs[HTTP_X_REQUEST_START] = "t=#{request_start}" if request_start
  end
end
headers() click to toggle source
# File lib/lamby/rack.rb, line 79
def headers
  @headers ||= event['headers'] || {}
end
query_string() click to toggle source
# File lib/lamby/rack.rb, line 83
def query_string
  @query_string ||= if event.key?('rawQueryString')
    event['rawQueryString']
  elsif event.key?('multiValueQueryStringParameters')
    query = event['multiValueQueryStringParameters'] || {}
    query.map do |key, value|
      value.map{ |v| "#{key}=#{v}" }.join('&')
    end.flatten.join('&')
  else
    build_query_string
  end
end
request_id() click to toggle source
# File lib/lamby/rack.rb, line 110
def request_id
  context.aws_request_id
end
request_start() click to toggle source
# File lib/lamby/rack.rb, line 114
def request_start
  event.dig('requestContext', 'timeEpoch') || 
    event.dig('requestContext', 'requestTimeEpoch')
end