Skip to content

Commit e17d142

Browse files
authored
Add local Swagger UI (#40)
* Add local Swagger UI * Update Swagger definitions * Add basic Swagger UI test
1 parent 890fe26 commit e17d142

23 files changed

+172
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ rake routes
2929
Explore the API
3030
---------------
3131

32-
Explore the API using [Swagger UI](http://petstore.swagger.io). Run the application and point the explorer to `http://localhost:9292/api/swagger_doc` or `http://grape-on-rack.herokuapp.com/api/swagger_doc`.
32+
Explore the API using [Swagger UI](https://github.com/swagger-api/swagger-ui). Run the application and navigate to [http://locahost:9292/swagger/index.html](http://locahost:9292/swagger/index.html).
3333

3434

3535
## Examples

api/post_json.rb

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module Acme
22
class PostJson < Grape::API
33
format :json
44
desc 'Creates a spline that can be reticulated.'
5+
params do
6+
requires :reticulated, type: String, documentation: { param_type: 'body' }
7+
end
58
resource :spline do
69
post do
710
{ reticulated: params[:reticulated] }

api/post_put.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PostPut < Grape::API
1212
{ rang: result }
1313
end
1414
params do
15-
requires :count, type: Integer
15+
requires :count, type: Integer, documentation: { param_type: 'body' }
1616
end
1717
put :ring do
1818
result = (PostPut.rang += params[:count].to_i)

api/rescue_from.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ class RescueFrom < Grape::API
33
rescue_from :all do |e|
44
Rack::Response.new([e.message], 500, 'Content-type' => 'text/error')
55
end
6-
desc 'Raises an exception.'
6+
desc 'Raises an exception.' do
7+
success [{ code: 500, message: 'Error: Internal Server Error' }]
8+
end
79
get :raise do
810
raise 'Unexpected error.'
911
end

public/swagger/favicon-16x16.png

665 Bytes
Loading

public/swagger/favicon-32x32.png

628 Bytes
Loading

public/swagger/index.css

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
html {
2+
box-sizing: border-box;
3+
overflow: -moz-scrollbars-vertical;
4+
overflow-y: scroll;
5+
}
6+
7+
*,
8+
*:before,
9+
*:after {
10+
box-sizing: inherit;
11+
}
12+
13+
body {
14+
margin: 0;
15+
background: #fafafa;
16+
}

public/swagger/index.html

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!-- HTML for static distribution bundle build -->
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Swagger UI</title>
7+
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
8+
<link rel="stylesheet" type="text/css" href="index.css" />
9+
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
10+
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
11+
</head>
12+
13+
<body>
14+
<div id="swagger-ui"></div>
15+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
16+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
17+
<script src="./swagger-initializer.js" charset="UTF-8"> </script>
18+
</body>
19+
</html>

public/swagger/oauth2-redirect.html

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!doctype html>
2+
<html lang="en-US">
3+
<head>
4+
<title>Swagger UI: OAuth2 Redirect</title>
5+
</head>
6+
<body>
7+
<script>
8+
'use strict';
9+
function run () {
10+
var oauth2 = window.opener.swaggerUIRedirectOauth2;
11+
var sentState = oauth2.state;
12+
var redirectUrl = oauth2.redirectUrl;
13+
var isValid, qp, arr;
14+
15+
if (/code|token|error/.test(window.location.hash)) {
16+
qp = window.location.hash.substring(1);
17+
} else {
18+
qp = location.search.substring(1);
19+
}
20+
21+
arr = qp.split("&");
22+
arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';});
23+
qp = qp ? JSON.parse('{' + arr.join() + '}',
24+
function (key, value) {
25+
return key === "" ? value : decodeURIComponent(value);
26+
}
27+
) : {};
28+
29+
isValid = qp.state === sentState;
30+
31+
if ((
32+
oauth2.auth.schema.get("flow") === "accessCode" ||
33+
oauth2.auth.schema.get("flow") === "authorizationCode" ||
34+
oauth2.auth.schema.get("flow") === "authorization_code"
35+
) && !oauth2.auth.code) {
36+
if (!isValid) {
37+
oauth2.errCb({
38+
authId: oauth2.auth.name,
39+
source: "auth",
40+
level: "warning",
41+
message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
42+
});
43+
}
44+
45+
if (qp.code) {
46+
delete oauth2.state;
47+
oauth2.auth.code = qp.code;
48+
oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl});
49+
} else {
50+
let oauthErrorMsg;
51+
if (qp.error) {
52+
oauthErrorMsg = "["+qp.error+"]: " +
53+
(qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") +
54+
(qp.error_uri ? "More info: "+qp.error_uri : "");
55+
}
56+
57+
oauth2.errCb({
58+
authId: oauth2.auth.name,
59+
source: "auth",
60+
level: "error",
61+
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
62+
});
63+
}
64+
} else {
65+
oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl});
66+
}
67+
window.close();
68+
}
69+
70+
if (document.readyState !== 'loading') {
71+
run();
72+
} else {
73+
document.addEventListener('DOMContentLoaded', function () {
74+
run();
75+
});
76+
}
77+
</script>
78+
</body>
79+
</html>

public/swagger/swagger-initializer.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
window.onload = function() {
2+
//<editor-fold desc="Changeable Configuration Block">
3+
4+
// the following lines will be replaced by docker/configurator, when it runs in a docker-container
5+
window.ui = SwaggerUIBundle({
6+
url: "http://localhost:9292/api/swagger_doc",
7+
dom_id: '#swagger-ui',
8+
deepLinking: true,
9+
presets: [
10+
SwaggerUIBundle.presets.apis,
11+
SwaggerUIStandalonePreset
12+
],
13+
plugins: [
14+
SwaggerUIBundle.plugins.DownloadUrl
15+
],
16+
layout: "StandaloneLayout"
17+
});
18+
19+
//</editor-fold>
20+
};

public/swagger/swagger-ui-bundle.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui-bundle.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui-es-bundle-core.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui-es-bundle-core.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui-es-bundle.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui-es-bundle.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui-standalone-preset.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui-standalone-preset.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui.css

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui.css.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/swagger/swagger-ui.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/integration/grape_on_rack_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
expect(title).to eq('Page Not Found')
3030
end
3131
end
32+
context 'Swagger UI' do
33+
it 'displays Swagger UI' do
34+
visit '/swagger/index.html'
35+
expect(title).to eq('Swagger UI')
36+
end
37+
end
3238
context 'exception' do
3339
before :each do
3440
visit '/api/raise'

0 commit comments

Comments
 (0)