Class: Lamby::RackAlb

Inherits:
Rack
  • Object
show all
Defined in:
lib/lamby/rack_alb.rb

Constant Summary

Constants inherited from Rack

Lamby::Rack::HTTP_COOKIE, Lamby::Rack::HTTP_X_REQUESTID, Lamby::Rack::HTTP_X_REQUEST_START, Lamby::Rack::LAMBDA_CONTEXT, Lamby::Rack::LAMBDA_EVENT

Instance Attribute Summary

Attributes inherited from Rack

#context, #event

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rack

#env, #initialize, lookup, types

Constructor Details

This class inherits a constructor from Lamby::Rack

Class Method Details

.handle?(event) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
# File 'lib/lamby/rack_alb.rb', line 6

def handle?(event)
  event.key?('httpMethod') && 
    event.dig('requestContext', 'elb')
end

Instance Method Details

#alb?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/lamby/rack_alb.rb', line 13

def alb?
  true
end

#multi_value?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/lamby/rack_alb.rb', line 17

def multi_value?
  event.key? 'multiValueHeaders'
end

#response(handler) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lamby/rack_alb.rb', line 21

def response(handler)
  hhdrs = handler.headers
  if multi_value?
    multivalue_headers = hhdrs.transform_values { |v| Array[v].compact.flatten }
    multivalue_headers['Set-Cookie'] = handler.set_cookies if handler.set_cookies
  end
  status_description = "#{handler.status} #{::Rack::Utils::HTTP_STATUS_CODES[handler.status]}"
  base64_encode = handler.base64_encodeable?(hhdrs)
  body = Base64.strict_encode64(handler.body) if base64_encode
  { multiValueHeaders: multivalue_headers,
    statusDescription: status_description,
    isBase64Encoded: base64_encode,
    body: body }.compact
end