Module: Apigatewayv2Rack

Defined in:
lib/apigatewayv2_rack.rb,
lib/apigatewayv2_rack/error.rb,
lib/apigatewayv2_rack/request.rb,
lib/apigatewayv2_rack/version.rb,
lib/apigatewayv2_rack/response.rb,
lib/apigatewayv2_rack/middlewares/cloudfront_xff.rb,
lib/apigatewayv2_rack/middlewares/cloudfront_verify.rb

Defined Under Namespace

Modules: Handler, Middlewares Classes: Error, Request, Response

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.generate_handler(app, &block) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/apigatewayv2_rack.rb', line 34

def self.generate_handler(app, &block)
  m = Module.new
  m.extend(Handler)
  m.instance_variable_set(:@app, app)
  m.instance_variable_set(:@block, block)
  m
end

.handle_request(app:, event:, context:, request_options: {}, &block) ⇒ Object

Takes Rack app, Lambda event and context of API Gateway V2 event and returns a HTTP response from app as API Gateway V2 Lambda event format.

When block is given, converted Rack env will be passed to make some final modification before passing it to an app.



17
18
19
20
21
22
23
# File 'lib/apigatewayv2_rack.rb', line 17

def self.handle_request(app:, event:, context:, request_options: {}, &block)
  req = Request.new(event, context, **request_options)
  env = req.to_h
  block&.call(env, req)
  status, headers, body = app.call(env)
  Response.new(status: status, headers: headers, body: body, elb: req.elb?, multivalued: req.multivalued?).as_json
end

.handler_from_rack_config_file(path = './config.ru', &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/apigatewayv2_rack.rb', line 42

def self.handler_from_rack_config_file(path = './config.ru', &block)
  require 'rack'
  require 'rack/builder'
  app = if Rack.release[0] == '2'
    Rack::Builder.load_file(path, {})[0]
  else
    Rack::Builder.load_file(path)
  end
  generate_handler(app, &block)
end