Sha256: 80ab376dde69ebd767427a7a2355e43fd59a97047de211b76a3bf8688576b88b

Contents?: true

Size: 898 Bytes

Versions: 1

Compression:

Stored size: 898 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 = "#{base_url}/swagger_doc"

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

      def base_url
        @base_url ||= "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
napa-ext-0.0.5 lib/napa/ext/swagger_doc.rb