Class: SwaggerUiWrapper::Rack::MiddleWare

Inherits:
Object
  • Object
show all
Defined in:
lib/swagger_ui_wrapper/rack/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
# File 'lib/swagger_ui_wrapper/rack/middleware.rb', line 7

def initialize(app, options)
  @app = app
  @api_url_base = options[:api_url_base] # http://test-server.com
  @path = options[:path] || '/swagger/'
  swagger_path = File.expand_path('../../../vendor/assets', File.dirname(__FILE__))
  @swagger_assets_handler = ::Rack::Static.new @app, root: swagger_path,
                                                    urls: ['/swagger'] #,
                                                    # index: 'v1.html' # switched to redirect instead of render
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/swagger_ui_wrapper/rack/middleware.rb', line 17

def call(env)
  if env['REQUEST_PATH'] && env['REQUEST_PATH'].start_with?(@path)
    if env['REQUEST_PATH'] == '/swagger/'
      [302, {'Content-Type' => 'text',
             'Location' => (@api_url_base || (env['rack.url_scheme'] + "://" + env["HTTP_HOST"].to_s)) + @path + 'v1.html',
             'Content-Type' => 'text/html; charset=UTF-8'}, ['302 found']]
    else
      # some hack for GRAPE:
      # _env = env.merge("QUERY_STRING" => "url=http://localhost:9292/v1/swagger_doc")
      # @swagger_assets_handler.call(_env)
      @swagger_assets_handler.call(env)
    end
  else
    @app.call(env)
  end
end