Class: OpenApi::Rswag::Api::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/open_api/rswag/api/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, config) ⇒ Middleware

Returns a new instance of Middleware.



8
9
10
11
# File 'lib/open_api/rswag/api/middleware.rb', line 8

def initialize(app, config)
  @app = app
  @config = config
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/open_api/rswag/api/middleware.rb', line 13

def call(env)
  path = env['PATH_INFO']
  filename = "#{@config.resolve_swagger_root(env)}/#{path}"

  if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
    swagger = load_json(filename)
    @config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil?

    return [
      '200',
      { 'Content-Type' => 'application/json' },
      [ JSON.dump(swagger) ]
    ]
  end

  return @app.call(env)
end