Skip to content

Commit c8f94f1

Browse files
authored
Add Swagger documentation (#46)
* Adds Swagger documentation * Add Swagger spec
1 parent a6f63a3 commit c8f94f1

File tree

11 files changed

+55
-1
lines changed

11 files changed

+55
-1
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ gem 'bootsnap', require: false
4747
# gem "image_processing", "~> 1.2"
4848

4949
gem 'grape', '~> 1.7'
50+
gem 'grape-swagger'
5051

5152
group :development, :test do
5253
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ GEM
118118
mustermann-grape (~> 1.0.0)
119119
rack (>= 1.3.0)
120120
rack-accept
121+
grape-swagger (1.6.0)
122+
grape (~> 1.3)
121123
i18n (1.12.0)
122124
concurrent-ruby (~> 1.0)
123125
importmap-rails (1.1.1)
@@ -297,6 +299,7 @@ DEPENDENCIES
297299
capybara
298300
debug
299301
grape (~> 1.7)
302+
grape-swagger
300303
importmap-rails
301304
jbuilder
302305
pg (~> 1.1)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ bundle install
1919
rails s
2020
```
2121

22-
Try http://localhost:3000/api/ping or http://localhost:3000/api/protected/ping with _username_ and _password_.
22+
- Try http://localhost:3000/api/ping or http://localhost:3000/api/protected/ping with _username_ and _password_.
23+
- View Swagger docs at http://localhost:3000/swagger.

app/api/acme/post.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module Acme
22
class Post < Grape::API
33
desc 'Creates a spline that can be reticulated.'
4+
params do
5+
optional :reticulated, type: Boolean, documentation: { param_type: 'body' }
6+
end
47
resource :spline do
58
post do
69
{ reticulated: params[:reticulated] }

app/api/api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ class API < Grape::API
66
mount Acme::Protected
77
mount Acme::Post
88
mount Acme::Headers
9+
add_swagger_documentation info: { title: 'grape-on-rails' }
910
end

gemfiles/rails_6.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
44

55
gem 'bootsnap', require: false
66
gem 'grape', '~> 1.7'
7+
gem 'grape-swagger'
78
gem 'importmap-rails'
89
gem 'jbuilder'
910
gem 'pg', '~> 1.1'

gemfiles/rails_6_1.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
44

55
gem 'bootsnap', require: false
66
gem 'grape', '~> 1.7'
7+
gem 'grape-swagger'
78
gem 'importmap-rails'
89
gem 'jbuilder'
910
gem 'pg', '~> 1.1'

gemfiles/rails_7.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
44

55
gem 'bootsnap', require: false
66
gem 'grape', '~> 1.7'
7+
gem 'grape-swagger'
78
gem 'importmap-rails'
89
gem 'jbuilder'
910
gem 'pg', '~> 1.1'

gemfiles/rails_edge.gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
44

55
gem 'bootsnap', require: false
66
gem 'grape', '~> 1.7'
7+
gem 'grape-swagger'
78
gem 'importmap-rails'
89
gem 'jbuilder'
910
gem 'pg', '~> 1.1'

public/swagger/index.html

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="description" content="SwaggerUI" />
8+
<title>SwaggerUI</title>
9+
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swagger-ui.css" />
10+
</head>
11+
12+
<body>
13+
<div id="swagger-ui"></div>
14+
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin></script>
15+
<script src="https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js" crossorigin></script>
16+
<script>
17+
window.onload = () => {
18+
window.ui = SwaggerUIBundle({
19+
url: '/api/swagger_doc',
20+
dom_id: '#swagger-ui',
21+
presets: [
22+
SwaggerUIBundle.presets.apis,
23+
SwaggerUIStandalonePreset
24+
],
25+
layout: "StandaloneLayout",
26+
});
27+
};
28+
</script>
29+
</body>
30+
31+
</html>

0 commit comments

Comments
 (0)