mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
36 lines
721 B
Ruby
36 lines
721 B
Ruby
require 'json'
|
|
|
|
module SwaggerRails
|
|
class SwaggerJson
|
|
|
|
def initialize(app, config)
|
|
@app = app
|
|
@config = config
|
|
end
|
|
|
|
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
|
|
|
|
private
|
|
|
|
def load_json(filename)
|
|
JSON.parse(File.read(filename))
|
|
end
|
|
end
|
|
end
|