lazy_api_doc 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/generators/lazy_api_doc/install_generator.rb +3 -0
- data/lib/generators/lazy_api_doc/templates/public/layout.yml +0 -4
- data/lib/lazy_api_doc.rb +12 -2
- data/lib/lazy_api_doc/generator.rb +32 -7
- data/lib/lazy_api_doc/route_parser.rb +1 -1
- data/lib/lazy_api_doc/variants_parser.rb +7 -3
- data/lib/lazy_api_doc/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e13f1dbef507e8cd73dba019cf5b46049a06f257225aaba546a1e426465cfb0a
|
4
|
+
data.tar.gz: f7d05704e71695f96752ae86678df55d5ffbaed7fbcd8af66cb4800fae40622d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 412a28cb29f54a64e3290ad235b645932a86d253f3bbd5cadd9225638bec0c966a9085b56252ed27d28880c31d672fc8712c05723c0cec92f8d7d173cac3c73a
|
7
|
+
data.tar.gz: 5676bfcf8a8e909d9838239051c693512938ab8d0e8150e7e4b1474adf94423f365e5a3ac142e8f0df1e31f1fbf9f9de0c9e7bdbe1ada138bf7e7b74d69514ed
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -23,6 +23,7 @@ module LazyApiDoc
|
|
23
23
|
insert_into_file 'spec/rails_helper.rb', after: "RSpec.configure do |config|\n" do
|
24
24
|
<<~RUBY
|
25
25
|
if ENV['DOC']
|
26
|
+
require 'lazy_api_doc'
|
26
27
|
require 'support/lazy_api_doc_interceptor'
|
27
28
|
|
28
29
|
config.include LazyApiDocInterceptor, type: :request
|
@@ -41,7 +42,9 @@ module LazyApiDoc
|
|
41
42
|
|
42
43
|
append_to_file 'test/test_helper.rb' do
|
43
44
|
<<~RUBY
|
45
|
+
|
44
46
|
if ENV['DOC']
|
47
|
+
require 'lazy_api_doc'
|
45
48
|
require 'support/lazy_api_doc_interceptor'
|
46
49
|
|
47
50
|
class ActionDispatch::IntegrationTest
|
data/lib/lazy_api_doc.rb
CHANGED
@@ -15,14 +15,19 @@ module LazyApiDoc
|
|
15
15
|
generator.add(example)
|
16
16
|
end
|
17
17
|
|
18
|
-
def self.add_spec(example)
|
18
|
+
def self.add_spec(example) # rubocop:disable Metrics/AbcSize
|
19
19
|
add(
|
20
20
|
controller: example.request.params[:controller],
|
21
21
|
action: example.request.params[:action],
|
22
22
|
description: example.class.description,
|
23
|
+
source_location: [example.class.metadata[:file_path], example.class.metadata[:line_number]],
|
23
24
|
verb: example.request.method,
|
24
25
|
params: example.request.params,
|
25
26
|
content_type: example.request.content_type.to_s,
|
27
|
+
request: {
|
28
|
+
query_params: example.request.query_parameters,
|
29
|
+
full_path: example.request.fullpath
|
30
|
+
},
|
26
31
|
response: {
|
27
32
|
code: example.response.status,
|
28
33
|
content_type: example.response.content_type.to_s,
|
@@ -31,14 +36,19 @@ module LazyApiDoc
|
|
31
36
|
)
|
32
37
|
end
|
33
38
|
|
34
|
-
def self.add_test(example)
|
39
|
+
def self.add_test(example) # rubocop:disable Metrics/AbcSize
|
35
40
|
add(
|
36
41
|
controller: example.request.params[:controller],
|
37
42
|
action: example.request.params[:action],
|
38
43
|
description: example.name.gsub(/\Atest_/, '').humanize,
|
44
|
+
source_location: example.method(example.name).source_location,
|
39
45
|
verb: example.request.method,
|
40
46
|
params: example.request.params,
|
41
47
|
content_type: example.request.content_type.to_s,
|
48
|
+
request: {
|
49
|
+
query_params: example.request.query_parameters,
|
50
|
+
full_path: example.request.fullpath
|
51
|
+
},
|
42
52
|
response: {
|
43
53
|
code: example.response.status,
|
44
54
|
content_type: example.response.content_type.to_s,
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
|
1
3
|
module LazyApiDoc
|
2
4
|
class Generator
|
3
5
|
attr_reader :examples
|
@@ -14,7 +16,7 @@ module LazyApiDoc
|
|
14
16
|
|
15
17
|
def result
|
16
18
|
result = {}
|
17
|
-
@examples.group_by { |
|
19
|
+
@examples.sort_by(&:source_location).group_by { |ex| [ex.controller, ex.action] }.map do |_, examples|
|
18
20
|
first = examples.first
|
19
21
|
route = ::LazyApiDoc::RouteParser.new(first.controller, first.action, first.verb).route
|
20
22
|
doc_path = route[:doc_path]
|
@@ -24,18 +26,19 @@ module LazyApiDoc
|
|
24
26
|
result
|
25
27
|
end
|
26
28
|
|
27
|
-
def example_group(example, examples, route)
|
29
|
+
def example_group(example, examples, route) # rubocop:disable Metrics/AbcSize
|
28
30
|
{
|
29
31
|
route[:verb].downcase => {
|
30
32
|
"tags" => [example.controller],
|
31
33
|
"description" => example["description"].capitalize,
|
32
34
|
"summary" => example.action,
|
33
|
-
"parameters" => path_params(route, examples),
|
35
|
+
"parameters" => path_params(route, examples) + query_params(examples),
|
34
36
|
"requestBody" => body_params(route, examples),
|
35
37
|
"responses" => examples.group_by { |ex| ex.response[:code] }.map do |code, variants|
|
36
38
|
[
|
37
39
|
code,
|
38
40
|
{
|
41
|
+
"description" => variants.first["description"].capitalize,
|
39
42
|
"content" => {
|
40
43
|
example.response[:content_type] => {
|
41
44
|
"schema" => ::LazyApiDoc::VariantsParser.new(variants.map { |v| parse_body(v.response) }).result
|
@@ -43,8 +46,8 @@ module LazyApiDoc
|
|
43
46
|
}
|
44
47
|
}
|
45
48
|
]
|
46
|
-
end.to_h
|
47
|
-
}
|
49
|
+
end.to_h # rubocop:disable Style/MultilineBlockChain
|
50
|
+
}.reject { |_, v| v.nil? }
|
48
51
|
}
|
49
52
|
end
|
50
53
|
|
@@ -59,10 +62,32 @@ module LazyApiDoc
|
|
59
62
|
end
|
60
63
|
|
61
64
|
def path_params(route, examples)
|
62
|
-
|
63
|
-
::LazyApiDoc::VariantsParser.new(
|
65
|
+
path_variants = examples.map { |example| example.params.slice(*route[:path_params]) }
|
66
|
+
::LazyApiDoc::VariantsParser.new(path_variants).result["properties"].map do |param_name, schema|
|
64
67
|
{
|
65
68
|
'in' => "path",
|
69
|
+
'required' => true,
|
70
|
+
'name' => param_name,
|
71
|
+
'schema' => schema
|
72
|
+
}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def query_params(examples)
|
77
|
+
query_variants = examples.map do |example|
|
78
|
+
full_path = example.request[:full_path].split('?')
|
79
|
+
next {} if full_path.size == 1
|
80
|
+
|
81
|
+
# TODO: simplify it
|
82
|
+
full_path.last.split('&').map { |part| part.split('=').map { |each| CGI.unescape(each) } }.group_by(&:first)
|
83
|
+
.transform_values { |v| v.map(&:last) }.map { |k, v| [k, k.match?(/\[\]\z/) ? v : v.first] }.to_h
|
84
|
+
end
|
85
|
+
|
86
|
+
parsed = ::LazyApiDoc::VariantsParser.new(query_variants).result
|
87
|
+
parsed["properties"].map do |param_name, schema|
|
88
|
+
{
|
89
|
+
'in' => "query",
|
90
|
+
'required' => parsed['required'].include?(param_name),
|
66
91
|
'name' => param_name,
|
67
92
|
'schema' => schema
|
68
93
|
}
|
@@ -9,7 +9,7 @@ module LazyApiDoc
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def route
|
12
|
-
self.class.routes.find { |r| r[:action] == action && r[:controller] == controller && r[:verb]
|
12
|
+
self.class.routes.find { |r| r[:action] == action && r[:controller] == controller && r[:verb].include?(verb) }
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.routes
|
@@ -17,7 +17,7 @@ module LazyApiDoc
|
|
17
17
|
when Array
|
18
18
|
parse_array(variant, variants)
|
19
19
|
when Hash
|
20
|
-
parse_hash(
|
20
|
+
parse_hash(variants)
|
21
21
|
else
|
22
22
|
types_template(variants).merge("example" => variant)
|
23
23
|
end
|
@@ -50,6 +50,8 @@ module LazyApiDoc
|
|
50
50
|
"boolean"
|
51
51
|
when String
|
52
52
|
type_of_string(variant)
|
53
|
+
when Float
|
54
|
+
'number'
|
53
55
|
else
|
54
56
|
variant.class.name.downcase
|
55
57
|
end
|
@@ -64,8 +66,10 @@ module LazyApiDoc
|
|
64
66
|
end
|
65
67
|
end
|
66
68
|
|
67
|
-
def parse_hash(
|
68
|
-
result
|
69
|
+
def parse_hash(variants)
|
70
|
+
result = types_template(variants)
|
71
|
+
variant = variants.select { |v| v.instance_of?(Hash) }.reverse_each
|
72
|
+
.each_with_object({}) { |v, res| res.merge!(v) }
|
69
73
|
result["properties"] = variant.map do |key, val|
|
70
74
|
[
|
71
75
|
key.to_s,
|
data/lib/lazy_api_doc/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_api_doc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Guban
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-04-
|
11
|
+
date: 2020-04-10 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: The gem collects all requests and responses from your request specs and
|
14
14
|
generates documentationbased on it
|