Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rspec-rails-swagger
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open-source
rspec-rails-swagger
Commits
315e948a
Commit
315e948a
authored
Oct 01, 2016
by
andrew morton
Committed by
GitHub
Oct 01, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #15 from drewish/install-generator
Generator to handle installation
parents
2921b770
3abee580
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
46 deletions
+62
-46
README.md
README.md
+31
-46
swagger_install_generator.rb
...rators/rspec/swagger_install/swagger_install_generator.rb
+13
-0
swagger_helper.rb
...rs/rspec/swagger_install/templates/spec/swagger_helper.rb
+18
-0
No files found.
README.md
View file @
315e948a
...
...
@@ -12,68 +12,53 @@ The design of this was heavily influenced by the awesome [swagger_rails gem](htt
## Setup
-
Add the gem to your Rails app's
`Gemfile`
:
Add the gem to your Rails app's
`Gemfile`
:
```
rb
group
:development
,
:test
do
gem
'rspec-rails-swagger'
end
```
-
If you don't already have a
`spec/rails_helper.rb`
file run:
```
shell
rails generate rspec:install
Update your bundle:
```
-
Create
`spec/swagger_helper.rb`
file (eventually
[
this will become a
generator](https://github.com/drewish/rspec-rails-swagger/issues/3)):
```
rb
require
'rspec/rails/swagger'
require
'rails_helper'
RSpec
.
configure
do
|
config
|
# Specify a root directory where the generated Swagger files will be saved.
config
.
swagger_root
=
Rails
.
root
.
to_s
+
'/swagger'
# Define one or more Swagger documents and global metadata for each.
config
.
swagger_docs
=
{
'v1/swagger.json'
=>
{
swagger:
'2.0'
,
info:
{
title:
'API V1'
,
version:
'v1'
}
},
'v2/swagger.json'
=>
{
swagger:
'2.0'
,
info:
{
title:
'API V2'
,
version:
'v2'
}
}
}
end
bundle install
```
-
Use the generator to help create a Swagger request spec. Running:
If you don't have a
`spec/rails_helper.rb`
file:
```
rails generate rspec:
swagger PostsController
rails generate rspec:
install
```
will give you a nice starting point with paths and operations for each method
in the controller.
## Generate the docs
Create the
`spec/swagger_helper.rb`
file:
```
bundle exec rake swagger
rails generate rspec:swagger_install
```
## Running tests
## Documenting Your API
Now you can edit
`spec/swagger_helper.rb`
and start filling in the top level
Swagger documention, e.g. basePath,
[
definitions
](
http://swagger.io/specification/#definitionsObject
)
,
[
parameters
](
http://swagger.io/specification/#parametersDefinitionsObject
)
,
[
tags
](
http://swagger.io/specification/#tagObject
)
, etc.
You can use the generator to create a spec to documentation a controller:
The
`make_site.sh`
script will create a test site for a specific version of
Rails and run the tests:
```
RAILS_VERSION=4.2.0
./make_site.sh
rails generate rspec:swagger PostsController
```
Once the test site is created you can just re-run the tests:
That will create a
`spec/requests/posts_spec.rb`
file with the paths, operations
and some default requests filled in. With the structure in place you should only
need to add
`before`
calls to create records and then update the
`let`
s to
return the appropriate values.
## Generate the JSON
To create the Swagger JSON files use the rake task:
```
bundle exec r
spec
bundle exec r
ake swagger
```
Now you can use Swagger UI or the renderer of your choice to display the
formatted documentation.
lib/generators/rspec/swagger_install/swagger_install_generator.rb
0 → 100644
View file @
315e948a
require
'rails/generators'
module
Rspec
module
Generators
class
SwaggerInstallGenerator
<
::
Rails
::
Generators
::
Base
source_root
File
.
expand_path
(
'../templates'
,
__FILE__
)
def
copy_swagger_helper
template
'spec/swagger_helper.rb'
end
end
end
end
lib/generators/rspec/swagger_install/templates/spec/swagger_helper.rb
0 → 100644
View file @
315e948a
require
'rspec/rails/swagger'
require
'rails_helper'
RSpec
.
configure
do
|
config
|
# Specify a root directory where the generated Swagger files will be saved.
config
.
swagger_root
=
Rails
.
root
.
to_s
+
'/swagger'
# Define one or more Swagger documents and global metadata for each.
config
.
swagger_docs
=
{
'v1/swagger.json'
=>
{
swagger:
'2.0'
,
info:
{
title:
'API V1'
,
version:
'v1'
}
}
}
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment