Skip to content

Commit

Permalink
propagate block
Browse files Browse the repository at this point in the history
  • Loading branch information
sorah committed Mar 23, 2023
1 parent e81de23 commit 2df61fa
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/apigatewayv2_rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,36 @@ module Apigatewayv2Rack
def self.handle_request(app:, event:, context:, request_options: {}, &block)
req = Request.new(event, context, **request_options)
env = req.to_h
block&.call(env)
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

module Handler
attr_reader :app
def handle(event:, context:)
Apigatewayv2Rack.handle_request(event: event, context: context, app: @app)
attr_reader :app
attr_reader :block
def handle(event:, context:, &givenblock)
b = givenblock || @block
Apigatewayv2Rack.handle_request(event: event, context: context, app: @app, &b)
end
end

def self.generate_handler(app)
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

def self.handler_from_rack_config_file(path = './config.ru')
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)
generate_handler(app, &block)
end
end

0 comments on commit 2df61fa

Please sign in to comment.