Sha256: 62f00cefdc076e8a96ed59d83f3bf0137158f5805bf6ae7c6a5916a17399fc95

Contents?: true

Size: 784 Bytes

Versions: 2

Compression:

Stored size: 784 Bytes

Contents

require 'json'

module Rswag3
  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 = 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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rswag3-api-0.0.2 lib/rswag3/api/middleware.rb
rswag3-api-0.0.1 lib/rswag3/api/middleware.rb