class Rswag::Ui::Middleware

Public Class Methods

new(app, config) click to toggle source
Calls superclass method
# File lib/rswag/ui/middleware.rb, line 5
def initialize(app, config)
  @config = config
  super(app, urls: [ '' ], root: config.assets_root )
end

Public Instance Methods

call(env) click to toggle source
Calls superclass method
# File lib/rswag/ui/middleware.rb, line 10
def call(env)
  if base_path?(env) 
    redirect_uri = env['SCRIPT_NAME'].chomp('/') + '/index.html'
    return [ 301, { 'Location' => redirect_uri }, [ ] ]
  end

  if index_path?(env)
    return [ 200, { 'Content-Type' => 'text/html' }, [ render_template ] ]
  end

  super
end

Private Instance Methods

base_path?(env) click to toggle source
# File lib/rswag/ui/middleware.rb, line 25
def base_path?(env)
  env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/"
end
index_path?(env) click to toggle source
# File lib/rswag/ui/middleware.rb, line 29
def index_path?(env)
  env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/index.html"
end
render_template() click to toggle source
# File lib/rswag/ui/middleware.rb, line 33
def render_template
  file = File.new(template_filename)
  template = ERB.new(file.read)
  template.result(@config.get_binding)
end
template_filename() click to toggle source
# File lib/rswag/ui/middleware.rb, line 39
def template_filename
  @config.template_locations.find { |filename| File.exists?(filename) }
end