Swagger UI as Rails Engine for grape-swagger gem
Add this line to your application's Gemfile:
gem 'grape-swagger-rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install grape-swagger-rails
Add this line to ./config/routes.rb
:
mount GrapeSwaggerRails::Engine => '/swagger'
Create an initializer (e.g. ./config/initializers/swagger.rb
) and specify the URL to your Swagger API schema and app:
GrapeSwaggerRails.options.url = '/swagger_doc.json'
GrapeSwaggerRails.options.app_url = 'http://swagger.wordnik.com'
You can set the app name, default is "Swagger".
GrapeSwaggerRails.options.app_name = 'Swagger'
You can specify additional headers to add to each request:
GrapeSwaggerRails.options.headers['Special-Header'] = 'Some Secret Value'
Using the headers
option above, you could hard-code Basic Authentication credentials.
Alternatively, you can configure Basic Authentication through the UI, as described below.
If your application uses Basic Authentication, you can setup Swagger to send the username and password to the server with each request to your API:
GrapeSwaggerRails.options.api_auth = 'basic' # Or 'bearer' for OAuth
GrapeSwaggerRails.options.api_key_name = 'Authorization'
GrapeSwaggerRails.options.api_key_type = 'header'
Now you can specify the username and password to your API in the Swagger "API key" field by concatenating the values like this:
username:password
The javascript that loads on the Swagger page automatically encodes the username and password and adds the authorization header to your API request. See the official Swagger documentation about Custom Header Parameters
If your application uses token authentication passed as a query param, you can setup Swagger to send the API token along with each request to your API:
GrapeSwaggerRails.options.api_key_name = 'api_token'
GrapeSwaggerRails.options.api_key_type = 'query'
You can use the api_key
input box to fill in your API token.
You may want to authenticate users before displaying the Swagger UI, particularly when the API is protected by Basic Authentication.
Use the before
option to inspect the request before Swagger UI:
GrapeSwaggerRails.options.before_filter do |request|
# 1. Inspect the `request` or access the Swagger UI controller via `self`.
# 2. Check `current_user` or `can? :access, :api`, etc.
# 3. Redirect or error in case of failure.
end
To update Swagger UI from its distribution, run 'bundle exec rake swagger_ui:dist:update'. Examine the changes carefully.
##Contributors
- unloved (https://github.com/unloved)
- dapi (https://github.com/dapi)
- joelvh (https://github.com/joelvh)
- dblock (https://github.com/dblock)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request