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

Inherits:
Object
  • Object
show all
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.



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

def initialize(env)
  @env = env
  @controller = env['jets.controller']
  @event = env['lambda.event']
  @context = env['lambda.context']
  @meth = env['lambda.meth']
end

Class Method Details

.call(env) ⇒ Object



48
49
50
51
# File 'lib/jets/controller/middleware/main.rb', line 48

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

Instance Method Details

#callObject



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

def call
  ENV['JETS_HOST'] = jets_host # Dirty to use what's essentially a global variable but unsure how else to achieve
  dup.call!
end

#call!Object



25
26
27
28
# File 'lib/jets/controller/middleware/main.rb', line 25

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

#jets_hostObject



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

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.



32
33
34
35
36
37
38
39
40
# File 'lib/jets/controller/middleware/main.rb', line 32

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