Sha256: 3860d274cf6ffdd6bb40fd2eab6fc387ba3aeb6aa764464ad245ddba561a6baf
Contents?: true
Size: 1.36 KB
Versions: 13
Compression:
Stored size: 1.36 KB
Contents
require 'json' require 'yaml' require 'rack/mime' module Rswag module Api class Middleware 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 = parse_file(filename) @config.swagger_filter.call(swagger, env) unless @config.swagger_filter.nil? mime = Rack::Mime.mime_type(::File.extname(path), 'text/plain') headers = { 'Content-Type' => mime }.merge(@config.swagger_headers || {}) body = unload_swagger(filename, swagger) return [ '200', headers, [ body ] ] end return @app.call(env) end private def parse_file(filename) if /\.ya?ml$/ === filename load_yaml(filename) else load_json(filename) end end def load_yaml(filename) YAML.safe_load(File.read(filename)) end def load_json(filename) JSON.parse(File.read(filename)) end def unload_swagger(filename, swagger) if /\.ya?ml$/ === filename YAML.dump(swagger) else JSON.dump(swagger) end end end end end
Version data entries
13 entries across 13 versions & 1 rubygems