lazy_api_doc 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: b6a2460462b4e6747f547a54ff2a54883eaa06332c6cff5a282596b1fa4cad3e
4
+ data.tar.gz: a8684334ee355acbf77cf7267de1af061560019fc6b9f26894eea739da21136c
5
+ SHA512:
6
+ metadata.gz: 8dd797d746ede57afd720a224dd1aca0fc381b6287a7743808279f0e3d9915591edd09323d6793218405d4f6eb4ed67ee155b7b98a67862afca7aa6ec8a5be0e
7
+ data.tar.gz: d2fad45f01e0ea6ce438c891e528cb9a7b11d1b347d528138bec2eb23d0edb3136a7d4abc390ea687c6f3bd23fbccc13d3be8c12983f887452b38a3432fb0396
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
+ coverage
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,45 @@
1
+ # Rubocop rules explanation:
2
+ # https://github.com/rubocop-hq/rubocop/blob/master/config/default.yml
3
+ #
4
+ # Rubocop-Rspec rules explanation:
5
+ # https://github.com/rubocop-hq/rubocop-rspec/blob/master/config/default.yml
6
+
7
+ require: rubocop-rspec
8
+
9
+ Style/StringLiterals:
10
+ Enabled: false
11
+
12
+ Style/FrozenStringLiteralComment:
13
+ Enabled: false
14
+
15
+ Metrics/LineLength:
16
+ Exclude:
17
+ - lazy_api_doc.gemspec
18
+ Max: 120
19
+
20
+ Style/ExpandPathArguments:
21
+ Enabled: false
22
+
23
+ Layout/AlignHash:
24
+ Enabled: false
25
+
26
+ Metrics/BlockLength:
27
+ Exclude:
28
+ - spec/**/*
29
+
30
+ Metrics/MethodLength:
31
+ Max: 50
32
+
33
+ Style/Documentation:
34
+ Enabled: false
35
+
36
+ Metrics/AbcSize:
37
+ Max: 25
38
+
39
+
40
+
41
+ RSpec/ExampleLength:
42
+ Enabled: false
43
+
44
+ RSpec/DescribedClass:
45
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ lazy_api_doc
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.6.4
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.4
6
+ before_install: gem install bundler -v 2.1.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at biguban@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in lazy_api_doc.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'rubocop', require: false
9
+ gem "rubocop-rspec", require: false
10
+ gem "simplecov", require: false, group: :test
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lazy_api_doc (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ diff-lcs (1.3)
11
+ docile (1.3.2)
12
+ jaro_winkler (1.5.4)
13
+ parallel (1.19.0)
14
+ parser (2.6.5.0)
15
+ ast (~> 2.4.0)
16
+ rainbow (3.0.0)
17
+ rake (12.3.3)
18
+ rspec (3.9.0)
19
+ rspec-core (~> 3.9.0)
20
+ rspec-expectations (~> 3.9.0)
21
+ rspec-mocks (~> 3.9.0)
22
+ rspec-core (3.9.0)
23
+ rspec-support (~> 3.9.0)
24
+ rspec-expectations (3.9.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.9.0)
27
+ rspec-mocks (3.9.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.9.0)
30
+ rspec-support (3.9.0)
31
+ rubocop (0.76.0)
32
+ jaro_winkler (~> 1.5.1)
33
+ parallel (~> 1.10)
34
+ parser (>= 2.6)
35
+ rainbow (>= 2.2.2, < 4.0)
36
+ ruby-progressbar (~> 1.7)
37
+ unicode-display_width (>= 1.4.0, < 1.7)
38
+ rubocop-rspec (1.38.1)
39
+ rubocop (>= 0.68.1)
40
+ ruby-progressbar (1.10.1)
41
+ simplecov (0.18.5)
42
+ docile (~> 1.1)
43
+ simplecov-html (~> 0.11)
44
+ simplecov-html (0.12.2)
45
+ unicode-display_width (1.6.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ lazy_api_doc!
52
+ rake (~> 12.0)
53
+ rspec (~> 3.0)
54
+ rubocop
55
+ rubocop-rspec
56
+ simplecov
57
+
58
+ BUNDLED WITH
59
+ 2.1.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Bogdan Guban
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,62 @@
1
+ # LazyApiDoc
2
+
3
+ A library to generate OpenAPI V3 documentation from tests.
4
+
5
+ LazyApiDoc collects requests and responses from your controller and request specs, retrieves data types, optional
6
+ attributes, endpoint description and then generates OpenAPI documentation.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'lazy_api_doc', group: :test
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install lazy_api_doc
23
+
24
+ Then run install task
25
+
26
+ $ rails g lazy_api_doc:install
27
+
28
+ ## Usage
29
+
30
+ Update files `public/lazy_api_doc/index.html` and `public/lazy_api_doc/layout.yml`. These files will be
31
+ used as templates to show the documentation. You need to set your application name, description and
32
+ so on.
33
+
34
+ And just run your tests with `DOC=true` environment variable:
35
+
36
+ $ DOC=true rspec
37
+
38
+ or
39
+
40
+ # DOC=true rake test
41
+
42
+ The documentation will be placed `public/lazy_api_doc/api.yml`. To see it just run server
43
+
44
+ $ rails server
45
+
46
+ and navigate to http://localhost:3000/lazy_api_doc/
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bguban/lazy_api_doc. This project is intended
51
+ to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
52
+ the [code of conduct](https://github.com/bguban/lazy_api_doc/blob/master/CODE_OF_CONDUCT.md).
53
+
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
58
+
59
+ ## Code of Conduct
60
+
61
+ Everyone interacting in the LazyApiDoc project's codebases, issue trackers, chat rooms and mailing lists is expected to
62
+ follow the [code of conduct](https://github.com/bguban/lazy_api_doc/blob/master/CODE_OF_CONDUCT.md).
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 "lazy_api_doc"
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,29 @@
1
+ require_relative 'lib/lazy_api_doc/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "lazy_api_doc"
5
+ spec.version = LazyApiDoc::VERSION
6
+ spec.authors = ["Bogdan Guban"]
7
+ spec.email = ["biguban@gmail.com"]
8
+
9
+ spec.summary = "Creates openapi v3 documentation based on rspec request tests"
10
+ spec.description = "The gem collects all requests and responses from your request specs and generates documentationbased on it"
11
+ spec.homepage = "https://github.com/bguban/lazy_api_doc"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,60 @@
1
+ require 'rails/generators'
2
+
3
+ module LazyApiDoc
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('templates', __dir__)
7
+
8
+ desc "Copy base configuration for LazyApiDoc"
9
+ def install
10
+ copy_file 'public/index.html', 'public/lazy_api_doc/index.html'
11
+ copy_file 'public/layout.yml', 'public/lazy_api_doc/layout.yml'
12
+
13
+ install_rspec if Dir.exist?('spec')
14
+
15
+ install_minitest if Dir.exist?('test')
16
+ end
17
+
18
+ private
19
+
20
+ def install_rspec
21
+ copy_file 'support/rspec_interceptor.rb', 'spec/support/lazy_api_doc_interceptor.rb'
22
+
23
+ insert_into_file 'spec/rails_helper.rb', after: "RSpec.configure do |config|\n" do
24
+ <<~RUBY
25
+ if ENV['DOC']
26
+ require 'support/lazy_api_doc_interceptor'
27
+
28
+ config.include LazyApiDocInterceptor, type: :request
29
+ config.include LazyApiDocInterceptor, type: :controller
30
+
31
+ config.after(:suite) do
32
+ LazyApiDoc.save_result
33
+ end
34
+ end
35
+ RUBY
36
+ end
37
+ end
38
+
39
+ def install_minitest
40
+ copy_file 'support/minitest_interceptor.rb', 'test/support/lazy_api_doc_interceptor.rb'
41
+
42
+ append_to_file 'test/test_helper.rb' do
43
+ <<~RUBY
44
+ if ENV['DOC']
45
+ require 'support/lazy_api_doc_interceptor'
46
+
47
+ class ActionDispatch::IntegrationTest
48
+ include LazyApiDocInterceptor
49
+ end
50
+
51
+ Minitest.after_run do
52
+ LazyApiDoc.save_result
53
+ end
54
+ end
55
+ RUBY
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,24 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>My Application Documentation</title>
5
+ <!-- needed for adaptive design -->
6
+ <meta charset="utf-8"/>
7
+ <meta name="viewport" content="width=device-width, initial-scale=1">
8
+ <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
9
+
10
+ <!--
11
+ ReDoc doesn't change outer page styles
12
+ -->
13
+ <style>
14
+ body {
15
+ margin: 0;
16
+ padding: 0;
17
+ }
18
+ </style>
19
+ </head>
20
+ <body>
21
+ <redoc spec-url='./api.yml'></redoc>
22
+ <script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"></script>
23
+ </body>
24
+ </html>
@@ -0,0 +1,18 @@
1
+ openapi: 3.0.0
2
+ info:
3
+ title: App name
4
+ version: "1.0.0"
5
+ contact:
6
+ name: User Name
7
+ email: user@example.com
8
+ url: https://app.example.com
9
+ tags:
10
+
11
+ servers:
12
+ - url: https://app.example.com
13
+ description: description
14
+
15
+ paths:
16
+
17
+ components:
18
+ schemas:
@@ -0,0 +1,14 @@
1
+ module LazyApiDocInterceptor
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ %w[get post patch put head delete].each do |method|
6
+ define_method(method) do |*args|
7
+ result = super(*args)
8
+ # self.class.metadata[:doc]
9
+ LazyApiDoc.add_test(self)
10
+ result
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module LazyApiDocInterceptor
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ %w[get post patch put head delete].each do |method|
6
+ define_method(method) do |*args|
7
+ result = super(*args)
8
+ # self.class.metadata[:doc] can be used to document only tests with doc: true metadata
9
+ LazyApiDoc.add_spec(self)
10
+ result
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,56 @@
1
+ require "lazy_api_doc/version"
2
+ require "lazy_api_doc/variants_parser"
3
+ require "lazy_api_doc/generator"
4
+ require "lazy_api_doc/route_parser"
5
+ require "yaml"
6
+
7
+ module LazyApiDoc
8
+ class Error < StandardError; end
9
+
10
+ def self.generator
11
+ @generator ||= Generator.new
12
+ end
13
+
14
+ def self.add(example)
15
+ generator.add(example)
16
+ end
17
+
18
+ def self.add_spec(example)
19
+ add(
20
+ controller: example.request.params[:controller],
21
+ action: example.request.params[:action],
22
+ description: example.class.description,
23
+ verb: example.request.method,
24
+ params: example.request.params,
25
+ content_type: example.request.content_type.to_s,
26
+ response: {
27
+ code: example.response.status,
28
+ content_type: example.response.content_type.to_s,
29
+ body: example.response.body
30
+ }
31
+ )
32
+ end
33
+
34
+ def self.add_test(example)
35
+ add(
36
+ controller: example.request.params[:controller],
37
+ action: example.request.params[:action],
38
+ description: example.name.gsub(/\Atest_/, '').humanize,
39
+ verb: example.request.method,
40
+ params: example.request.params,
41
+ content_type: example.request.content_type.to_s,
42
+ response: {
43
+ code: example.response.status,
44
+ content_type: example.response.content_type.to_s,
45
+ body: example.response.body
46
+ }
47
+ )
48
+ end
49
+
50
+ def self.save_result(to: 'public/lazy_api_doc/api.yml', layout: 'public/lazy_api_doc/layout.yml')
51
+ layout = YAML.safe_load(File.read(Rails.root.join(layout)))
52
+ layout["paths"] ||= {}
53
+ layout["paths"].merge!(generator.result)
54
+ File.write(Rails.root.join(to), layout.to_yaml)
55
+ end
56
+ end
@@ -0,0 +1,86 @@
1
+ module LazyApiDoc
2
+ class Generator
3
+ attr_reader :examples
4
+
5
+ def initialize
6
+ @examples = []
7
+ end
8
+
9
+ def add(example)
10
+ return if example[:controller] == "anonymous" # don't handle virtual controllers
11
+
12
+ @examples << OpenStruct.new(example)
13
+ end
14
+
15
+ def result
16
+ result = {}
17
+ @examples.group_by { |example| [example.controller, example.action] }.map do |_, examples|
18
+ first = examples.first
19
+ route = ::LazyApiDoc::RouteParser.new(first.controller, first.action, first.verb).route
20
+ doc_path = route[:doc_path]
21
+ result[doc_path] ||= {}
22
+ result[doc_path].merge!(example_group(first, examples, route))
23
+ end
24
+ result
25
+ end
26
+
27
+ def example_group(example, examples, route)
28
+ {
29
+ route[:verb].downcase => {
30
+ "tags" => [example.controller],
31
+ "description" => example["description"].capitalize,
32
+ "summary" => example.action,
33
+ "parameters" => path_params(route, examples),
34
+ "requestBody" => body_params(route, examples),
35
+ "responses" => examples.group_by { |ex| ex.response[:code] }.map do |code, variants|
36
+ [
37
+ code,
38
+ {
39
+ "content" => {
40
+ example.response[:content_type] => {
41
+ "schema" => ::LazyApiDoc::VariantsParser.new(variants.map { |v| parse_body(v.response) }).result
42
+ }
43
+ }
44
+ }
45
+ ]
46
+ end.to_h
47
+ }
48
+ }
49
+ end
50
+
51
+ def parse_body(response)
52
+ if response[:content_type].match?("json")
53
+ JSON.parse(response[:body])
54
+ else
55
+ "Not a JSON response"
56
+ end
57
+ rescue JSON::ParserError
58
+ response[:body]
59
+ end
60
+
61
+ def path_params(route, examples)
62
+ variants = examples.map { |example| example.params.slice(*route[:path_params]) }
63
+ ::LazyApiDoc::VariantsParser.new(variants).result["properties"].map do |param_name, schema|
64
+ {
65
+ 'in' => "path",
66
+ 'name' => param_name,
67
+ 'schema' => schema
68
+ }
69
+ end
70
+ end
71
+
72
+ def body_params(route, examples)
73
+ return if route[:verb] == "GET"
74
+
75
+ first = examples.first
76
+ variants = examples.map { |example| example.params.except("controller", "action", *route[:path_params]) }
77
+ {
78
+ 'content' => {
79
+ first.content_type => {
80
+ 'schema' => ::LazyApiDoc::VariantsParser.new(variants).result
81
+ }
82
+ }
83
+ }
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,48 @@
1
+ module LazyApiDoc
2
+ class RouteParser
3
+ attr_reader :controller, :action, :verb
4
+
5
+ def initialize(controller, action, verb)
6
+ @controller = controller
7
+ @action = action
8
+ @verb = verb
9
+ end
10
+
11
+ def route
12
+ self.class.routes.find { |r| r[:action] == action && r[:controller] == controller && r[:verb] == verb }
13
+ end
14
+
15
+ def self.routes
16
+ return @routes if defined?(@routes)
17
+
18
+ all_routes = Rails.application.routes.routes
19
+ require "action_dispatch/routing/inspector"
20
+ inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
21
+ @routes = inspector.format(JsonRoutesFormatter.new, ENV["CONTROLLER"])
22
+ end
23
+ end
24
+ end
25
+
26
+ class JsonRoutesFormatter
27
+ def initialize
28
+ @buffer = []
29
+ end
30
+
31
+ def result
32
+ @buffer
33
+ end
34
+
35
+ def section_title(_title); end
36
+
37
+ def section(routes)
38
+ @buffer = routes.map do |r|
39
+ r[:doc_path] = r[:path].gsub("(.:format)", "").gsub(/(:\w+)/, '{\1}').delete(":")
40
+ r[:path_params] = r[:path].gsub("(.:format)", "").scan(/:\w+/).map { |p| p.delete(":").to_sym }
41
+ r[:controller] = r[:reqs].split("#").first
42
+ r[:action] = r[:reqs].split("#").last.split(" ").first
43
+ r
44
+ end
45
+ end
46
+
47
+ def header(_routes); end
48
+ end
@@ -0,0 +1,87 @@
1
+ module LazyApiDoc
2
+ class VariantsParser
3
+ OPTIONAL = :lazy_api_doc_optional
4
+ attr_reader :variants
5
+
6
+ def initialize(variants)
7
+ @variants = variants.is_a?(Array) ? variants : [variants]
8
+ end
9
+
10
+ def result
11
+ @result ||= parse(variants.first, variants)
12
+ end
13
+
14
+ def parse(variant, variants)
15
+ variants.delete(OPTIONAL)
16
+ case variant
17
+ when Array
18
+ parse_array(variant, variants)
19
+ when Hash
20
+ parse_hash(variant, variants)
21
+ else
22
+ types_template(variants).merge("example" => variant)
23
+ end
24
+ end
25
+
26
+ def types_template(variants)
27
+ types = types_of(variants)
28
+ if types.count == 1
29
+ {
30
+ "type" => types.first
31
+ }
32
+ else
33
+ {
34
+ "oneOf" => types.map { |t| { "type" => t } }
35
+ }
36
+ end
37
+ end
38
+
39
+ def types_of(variants)
40
+ variants.map { |v| type_of(v) }.uniq
41
+ end
42
+
43
+ def type_of(variant)
44
+ case variant
45
+ when Hash
46
+ "object"
47
+ when NilClass
48
+ "null"
49
+ when TrueClass, FalseClass
50
+ "boolean"
51
+ when String
52
+ type_of_string(variant)
53
+ else
54
+ variant.class.name.downcase
55
+ end
56
+ end
57
+
58
+ def type_of_string(variant)
59
+ case variant
60
+ when /\A\d+\.\d+\z/
61
+ "decimal"
62
+ else
63
+ "string"
64
+ end
65
+ end
66
+
67
+ def parse_hash(variant, variants)
68
+ result = types_template(variants)
69
+ result["properties"] = variant.map do |key, val|
70
+ [
71
+ key.to_s,
72
+ parse(val, variants.compact.map { |v| v.fetch(key, OPTIONAL) })
73
+ ]
74
+ end.to_h
75
+ result["required"] = variant.keys.select { |key| variants.compact.all? { |v| v.key?(key) } }
76
+ result
77
+ end
78
+
79
+ def parse_array(variant, variants)
80
+ first = variant.first
81
+ types_template(variants).merge(
82
+ "items" => parse(first, variants.map(&:first).compact),
83
+ "example" => variant
84
+ )
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module LazyApiDoc
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_api_doc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bogdan Guban
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The gem collects all requests and responses from your request specs and
14
+ generates documentationbased on it
15
+ email:
16
+ - biguban@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".rubocop.yml"
24
+ - ".ruby-gemset"
25
+ - ".ruby-version"
26
+ - ".travis.yml"
27
+ - CODE_OF_CONDUCT.md
28
+ - Gemfile
29
+ - Gemfile.lock
30
+ - LICENSE.txt
31
+ - README.md
32
+ - Rakefile
33
+ - bin/console
34
+ - bin/setup
35
+ - lazy_api_doc.gemspec
36
+ - lib/generators/lazy_api_doc/install_generator.rb
37
+ - lib/generators/lazy_api_doc/templates/public/index.html
38
+ - lib/generators/lazy_api_doc/templates/public/layout.yml
39
+ - lib/generators/lazy_api_doc/templates/support/minitest_interceptor.rb
40
+ - lib/generators/lazy_api_doc/templates/support/rspec_interceptor.rb
41
+ - lib/lazy_api_doc.rb
42
+ - lib/lazy_api_doc/generator.rb
43
+ - lib/lazy_api_doc/route_parser.rb
44
+ - lib/lazy_api_doc/variants_parser.rb
45
+ - lib/lazy_api_doc/version.rb
46
+ homepage: https://github.com/bguban/lazy_api_doc
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ allowed_push_host: https://rubygems.org
51
+ homepage_uri: https://github.com/bguban/lazy_api_doc
52
+ source_code_uri: https://github.com/bguban/lazy_api_doc
53
+ changelog_uri: https://github.com/bguban/lazy_api_doc
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.3.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.0.6
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Creates openapi v3 documentation based on rspec request tests
73
+ test_files: []