Class: Swagui::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/swagui/middleware.rb', line 7

def initialize(app, options={})
  @app = app

  @url_path = options[:url]
  @doc_path = options[:path] || @url_path
  @url_regex = Regexp.new("^#{@url_path}")
  @assets_url_regex = Regexp.new("^\/swagger-ui")

  app_doc_dir = File.expand_path(@doc_path, Dir.pwd)
  @app_file_server = Rack::File.new(app_doc_dir)
  swagger_ui_dir = File.expand_path('../../swagger-ui', File.dirname(__FILE__))
  @swagger_ui_file_server = Rack::File.new(swagger_ui_dir)
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/swagui/middleware.rb', line 21

def call(env)
  path = env["PATH_INFO"].strip

  if !(path =~ @url_regex).nil?
    path = path.gsub(@url_regex, '')
    if path == '' || path == '/'
      env["PATH_INFO"] = 'index.html'
      @swagger_ui_file_server.call(env)
    else
      env["PATH_INFO"] = path
      @app_file_server.call(env)
    end
  elsif !(path =~ @assets_url_regex).nil?
    env["PATH_INFO"] = path.gsub(@assets_url_regex, '')
    @swagger_ui_file_server.call(env)
  else
    @app.call(env)
  end
end