Skip to content

Installation

Zuzanna Stolińska edited this page May 24, 2017 · 1 revision

Add to Gemfile

gem 'swagger_ui_engine'

And then run:

$ bundle

Usage

Mount

Add to your config/routes.rb

mount SwaggerUiEngine::Engine, at: "/api_docs"

You can place this route under admin_constraint or other restricted path, or configure basic HTTP authentication.

Devise auth

authenticate :user, lambda { |u| u.admin? } do
  mount SwaggerUiEngine::Engine, at: "/api_docs"
end

Basic HTTP auth

Set admin username and password in an initializer:

# config/initializers/swagger_ui_engine.rb

SwaggerUiEngine.configure do |config|
  config.admin_username = ENV['ADMIN_USERNAME']
  config.admin_password = ENV['ADMIN_PASSWORD']
end

Initialize

Versioned API documentations

Set the path of your json/yaml versioned documentations in an initializer:

# config/initializers/swagger_ui_engine.rb

SwaggerUiEngine.configure do |config|
  config.swagger_url = {
    v1: 'api/v1/swagger.yaml',
    v2: 'api/v2/swagger.yaml',
  }
end

and place your main documentation file under /public/api path.

Single API documentation

You can define your main documentation url in a hash value (same way as in the versioned documentations) or pass single string with the url:

# config/initializers/swagger_ui_engine.rb

SwaggerUiEngine.configure do |config|
  config.swagger_url = 'api/v1/swagger.yaml'
end

This is a compatibility patch for the SwaggerUiEngine gem versions <= 0.0.5