Skip to content

Commit

Permalink
Add Swagger documentation (#46)
Browse files Browse the repository at this point in the history
* Adds Swagger documentation

* Add Swagger spec
  • Loading branch information
duffn authored Mar 26, 2023
1 parent a6f63a3 commit c8f94f1
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 1 deletion.
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

0 comments on commit c8f94f1

Please sign in to comment.