Skip to content

Add Swagger documentation #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ gem 'bootsnap', require: false
# gem "image_processing", "~> 1.2"

gem 'grape', '~> 1.7'
gem 'grape-swagger'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ GEM
mustermann-grape (~> 1.0.0)
rack (>= 1.3.0)
rack-accept
grape-swagger (1.6.0)
grape (~> 1.3)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
importmap-rails (1.1.1)
Expand Down Expand Up @@ -297,6 +299,7 @@ DEPENDENCIES
capybara
debug
grape (~> 1.7)
grape-swagger
importmap-rails
jbuilder
pg (~> 1.1)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ bundle install
rails s
```

Try http://localhost:3000/api/ping or http://localhost:3000/api/protected/ping with _username_ and _password_.
- Try http://localhost:3000/api/ping or http://localhost:3000/api/protected/ping with _username_ and _password_.
- View Swagger docs at http://localhost:3000/swagger.
3 changes: 3 additions & 0 deletions app/api/acme/post.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module Acme
class Post < Grape::API
desc 'Creates a spline that can be reticulated.'
params do
optional :reticulated, type: Boolean, documentation: { param_type: 'body' }
end
resource :spline do
post do
{ reticulated: params[:reticulated] }
Expand Down
1 change: 1 addition & 0 deletions app/api/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ class API < Grape::API
mount Acme::Protected
mount Acme::Post
mount Acme::Headers
add_swagger_documentation info: { title: 'grape-on-rails' }
end
1 change: 1 addition & 0 deletions gemfiles/rails_6.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source 'https://rubygems.org'

gem 'bootsnap', require: false
gem 'grape', '~> 1.7'
gem 'grape-swagger'
gem 'importmap-rails'
gem 'jbuilder'
gem 'pg', '~> 1.1'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_6_1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source 'https://rubygems.org'

gem 'bootsnap', require: false
gem 'grape', '~> 1.7'
gem 'grape-swagger'
gem 'importmap-rails'
gem 'jbuilder'
gem 'pg', '~> 1.1'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_7.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source 'https://rubygems.org'

gem 'bootsnap', require: false
gem 'grape', '~> 1.7'
gem 'grape-swagger'
gem 'importmap-rails'
gem 'jbuilder'
gem 'pg', '~> 1.1'
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ source 'https://rubygems.org'

gem 'bootsnap', require: false
gem 'grape', '~> 1.7'
gem 'grape-swagger'
gem 'importmap-rails'
gem 'jbuilder'
gem 'pg', '~> 1.1'
Expand Down
31 changes: 31 additions & 0 deletions public/swagger/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="SwaggerUI" />
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/swagger-ui.css" />
</head>

<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/[email protected]/swagger-ui-bundle.js" crossorigin></script>
<script src="https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: '/api/swagger_doc',
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout",
});
};
</script>
</body>

</html>
10 changes: 10 additions & 0 deletions spec/features/swagger_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'spec_helper'

describe 'Swagger', js: true, type: :feature do
before :each do
visit '/swagger'
end
it 'displays Swagger page' do
expect(page.find('.title')).to have_content 'grape-on-rails'
end
end