Skip to content

Commit 8e6be8a

Browse files
committed
Added documentation about new config options
1 parent 4fcfd29 commit 8e6be8a

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

README.md

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Swagger UI as Rails Engine for grape-swagger gem
66

77
Add this line to your application's Gemfile:
88

9-
gem 'grape-swagger-rails'
9+
```ruby
10+
gem 'grape-swagger-rails'
11+
```
1012

1113
And then execute:
1214

@@ -16,22 +18,75 @@ Or install it yourself as:
1618

1719
$ gem install grape-swagger-rails
1820

19-
## Usage: add this line to your routes.rb
21+
## Usage
2022

21-
mount GrapeSwaggerRails::Engine => '/swagger'
23+
Add this line to `./config/routes.rb`:
2224

23-
Create `./config/initializer/swagger.rb` with lines:
25+
```ruby
26+
mount GrapeSwaggerRails::Engine => '/swagger'
27+
```
2428

25-
GrapeSwaggerRails.options.url = "/swagger_doc.json"
26-
GrapeSwaggerRails.options.app_name = 'Swagger'
27-
GrapeSwaggerRails.options.app_url = 'http://swagger.wordnik.com'
29+
Create an initializer (e.g. `./config/initializer/swagger.rb`) and specify the URL to your Swagger API schema:
30+
31+
```ruby
32+
GrapeSwaggerRails.options.url = '/swagger_doc.json'
33+
GrapeSwaggerRails.options.app_name = 'Swagger'
34+
GrapeSwaggerRails.options.app_url = 'http://swagger.wordnik.com'
35+
```
36+
37+
You can specify additional headers to add to each request:
38+
39+
```ruby
40+
GrapeSwaggerRails.options.headers['Special-Header'] = 'Some Secret Value'
41+
```
42+
43+
Using the `headers` option above, you could hard-code Basic Authentication credentials.
44+
Alternatively, you can configure Basic Authentication through the UI, as described below.
45+
46+
### Basic Authentication
47+
48+
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:
49+
50+
```ruby
51+
GrapeSwaggerRails.options.api_auth = 'basic'
52+
GrapeSwaggerRails.options.api_key_name = 'Authorization'
53+
GrapeSwaggerRails.options.api_key_type = 'header'
54+
```
55+
56+
Now you can specify the username and password to your API in the Swagger "API key" field by concatenating the values like this:
57+
58+
username:password
59+
60+
The javascript that loads on the Swagger page automatically encodes the username and password and adds the authorization header to your API request.
61+
See the official Swagger documentation about [Custom Header Parameters](https://github.com/wordnik/swagger-ui#custom-header-parameters---for-basic-auth-etc)
62+
63+
### Swagger UI Authorization
64+
65+
You may want to authenticate users before displaying the Swagger UI, particularly when the API is protected by Basic Authentication.
66+
Use the `authenticate_with` option to inspect the request to the Swagger UI:
67+
68+
```ruby
69+
GrapeSwaggerRails.options.authenticate_with do |request|
70+
# 1. Inspect the `request` or access the Swagger UI controller via `self`
71+
# 2. Check `current_user` or `can? :access, :api`, etc....
72+
# 3. return a boolean value
73+
end
74+
```
75+
76+
The block above is stored in the `authentication_proc` option:
77+
78+
```ruby
79+
GrapeSwaggerRails.options.authentication_proc: Proc.new{|request| # return a boolean value}
80+
```
2881

2982
## Known problems
3083

3184
To avoid problems with the validation parameters in `POST` request using this gem,
3285
please use the head version:
3386

34-
gem 'grape-swagger', :git=>'git://github.com/jhecking/grape-swagger.git'
87+
```ruby
88+
gem 'grape-swagger', :git=>'git://github.com/jhecking/grape-swagger.git'
89+
```
3590

3691
## Contributing
3792

0 commit comments

Comments
 (0)