Skip to content

Commit e723871

Browse files
committed
Merge pull request #10 from wellthie/master
Updated README.md with new options
2 parents eac3dc4 + 8e6be8a commit e723871

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
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

app/views/grape_swagger_rails/application/index.html.erb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
headers: options.headers,
1717
supportedSubmitMethods: ['get', 'post', 'put', 'delete'],
1818
onComplete: function(swaggerApi, swaggerUi){
19-
if(console) {
19+
if('console' in window) {
2020
console.log("Loaded SwaggerUI")
2121
console.log(swaggerApi);
2222
console.log(swaggerUi);
2323
}
2424
$('pre code').each(function(i, e) {hljs.highlightBlock(e)});
2525
},
2626
onFailure: function(data) {
27-
if(console) {
27+
if('console' in window) {
2828
console.log("Unable to Load SwaggerUI");
2929
console.log(data);
3030
}
@@ -34,12 +34,11 @@
3434

3535
$('#input_apiKey').change(function() {
3636
var key = $('#input_apiKey')[0].value;
37-
console.log("key: " + key);
37+
3838
if(key && key.trim() != "") {
3939
if (options.api_auth == 'basic') {
4040
key = "Basic " + Base64.encode(key);
4141
}
42-
console.log("added key " + key);
4342
window.authorizations.add("key", new ApiKeyAuthorization(options.api_key_name, key, options.api_key_type));
4443
} else {
4544
window.authorizations.add("key", null);

lib/grape-swagger-rails.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ def authenticate_with(&block)
1010
mattr_accessor :options
1111

1212
self.options = Options.new(
13+
1314
url: '/swagger_doc.json',
14-
api_key_name: 'api_key',
15-
api_key_type: 'query',
16-
api_auth: '', # 'basic'
17-
headers: {},
1815
app_name: 'Swagger',
1916
app_url: 'http://swagger.wordnik.com',
17+
18+
headers: {},
19+
20+
api_auth: '', # 'basic'
21+
api_key_name: 'api_key', # 'Authorization'
22+
api_key_type: 'query', # 'header'
23+
2024
authentication_proc: nil # Proc used as a controller before filter that returns a boolean
2125
)
2226

0 commit comments

Comments
 (0)