Class: Jets::Controller::Middleware::Main

Inherits:
Object
  • Object
show all
Includes:
ExceptionReporting
Defined in:
lib/jets/controller/middleware/main.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Main

Returns a new instance of Main.



14
15
16
17
18
19
20
# File 'lib/jets/controller/middleware/main.rb', line 14

def initialize(env)
  @env = env
  @controller = env['jets.controller'] # original controller instance from handler or mimic
  @context = env['jets.context']  # original AWS Lambda event or mimic context
  @event = env['jets.event']      # original AWS Lambda event or mimic event
  @meth = env['jets.meth']
end

Class Method Details

.call(env) ⇒ Object



56
57
58
59
# File 'lib/jets/controller/middleware/main.rb', line 56

def self.call(env)
  instance = new(env)
  instance.call
end

Instance Method Details

#callObject



22
23
24
# File 'lib/jets/controller/middleware/main.rb', line 22

def call
  dup.call!
end

#call!Object

With exception reporting here instead of Controller::Base#process so any exception in the middleware stack is caught. Also using with_exception_reporting instead of prepend Jets::ExceptionReporting::Process because the method is call! not process. The interface is different.



31
32
33
34
35
36
# File 'lib/jets/controller/middleware/main.rb', line 31

def call!
  with_exception_reporting do
    setup
    @controller.dispatch! # Returns triplet
  end
end

#jets_hostObject



50
51
52
53
54
# File 'lib/jets/controller/middleware/main.rb', line 50

def jets_host
  protocol = @event.dig('headers', 'X-Forwarded-Proto') || @env['rack.url_scheme']
  default = "#{protocol}://#{@env['HTTP_HOST']}"
  Jets.config.helpers.host || default
end

#setupObject

Common setup logical at this point of middleware processing right before calling any controller actions.



40
41
42
43
44
45
46
47
48
# File 'lib/jets/controller/middleware/main.rb', line 40

def setup
  # We already recreated a mimic rack env earlier as part of the very first
  # middleware layer. However, by the time the rack env reaches the main middleware
  # it could had been updated by other middlewares. We update the env here again.
  @controller.request.set_env!(@env)
  # This allows sesison helpers to work. Sessions are managed by
  # the Rack::Session::Cookie middleware by default.
  @controller.session = @env['rack.session'] || {}
end