Class: CompositionEngine::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/composition_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_app) ⇒ Middleware

Returns a new instance of Middleware.



27
28
29
# File 'lib/composition_engine.rb', line 27

def initialize(_app)
  self.app = _app
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



25
26
27
# File 'lib/composition_engine.rb', line 25

def app
  @app
end

#envObject

Returns the value of attribute env.



25
26
27
# File 'lib/composition_engine.rb', line 25

def env
  @env
end

Instance Method Details

#call(_env) ⇒ Object



31
32
33
34
# File 'lib/composition_engine.rb', line 31

def call(_env)
  self.env = _env
  call_override || app.call(_env)
end

#call_overrideObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/composition_engine.rb', line 36

def call_override
  case scoped_path
    when false
      LoginHandler.(env) #This returns false and defaults control to the next middleware if they're already logged in
    when '/inspect'
      [200,{'Content-Type' => 'text/plain'},[self.env.keys.map{|k| "#{k.inspect}: #{self.env[k].inspect}"}.join("\n")]]
    when /^#{LoginHandler.resource_path}/
      LoginHandler.handle(env)
    else
      [404,{'ContentType' => 'text/plain'},['Not Found']]
  end
end

#scoped_pathObject



49
50
51
52
53
54
# File 'lib/composition_engine.rb', line 49

def scoped_path
  return false if !(env['PATH_INFO'] =~ /^#{PATH_PREFIX}(\/.*)?$/)

  return '/' if $1.nil?
  return $1
end