weak_swagger_parameters 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +88 -12
- data/lib/weak_swagger_parameters/controller.rb +24 -2
- data/lib/weak_swagger_parameters/definitions/api.rb +14 -24
- data/lib/weak_swagger_parameters/version.rb +1 -1
- data/weak_swagger_parameters.gemspec +1 -1
- metadata +3 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85e46ed943f82522b4affcca0fbe25da225527c8
|
4
|
+
data.tar.gz: e8f515e3aa857121ec296d75c7783cdb0758334c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db40e7a3e5b89864cc151dc2f516d0769a1134bed82a3d6308506de488b720f1eae1052286a058546695f78dcaaa64c3d0ba10409b32e5b46b5de1a79043524c
|
7
|
+
data.tar.gz: 5e261df19f41b3c7a99e0c381d21b695df5997ae306390b25fa3913918399effd8fc0c9d9b88a9746fb41fbe2786df500fbc15e081ce8f1b672874d35f5d21e4
|
data/README.md
CHANGED
@@ -4,9 +4,7 @@
|
|
4
4
|
|
5
5
|
# WeakSwaggerParameters
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
TODO: Delete this and the text above, and describe your gem
|
7
|
+
This is an integration gem between `weak_parameters` and `swagger-blocks` to allow creating interactive swagger documentation and input parameter validation without duplicating the definitions using both DSLs.
|
10
8
|
|
11
9
|
## Installation
|
12
10
|
|
@@ -18,25 +16,103 @@ gem 'weak_swagger_parameters'
|
|
18
16
|
|
19
17
|
And then execute:
|
20
18
|
|
21
|
-
$ bundle
|
22
|
-
|
23
|
-
Or install it yourself as:
|
24
|
-
|
25
|
-
$ gem install weak_swagger_parameters
|
19
|
+
$ bundle install
|
26
20
|
|
27
21
|
## Usage
|
28
22
|
|
29
|
-
|
23
|
+
(1) Add route to a docs controller to serve Swagger v2.0 json
|
24
|
+
````ruby
|
25
|
+
# routes.rb
|
26
|
+
|
27
|
+
namespace :v1 do
|
28
|
+
resources :docs, only: [:index]
|
29
|
+
end
|
30
|
+
````
|
31
|
+
|
32
|
+
(2) Create a controller for serving the Swagger v2.0 json
|
33
|
+
````ruby
|
34
|
+
module V1
|
35
|
+
class DocsController < ActionController::Base
|
36
|
+
include WeakSwaggerParameters::Controller
|
37
|
+
|
38
|
+
add_to_doc_section('V1')
|
39
|
+
|
40
|
+
swagger_root swagger: '2.0' do
|
41
|
+
info version: '1.0', title: 'The best api', description: 'Api that does everything'
|
42
|
+
key :host, 'example.com'
|
43
|
+
key :consumes, ['application/json']
|
44
|
+
key :produces, ['application/json']
|
45
|
+
end
|
46
|
+
def index
|
47
|
+
render_docs('V1')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
````
|
52
|
+
|
53
|
+
(3) Add metadata to existing controllers for them to show up in the documentation
|
54
|
+
|
55
|
+
````ruby
|
56
|
+
class ItemsController < ActionController::Base
|
57
|
+
include WeakSwaggerParameters::Controller
|
58
|
+
|
59
|
+
add_to_doc_section('V1')
|
60
|
+
|
61
|
+
api :create, '/tests', 'Create test' do
|
62
|
+
params do
|
63
|
+
path do
|
64
|
+
string :short_name, 'Short test name'
|
65
|
+
integer :count, 'Count of tests'
|
66
|
+
end
|
67
|
+
query do
|
68
|
+
string :token, 'The token'
|
69
|
+
end
|
70
|
+
body do
|
71
|
+
string :subject, 'The unit under test'
|
72
|
+
string :context, 'The context of the test'
|
73
|
+
integer :runs, 'Run times'
|
74
|
+
boolean :passed, 'Passed'
|
75
|
+
boolean :boolean_required, 'Boolean required', required: true
|
76
|
+
string :string_required, 'String required', required: true
|
77
|
+
integer :integer_required, 'Integer required', required: true
|
78
|
+
string :string_enum, 'String enum', enum: %w(a b c)
|
79
|
+
string :string_default, 'String default', default: 'origin'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
response 201, 'Created the test'
|
83
|
+
response 400, 'Bad Request'
|
84
|
+
end
|
85
|
+
def create
|
86
|
+
# ...
|
87
|
+
end
|
88
|
+
end
|
89
|
+
````
|
90
|
+
|
91
|
+
For complete examples, see the specs.
|
30
92
|
|
31
93
|
## Development
|
32
94
|
|
33
|
-
|
95
|
+
Install dependencies
|
96
|
+
|
97
|
+
```
|
98
|
+
bundle install
|
99
|
+
```
|
100
|
+
|
101
|
+
Run Specs
|
102
|
+
|
103
|
+
```
|
104
|
+
bundle exec rake rspec
|
105
|
+
```
|
34
106
|
|
35
|
-
|
107
|
+
Run Rubocop
|
108
|
+
|
109
|
+
```
|
110
|
+
bundle exec rake rubocop
|
111
|
+
```
|
36
112
|
|
37
113
|
## Contributing
|
38
114
|
|
39
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
115
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/AgileFreaks/weak_swagger_parameters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
40
116
|
|
41
117
|
|
42
118
|
## License
|
@@ -13,8 +13,30 @@ module WeakSwaggerParameters
|
|
13
13
|
(@doc_sections || []).include?(doc_section)
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
16
|
+
def get(action, path, description, &block)
|
17
|
+
api(:get, action, path, description, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def post(action, path, description, &block)
|
21
|
+
api(:post, action, path, description, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
def put(action, path, description, &block)
|
25
|
+
api(:put, action, path, description, &block)
|
26
|
+
end
|
27
|
+
|
28
|
+
def patch(action, path, description, &block)
|
29
|
+
api(:patch, action, path, description, &block)
|
30
|
+
end
|
31
|
+
|
32
|
+
def delete(action, path, description, &block)
|
33
|
+
api(:delete, action, path, description, &block)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def api(http_method, action, path, description, &block)
|
39
|
+
api_docs = WeakSwaggerParameters::Definitions::Api.new(http_method, action, path, description, &block)
|
18
40
|
api_docs.apply_validations(self)
|
19
41
|
api_docs.apply_docs(self)
|
20
42
|
end
|
@@ -2,25 +2,18 @@
|
|
2
2
|
module WeakSwaggerParameters
|
3
3
|
module Definitions
|
4
4
|
class Api
|
5
|
-
KNOWN_METHODS = {
|
6
|
-
create: :post,
|
7
|
-
index: :get,
|
8
|
-
show: :get,
|
9
|
-
destroy: :delete,
|
10
|
-
update: :put
|
11
|
-
}.freeze
|
12
|
-
|
13
5
|
attr_reader :path
|
14
6
|
|
15
|
-
def initialize(
|
16
|
-
@
|
7
|
+
def initialize(http_method, action, path, summary, &block)
|
8
|
+
@http_method = http_method
|
9
|
+
@action = action
|
17
10
|
@path = path
|
18
11
|
@summary = summary
|
19
12
|
@param_definition = nil
|
20
13
|
@response_definitions = []
|
21
14
|
@description = nil
|
22
15
|
|
23
|
-
instance_eval(&block)
|
16
|
+
instance_eval(&block) if block.present?
|
24
17
|
end
|
25
18
|
|
26
19
|
def description(description)
|
@@ -31,16 +24,16 @@ module WeakSwaggerParameters
|
|
31
24
|
@param_definition = WeakSwaggerParameters::Definitions::Params.new(&block)
|
32
25
|
end
|
33
26
|
|
34
|
-
def response(status_code, description)
|
27
|
+
def response(status_code, description = '')
|
35
28
|
@response_definitions << WeakSwaggerParameters::Definitions::Response.new(status_code, description)
|
36
29
|
end
|
37
30
|
|
38
31
|
def apply_validations(controller_class)
|
39
32
|
child_definitions = validation_definitions
|
40
|
-
|
33
|
+
action = @action
|
41
34
|
|
42
35
|
controller_class.instance_eval do
|
43
|
-
validates
|
36
|
+
validates action do
|
44
37
|
child_definitions.each { |definition| definition.apply_validations(self) }
|
45
38
|
end
|
46
39
|
end
|
@@ -48,11 +41,12 @@ module WeakSwaggerParameters
|
|
48
41
|
|
49
42
|
def apply_docs(controller_class)
|
50
43
|
this = self
|
44
|
+
http_method = @http_method
|
51
45
|
operation_params = operation_params(http_method, controller_class)
|
52
46
|
|
53
47
|
controller_class.instance_eval do
|
54
48
|
swagger_path this.path do
|
55
|
-
operation
|
49
|
+
operation http_method, operation_params do
|
56
50
|
this.child_definitions.each { |definition| definition.apply_docs(self) }
|
57
51
|
end
|
58
52
|
end
|
@@ -60,24 +54,20 @@ module WeakSwaggerParameters
|
|
60
54
|
end
|
61
55
|
|
62
56
|
def child_definitions
|
63
|
-
validation_definitions + @response_definitions
|
64
|
-
end
|
65
|
-
|
66
|
-
def http_method
|
67
|
-
KNOWN_METHODS[@method]
|
57
|
+
(validation_definitions + @response_definitions).compact
|
68
58
|
end
|
69
59
|
|
70
60
|
private
|
71
61
|
|
72
62
|
def validation_definitions
|
73
|
-
[@param_definition]
|
63
|
+
[@param_definition].compact
|
74
64
|
end
|
75
65
|
|
76
66
|
def operation_params(method, controller_class)
|
77
67
|
name = resource_name(controller_class)
|
78
68
|
{
|
79
69
|
summary: @summary,
|
80
|
-
operationId: operation_id(method,
|
70
|
+
operationId: operation_id(method, controller_class),
|
81
71
|
tags: [name]
|
82
72
|
}.tap do |h|
|
83
73
|
h[:description] = @description unless @description.blank?
|
@@ -88,8 +78,8 @@ module WeakSwaggerParameters
|
|
88
78
|
controller_class.controller_name.humanize
|
89
79
|
end
|
90
80
|
|
91
|
-
def operation_id(method,
|
92
|
-
"#{method}#{
|
81
|
+
def operation_id(method, controller_class)
|
82
|
+
"#{method}_#{controller_class.controller_name}".camelize(:lower)
|
93
83
|
end
|
94
84
|
end
|
95
85
|
end
|
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
spec.require_paths = ['lib']
|
22
22
|
|
23
|
-
spec.add_dependency 'rails', '>= 4'
|
23
|
+
spec.add_dependency 'rails', '>= 4'
|
24
24
|
spec.add_dependency 'weak_parameters', '~> 0.4'
|
25
25
|
spec.add_dependency 'swagger-blocks', '~> 1.3'
|
26
26
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weak_swagger_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- AgileFreaks
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -17,9 +17,6 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4'
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '5'
|
23
20
|
type: :runtime
|
24
21
|
prerelease: false
|
25
22
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,9 +24,6 @@ dependencies:
|
|
27
24
|
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '4'
|
30
|
-
- - "<"
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '5'
|
33
27
|
- !ruby/object:Gem::Dependency
|
34
28
|
name: weak_parameters
|
35
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -209,7 +203,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
209
203
|
version: '0'
|
210
204
|
requirements: []
|
211
205
|
rubyforge_project:
|
212
|
-
rubygems_version: 2.5.1
|
206
|
+
rubygems_version: 2.4.5.1
|
213
207
|
signing_key:
|
214
208
|
specification_version: 4
|
215
209
|
summary: Generate docs and Validate request parameters
|