rspec-rails-swagger 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +49 -0
- data/CONTRIBUTING.md +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/lib/generators/rspec/swagger/USAGE +9 -0
- data/lib/generators/rspec/swagger/swagger_generator.rb +23 -0
- data/lib/generators/rspec/swagger/templates/spec.rb +26 -0
- data/lib/generators/rspec/swagger_install/swagger_install_generator.rb +13 -0
- data/lib/generators/rspec/swagger_install/templates/spec/swagger_helper.rb +18 -0
- data/lib/rspec/rails/swagger/formatter.rb +2 -2
- data/lib/rspec/rails/swagger/helpers.rb +4 -0
- data/lib/rspec/rails/swagger/request_builder.rb +3 -3
- data/lib/rspec/rails/swagger/route_parser.rb +62 -0
- data/lib/rspec/rails/swagger/version.rb +1 -1
- data/lib/rspec/rails/swagger.rb +4 -0
- metadata +12 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a2b47ce191c99f44fb347b30d0f008598b12333
|
4
|
+
data.tar.gz: ef2a2ba21b5ae8205d5fef54ba82f41433da7315
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c6b22a14ed9e8500c79079b2df7752466ee655c6e68b12219538da0ded969bd3ed4025a912f3e2177c9ab6fcc66b25cc76abfb57fc97df470abafdd061900e5
|
7
|
+
data.tar.gz: 6ab205147771de49993331072bd1961ec81fe8a766b01adaffa7f4e13153f695900451fbffd192b906babcb798370c4a6c21a4df461a10b00e3f611e0f921b8b
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at drewish@katherinehouse.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
## Running tests
|
4
|
+
|
5
|
+
The `make_site.sh` script will create a test site for a specific version of
|
6
|
+
Rails and run the tests:
|
7
|
+
```
|
8
|
+
RAILS_VERSION=4.2.0
|
9
|
+
./make_site.sh
|
10
|
+
```
|
11
|
+
|
12
|
+
Once the test site is created you can just re-run the tests:
|
13
|
+
```
|
14
|
+
bundle exec rspec
|
15
|
+
```
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 andrew morton
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# RSpec Rails Swagger
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/drewish/rspec-rails-swagger.svg?branch=master)](https://travis-ci.org/drewish/rspec-rails-swagger)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/drewish/rspec-rails-swagger/badges/gpa.svg)](https://codeclimate.com/github/drewish/rspec-rails-swagger)
|
5
|
+
|
6
|
+
This gem helps you generate Swagger docs by using RSpec to document the paths.
|
7
|
+
You execute a command to run the tests and generate the `.json` output. Running
|
8
|
+
the tests ensures that your API and docs are in agreement, and generates output
|
9
|
+
that can be used as examples.
|
10
|
+
|
11
|
+
The design of this was heavily influenced by the awesome [swagger_rails gem](https://github.com/domaindrivendev/swagger_rails).
|
12
|
+
|
13
|
+
## Setup
|
14
|
+
|
15
|
+
Add the gem to your Rails app's `Gemfile`:
|
16
|
+
```rb
|
17
|
+
group :development, :test do
|
18
|
+
gem 'rspec-rails-swagger'
|
19
|
+
end
|
20
|
+
```
|
21
|
+
|
22
|
+
Update your bundle:
|
23
|
+
```
|
24
|
+
bundle install
|
25
|
+
```
|
26
|
+
|
27
|
+
If you don't have a `spec/rails_helper.rb` file:
|
28
|
+
```
|
29
|
+
rails generate rspec:install
|
30
|
+
```
|
31
|
+
|
32
|
+
Create the `spec/swagger_helper.rb` file:
|
33
|
+
```
|
34
|
+
rails generate rspec:swagger_install
|
35
|
+
```
|
36
|
+
|
37
|
+
## Documenting Your API
|
38
|
+
|
39
|
+
Now you can edit `spec/swagger_helper.rb` and start filling in the top level
|
40
|
+
Swagger documention, e.g. basePath, [definitions](http://swagger.io/specification/#definitionsObject),
|
41
|
+
[parameters](http://swagger.io/specification/#parametersDefinitionsObject),
|
42
|
+
[tags](http://swagger.io/specification/#tagObject), etc.
|
43
|
+
|
44
|
+
You can use the generator to create a spec to documentation a controller:
|
45
|
+
|
46
|
+
```
|
47
|
+
rails generate rspec:swagger PostsController
|
48
|
+
```
|
49
|
+
|
50
|
+
That will create a `spec/requests/posts_spec.rb` file with the paths, operations
|
51
|
+
and some default requests filled in. With the structure in place you should only
|
52
|
+
need to add `before` calls to create records and then update the `let`s to
|
53
|
+
return the appropriate values.
|
54
|
+
|
55
|
+
## Generate the JSON
|
56
|
+
|
57
|
+
To create the Swagger JSON files use the rake task:
|
58
|
+
|
59
|
+
```
|
60
|
+
bundle exec rake swagger
|
61
|
+
```
|
62
|
+
|
63
|
+
Now you can use Swagger UI or the renderer of your choice to display the
|
64
|
+
formatted documentation. [swagger_engine](https://github.com/batdevis/swagger_engine)
|
65
|
+
works pretty well and supports multiple documents.
|
@@ -0,0 +1,9 @@
|
|
1
|
+
Description:
|
2
|
+
This creates an RSpec request spec to define Swagger documentation for a
|
3
|
+
controller. It will create a test for each of the controller's methods.
|
4
|
+
|
5
|
+
Example:
|
6
|
+
rails generate rspec:swagger V3::AccountsController
|
7
|
+
|
8
|
+
This will create:
|
9
|
+
spec/requests/v3/accounts_spec.rb
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Rspec
|
4
|
+
module Generators
|
5
|
+
class SwaggerGenerator < ::Rails::Generators::NamedBase
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@routes = RSpec::Rails::Swagger::RouteParser.new(controller_path).routes
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_spec_file
|
13
|
+
template 'spec.rb', File.join('spec/requests', "#{controller_path}_spec.rb")
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def controller_path
|
19
|
+
file_path.chomp('_controller')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'swagger_helper'
|
2
|
+
|
3
|
+
RSpec.describe '<%= controller_path %>', type: :request do
|
4
|
+
<% @routes.each do | template, path_item | %>
|
5
|
+
path '<%= template %>' do
|
6
|
+
<% unless path_item[:params].empty? -%>
|
7
|
+
# You'll want to customize the parameter types...
|
8
|
+
<% path_item[:params].each do |param| -%>
|
9
|
+
parameter '<%= param %>', in: :path, type: :string
|
10
|
+
<% end -%>
|
11
|
+
# ...and values used to make the requests.
|
12
|
+
<% path_item[:params].each do |param| -%>
|
13
|
+
let(:<%= param %>) { '123' }
|
14
|
+
<% end -%>
|
15
|
+
|
16
|
+
<% end -%>
|
17
|
+
<% path_item[:actions].each do | action, details | -%>
|
18
|
+
<%= action %>(summary: '<%= details[:summary] %>') do
|
19
|
+
response(200, description: 'successful') do
|
20
|
+
# You can add before/let blocks here to trigger the response code
|
21
|
+
end
|
22
|
+
end
|
23
|
+
<% end -%>
|
24
|
+
end
|
25
|
+
<% end -%>
|
26
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Rspec
|
4
|
+
module Generators
|
5
|
+
class SwaggerInstallGenerator < ::Rails::Generators::Base
|
6
|
+
source_root File.expand_path('../templates', __FILE__)
|
7
|
+
|
8
|
+
def copy_swagger_helper
|
9
|
+
template 'spec/swagger_helper.rb'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rspec/rails/swagger'
|
2
|
+
require 'rails_helper'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
# Specify a root directory where the generated Swagger files will be saved.
|
6
|
+
config.swagger_root = Rails.root.to_s + '/swagger'
|
7
|
+
|
8
|
+
# Define one or more Swagger documents and global metadata for each.
|
9
|
+
config.swagger_docs = {
|
10
|
+
'v1/swagger.json' => {
|
11
|
+
swagger: '2.0',
|
12
|
+
info: {
|
13
|
+
title: 'API V1',
|
14
|
+
version: 'v1'
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
18
|
+
end
|
@@ -62,7 +62,7 @@ module RSpec
|
|
62
62
|
when :path_item
|
63
63
|
["\n", metadata[:swagger_path_item][:path]]
|
64
64
|
when :operation
|
65
|
-
["\n ", metadata[:swagger_operation][:method]
|
65
|
+
["\n ", "%-8s" % metadata[:swagger_operation][:method]]
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
@@ -107,7 +107,7 @@ module RSpec
|
|
107
107
|
operation[:parameters] = prepare_parameters(swagger_operation[:parameters])
|
108
108
|
end
|
109
109
|
operation.merge!(swagger_operation.slice(
|
110
|
-
:summary, :description, :externalDocs, :operationId,
|
110
|
+
:tags, :summary, :description, :externalDocs, :operationId,
|
111
111
|
:consumes, :produces, :schemes, :deprecated, :security
|
112
112
|
))
|
113
113
|
end
|
@@ -174,6 +174,10 @@ module RSpec
|
|
174
174
|
metadata[:swagger_operation][:produces] = mime_types
|
175
175
|
end
|
176
176
|
|
177
|
+
def tags *tags
|
178
|
+
metadata[:swagger_operation][:tags] = tags
|
179
|
+
end
|
180
|
+
|
177
181
|
def response status_code, attributes = {}, &block
|
178
182
|
attributes.symbolize_keys!
|
179
183
|
|
@@ -20,11 +20,11 @@ module RSpec
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def produces
|
23
|
-
metadata[:swagger_operation][:produces] || document[:produces]
|
23
|
+
Array(metadata[:swagger_operation][:produces]).presence || Array(document[:produces])
|
24
24
|
end
|
25
25
|
|
26
26
|
def consumes
|
27
|
-
metadata[:swagger_operation][:consumes] || document[:consumes]
|
27
|
+
Array(metadata[:swagger_operation][:consumes]).presence || Array(document[:consumes])
|
28
28
|
end
|
29
29
|
|
30
30
|
def parameters location = nil
|
@@ -52,7 +52,7 @@ module RSpec
|
|
52
52
|
headers = {}
|
53
53
|
|
54
54
|
# Match the names that Rails uses internally
|
55
|
-
headers['HTTP_ACCEPT'] = produces.
|
55
|
+
headers['HTTP_ACCEPT'] = produces.first if produces.present?
|
56
56
|
headers['CONTENT_TYPE'] = consumes.first if consumes.present?
|
57
57
|
|
58
58
|
# TODO: do we need to do some capitalization to match the rack
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Rails
|
3
|
+
module Swagger
|
4
|
+
class RouteParser
|
5
|
+
attr_reader :controller
|
6
|
+
|
7
|
+
def initialize(controller)
|
8
|
+
@controller = controller
|
9
|
+
end
|
10
|
+
|
11
|
+
def routes
|
12
|
+
::Rails.application.routes.routes.select do |route|
|
13
|
+
route.defaults[:controller] == controller
|
14
|
+
end.reduce({}) do |tree, route|
|
15
|
+
path = path_from(route)
|
16
|
+
verb = verb_from(route)
|
17
|
+
tree[path] ||= { params: params_from(route), actions: {} }
|
18
|
+
tree[path][:actions][verb] = { summary: summary_from(route) }
|
19
|
+
tree
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def path_from(route)
|
26
|
+
route.path.spec.to_s
|
27
|
+
.chomp('(.:format)') # Ignore any format suffix
|
28
|
+
.gsub(/:([^\/.?]+)/, '{\1}') # Convert :id to {id}
|
29
|
+
end
|
30
|
+
|
31
|
+
def verb_from(route)
|
32
|
+
verb = route.verb
|
33
|
+
if verb.kind_of? String
|
34
|
+
verb.downcase
|
35
|
+
else
|
36
|
+
verb.source.gsub(/[$^]/, '').downcase
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def summary_from(route)
|
41
|
+
verb = route.requirements[:action]
|
42
|
+
noun = route.requirements[:controller].split('/').last.singularize
|
43
|
+
|
44
|
+
# Apply a few customizations to make things more readable
|
45
|
+
case verb
|
46
|
+
when 'index'
|
47
|
+
verb = 'list'
|
48
|
+
noun = noun.pluralize
|
49
|
+
when 'destroy'
|
50
|
+
verb = 'delete'
|
51
|
+
end
|
52
|
+
|
53
|
+
"#{verb} #{noun}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def params_from(route)
|
57
|
+
route.segments - ['format']
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/rspec/rails/swagger.rb
CHANGED
@@ -4,6 +4,7 @@ require 'rspec/rails/swagger/document'
|
|
4
4
|
require 'rspec/rails/swagger/formatter'
|
5
5
|
require 'rspec/rails/swagger/helpers'
|
6
6
|
require 'rspec/rails/swagger/request_builder'
|
7
|
+
require 'rspec/rails/swagger/route_parser'
|
7
8
|
require 'rspec/rails/swagger/version'
|
8
9
|
|
9
10
|
module RSpec
|
@@ -16,6 +17,9 @@ module RSpec
|
|
16
17
|
rake_tasks do
|
17
18
|
load 'rspec/rails/swagger/tasks/swagger.rake'
|
18
19
|
end
|
20
|
+
generators do
|
21
|
+
require "generators/rspec/swagger/swagger_generator.rb"
|
22
|
+
end
|
19
23
|
end
|
20
24
|
end
|
21
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-rails-swagger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- andrew morton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -44,12 +44,22 @@ executables: []
|
|
44
44
|
extensions: []
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
|
+
- CODE_OF_CONDUCT.md
|
48
|
+
- CONTRIBUTING.md
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.md
|
51
|
+
- lib/generators/rspec/swagger/USAGE
|
52
|
+
- lib/generators/rspec/swagger/swagger_generator.rb
|
53
|
+
- lib/generators/rspec/swagger/templates/spec.rb
|
54
|
+
- lib/generators/rspec/swagger_install/swagger_install_generator.rb
|
55
|
+
- lib/generators/rspec/swagger_install/templates/spec/swagger_helper.rb
|
47
56
|
- lib/rspec/rails/swagger.rb
|
48
57
|
- lib/rspec/rails/swagger/configuration.rb
|
49
58
|
- lib/rspec/rails/swagger/document.rb
|
50
59
|
- lib/rspec/rails/swagger/formatter.rb
|
51
60
|
- lib/rspec/rails/swagger/helpers.rb
|
52
61
|
- lib/rspec/rails/swagger/request_builder.rb
|
62
|
+
- lib/rspec/rails/swagger/route_parser.rb
|
53
63
|
- lib/rspec/rails/swagger/tasks/swagger.rake
|
54
64
|
- lib/rspec/rails/swagger/version.rb
|
55
65
|
homepage: https://github.com/drewish/rspec-rails-swagger
|