rspec-autoswagger 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e9ca20badc411c1e7708446e3b85298a19c03af38afaf5f7d304df647a1a38f3
4
+ data.tar.gz: f94c3bdb32d80bf3bb6a6675312d03a0c04e0483eac887573bc670d1b47fb8ba
5
+ SHA512:
6
+ metadata.gz: f8d0cd561f05b89ccedfc16a1f63e968ef08335695bfadc2a185268c591595a227f9e564070653d91575b4fed8de8cf2780e66c3ecb2f717819ac3e61d410c42
7
+ data.tar.gz: 1f17084e9e12108d00f2f018f9f0a4525ee5de1561c2990d6375cec0bb983ff248f474ffff4909269d4d2b3c11811b1f05fd1bf6a293fe3a5adb86487a98539d
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ *.sw*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rspec-autoswagger.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 sayakahojo
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,39 @@
1
+ # Rspec::Autoswagger
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rspec/autoswagger`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rspec-autoswagger'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rspec-autoswagger
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rspec-autoswagger.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rspec/autoswagger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ require 'rspec/autoswagger/parts/path'
2
+ require 'rspec/autoswagger/parts/info'
3
+ require 'rspec/autoswagger/parts/definition'
4
+
5
+ module Rspec
6
+ module Autoswagger
7
+ class DocPart
8
+
9
+ attr_reader :rspec_core_obj, :example
10
+
11
+ def initialize(rspec_core_obj, example)
12
+ @rspec_core_obj = rspec_core_obj
13
+ @example = example
14
+ end
15
+
16
+ def response_name
17
+ example.full_description[%r<(GET|POST|PATCH|PUT|DELETE) ([^ ]+)>, 2].gsub(/\/|:/, '').camelize
18
+ end
19
+
20
+ def create_path
21
+ path = Parts::Path.new(rspec_core_obj, example, response_name)
22
+ path.generate_hash
23
+ end
24
+
25
+ def create_definition
26
+ definition = Parts::Definition.new(rspec_core_obj.response.body, response_name)
27
+ definition.generate_definitions
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,41 @@
1
+ require 'rspec/autoswagger/doc_part'
2
+ module Rspec module Autoswagger
3
+ class DocParts
4
+
5
+ attr_reader :specification, :info, :paths, :definitions
6
+ def initialize
7
+ @info = Parts::Info.generate_hash
8
+ @paths = {}
9
+ @definitions = {}
10
+ @specification = {}
11
+ end
12
+
13
+ def add(rspec_core_obj, example)
14
+ doc_part = DocPart.new(rspec_core_obj, example)
15
+ path, param_definitions = doc_part.create_path
16
+ if paths.keys.include?(path.keys.first)
17
+ paths[path.keys.first].merge!(path.values.first)
18
+ else
19
+ paths.merge!(path)
20
+ end
21
+ definitions.merge!(doc_part.create_definition)
22
+ definitions.merge!(param_definitions) unless param_definitions.empty?
23
+ end
24
+
25
+ def aggregate
26
+ specification.merge!(info)
27
+ specification.merge!({ 'paths' => paths })
28
+ specification.merge!({ 'definitions' => definitions })
29
+
30
+ specification
31
+ end
32
+
33
+ def to_yaml
34
+ aggregate if specification.empty?
35
+
36
+ FileUtils::mkdir_p(Rails.root.to_s + '/tmp/')
37
+ YAML.dump(specification, File.open(Rails.root.to_s + '/tmp/swagger.yml', 'w'))
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,16 @@
1
+ swagger: '2.0'
2
+ info:
3
+ description: Doc Application For Project Api
4
+ version: 1.0.0
5
+ title: Project Api Doc
6
+ license:
7
+ name: Apache 2.0
8
+ url: http://www.apache.org/licenses/LICENSE-2.0.html
9
+ host: contents.kurashiru.com
10
+ basePath: /api/v1
11
+ schemes:
12
+ - https
13
+ consumes:
14
+ - application/json
15
+ produces:
16
+ - application/json
@@ -0,0 +1,49 @@
1
+ require 'swagger_model'
2
+
3
+ module Rspec
4
+ module Autoswagger
5
+ module Parts
6
+ class Definition
7
+ attr_reader :json, :response_name
8
+
9
+ DEFAULT_PATH = './tmp/specifications'
10
+
11
+ def initialize(json, response_name)
12
+ @json = json
13
+ @response_name = response_name
14
+ end
15
+
16
+ def tmp_file_path
17
+ DEFAULT_PATH
18
+ end
19
+
20
+ def generate_model_definitions
21
+ model_definition_hash = {}
22
+ generate_hash_and_file['models'].each do |key, value|
23
+ model_definition_hash.merge!(value)
24
+ end
25
+ model_definition_hash
26
+ end
27
+
28
+ def generate_response_definitions
29
+ generate_hash_and_file['responses']
30
+ end
31
+
32
+ def generate_definitions
33
+ model_hash = generate_model_definitions
34
+ response_hash = generate_response_definitions
35
+ response_hash.merge!(model_hash)
36
+ response_hash
37
+ end
38
+
39
+ def generate_hash_and_file
40
+ @definition_hash ||= SwaggerModel::SwaggerV2.create_from_json(
41
+ json_string: json,
42
+ tmp_file_path: tmp_file_path,
43
+ response_name: response_name
44
+ )
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,14 @@
1
+ module Rspec
2
+ module Autoswagger
3
+ module Parts
4
+ class Info
5
+
6
+ DEFAULT_INFO = YAML.load_file(File.expand_path("../config/default_info.yml", __FILE__))
7
+
8
+ def self.generate_hash
9
+ DEFAULT_INFO
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,155 @@
1
+ module Rspec
2
+ module Autoswagger
3
+ module Parts
4
+ class Path
5
+ attr_reader :response, :request, :description, :example, :path, :response_name
6
+
7
+ def initialize(rspec_core_obj, example, response_name)
8
+ @response = rspec_core_obj.response
9
+ @request = rspec_core_obj.request
10
+ @description = rspec_core_obj.description
11
+ @example = example
12
+ @response_name = response_name
13
+ end
14
+
15
+ def path
16
+ path = example.full_description[%r<(GET|POST|PATCH|PUT|DELETE) ([^ ]+)>, 2]
17
+ path.split("/").map { |name| name.include?(":") ? "{" + name.gsub(":", "") + "}" : name }.join("/")
18
+ end
19
+
20
+ def tags
21
+ tag = request.parameters['controller']
22
+ base_path = Info.generate_hash['basePath']
23
+ tag = ('/' + tag).gsub(base_path.to_s, '')
24
+ [tag]
25
+ end
26
+
27
+ def status
28
+ response.status
29
+ end
30
+
31
+ def method
32
+ request.method.downcase
33
+ end
34
+
35
+ def operation_id
36
+ (method + path.gsub('/', '_').gsub(/{|}/, '')).camelize
37
+ end
38
+
39
+ def param_model_name
40
+ (response_name + '_' + method + '_request_parameter').camelize
41
+ end
42
+
43
+ def params
44
+ return @params if @params.present?
45
+ @params = request.parameters.dup
46
+ @params.delete('controller')
47
+ @params.delete('action')
48
+ @params
49
+ end
50
+
51
+ def generate_parameters
52
+ schema = {}
53
+ params_arr = []
54
+ params.each do |name, value|
55
+ type = convert_value_to_type(value)
56
+ param_type = predict_param_type(name)
57
+ if param_type == 'body'
58
+ if type == 'array'
59
+ type_hash = SwaggerModel::SwaggerV2.parse_array(value, "dummy", "dummy")
60
+ type_hash.delete('example')
61
+ schema.merge!({ name => { 'type' => type, 'items' => type_hash } })
62
+ else
63
+ schema.merge!({ name => { 'type' => type } })
64
+ end
65
+ else
66
+ if type == 'array'
67
+ type_hash = SwaggerModel::SwaggerV2.parse_array(value, "dummy", "dummy")
68
+ type_hash.delete('example')
69
+ param_hash = {
70
+ 'name' => name,
71
+ 'in' => param_type,
72
+ 'type' => type,
73
+ 'items' => type_hash
74
+ }
75
+ else
76
+ param_hash = {
77
+ 'name' => name,
78
+ 'in' => param_type,
79
+ 'type' => type
80
+ }
81
+ end
82
+ param_hash['required'] = true if param_type == 'path'
83
+ params_arr << param_hash
84
+ end
85
+ end
86
+ param_definitions = {}
87
+ unless schema.empty?
88
+ params_arr << {
89
+ 'name' => 'body',
90
+ 'in' => 'body',
91
+ 'schema' => { "$ref" => "#/definitions/#{param_model_name}" }
92
+ }
93
+ param_definitions = { param_model_name => { 'type' => 'object', 'properties' => schema } }
94
+ end
95
+
96
+ [params_arr, param_definitions]
97
+ end
98
+
99
+ def generate_hash
100
+ hash = {}
101
+ path_sym = path
102
+ hash[path] = {}
103
+ hash[path][method] = {}
104
+
105
+ hash[path][method] = {}
106
+ hash[path][method]["tags"] = tags
107
+ hash[path][method]["summary"] = ''
108
+ hash[path][method]["description"] = description
109
+ hash[path][method]["operationId"] = operation_id
110
+
111
+ params, param_definitions = generate_parameters
112
+ hash[path][method]["parameters"] = params
113
+
114
+ hash[path][method]["produces"] = ['application/json']
115
+ hash[path][method]["responses"] = {}
116
+ hash[path][method]["responses"][status] = {}
117
+ hash[path][method]["responses"][status]["description"] = response_description
118
+ hash[path][method]["responses"][status]["schema"] = { "$ref" => "#/definitions/#{response_name}" }
119
+ [hash, param_definitions]
120
+ end
121
+
122
+ private
123
+
124
+ def response_description
125
+ case status.to_i
126
+ when 200
127
+ 'successful operation'
128
+ else
129
+ 'error operation'
130
+ end
131
+ end
132
+
133
+ def predict_param_type(param_name)
134
+ candidates = path.split("/").map { |name| name.gsub(/{|}/, "") }
135
+ return 'path' if candidates.include?(param_name.to_s)
136
+ method == 'get' ? 'query' : 'body'
137
+ end
138
+
139
+ def convert_value_to_type(value)
140
+ if value.to_s == 'true' || value.to_s == 'false'
141
+ 'boolean'
142
+ elsif value.is_a?(String)
143
+ 'string'
144
+ elsif value.is_a?(Integer)
145
+ 'integer'
146
+ elsif value.is_a?(Array)
147
+ 'array'
148
+ else
149
+ 'string'
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,11 @@
1
+ require 'rspec'
2
+ require 'rspec/autoswagger/doc_parts'
3
+
4
+ RSpec.configuration.after(:each, autoswagger: true) do |example|
5
+ Rspec::Autoswagger.doc_parts.add(self, example)
6
+ end
7
+
8
+ RSpec.configuration.after(:suite) do
9
+ Rspec::Autoswagger.doc_parts.aggregate
10
+ Rspec::Autoswagger.doc_parts.to_yaml
11
+ end
@@ -0,0 +1,5 @@
1
+ module Rspec
2
+ module Autoswagger
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require "rspec/autoswagger/version"
2
+ require "rspec/autoswagger/rspec" if ENV["AUTOSWAGGER"]
3
+ require "rspec/autoswagger/doc_parts"
4
+
5
+ module Rspec
6
+ module Autoswagger
7
+
8
+ def self.doc_parts
9
+ @doc_parts ||= DocParts.new
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,37 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rspec/autoswagger/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rspec-autoswagger"
8
+ spec.version = Rspec::Autoswagger::VERSION
9
+ spec.authors = ["joooee0000"]
10
+ spec.email = ["joooee0000@gmail.com"]
11
+
12
+ spec.summary = "Rspec extension to auto generate swagger/openapi specification"
13
+ spec.homepage = "https://github.com/joooee0000/rspec-autoswagger"
14
+ spec.license = "MIT"
15
+
16
+ if spec.respond_to?(:metadata)
17
+ else
18
+ raise "RubyGems 2.0 or newer is required to protect against " \
19
+ "public gem pushes."
20
+ end
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "swagger_model"
32
+ spec.add_dependency "rspec"
33
+ spec.add_dependency "activesupport"
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-autoswagger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - joooee0000
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: swagger_model
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ description:
84
+ email:
85
+ - joooee0000@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/rspec/autoswagger.rb
100
+ - lib/rspec/autoswagger/doc_part.rb
101
+ - lib/rspec/autoswagger/doc_parts.rb
102
+ - lib/rspec/autoswagger/parts/config/default_info.yml
103
+ - lib/rspec/autoswagger/parts/definition.rb
104
+ - lib/rspec/autoswagger/parts/info.rb
105
+ - lib/rspec/autoswagger/parts/path.rb
106
+ - lib/rspec/autoswagger/rspec.rb
107
+ - lib/rspec/autoswagger/version.rb
108
+ - rspec-autoswagger.gemspec
109
+ homepage: https://github.com/joooee0000/rspec-autoswagger
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.7.7
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Rspec extension to auto generate swagger/openapi specification
133
+ test_files: []