Sha256: 418135498e8c82a325b7f956c02dc77a525f77da2ba2b7f765e0f5f6a37e3e17

Contents?: true

Size: 823 Bytes

Versions: 2

Compression:

Stored size: 823 Bytes

Contents

module Napa
  module Ext
    class SwaggerDoc
      def initialize app, api_doc_path: '/api_doc'
        @app = app
        @api_doc_path = api_doc_path
      end

      def call env
        if enabled? && api_doc?(env)
          [
            '301',
            { 'Location' => target_swagger_url(env), 'Content-Type' => 'text/html' },
            []
          ]
        else
          app.call env
        end
      end

      private

      attr_reader :app, :api_doc_path

      def api_doc? env
        env['PATH_INFO'] == api_doc_path
      end

      def enabled?
        ENV.key? 'SWAGGER_UI_URL'
      end

      def target_swagger_url env
        swagger_api_uri = env['REQUEST_URI'].gsub api_doc_path, '/swagger_doc'

        "#{ENV['SWAGGER_UI_URL']}?swagger_doc=#{swagger_api_uri}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
napa-ext-0.0.4 lib/napa/ext/swagger_doc.rb
napa-ext-0.0.3 lib/napa/ext/swagger_doc.rb