auto-rswag 0.1.0 → 0.1.1
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/lib/auto_rswag.rb +11 -8
- data/lib/doc_writer.rb +36 -27
- data/lib/printer.rb +11 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06f50ff94bb14438b211685cdb3d54144978cef3a42379c02ca1b5a954337a8a
|
4
|
+
data.tar.gz: 43b6f985bf697b4944a6e018c71ce1bd4daeb8072984205c731d76e650fd2930
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c405a46499c9984ab08e2eb17fba5cf5f89bd307c539f65c96edcc52f4b7041886ea01cf99047cbdb4cd8568f8475f71febc41742c0b264085d17b4c1ab82de
|
7
|
+
data.tar.gz: 84cdad3bde8f9fac167a492321fd41047d97f5c17f581efdccde57b77f9e823f14a6280dec652acb1e40fce6db6fbe6cc580b6e4d8aebba5fe5dfb2c6f183325
|
data/lib/auto_rswag.rb
CHANGED
@@ -1,21 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'json'
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
require 'printer.rb'
|
5
|
+
require 'auto_rswag_helper.rb'
|
6
|
+
require 'doc_writer.rb'
|
7
7
|
|
8
8
|
# This module hooks into the Rspec test to retrieve useful
|
9
9
|
# metadata and submit it to AutoRswagHelper for conversion.
|
10
10
|
module AutoRswag
|
11
11
|
def update_documentation
|
12
|
-
|
13
|
-
|
14
|
-
after do
|
12
|
+
after do |example|
|
13
|
+
title = example.metadata[:response][:schema]['$ref'].split('/').last
|
15
14
|
payload = AutoRswagHelper.convert_response(response)
|
16
15
|
AutoRswagHelper.map_fields(payload)
|
17
|
-
docs = SwaggerPrinter.print_swagger(payload,
|
18
|
-
DocWriter.write_docs(docs,
|
16
|
+
docs = SwaggerPrinter.print_swagger(payload, title)
|
17
|
+
DocWriter.new.write_docs(docs, title)
|
19
18
|
end
|
20
19
|
end
|
21
20
|
end
|
21
|
+
|
22
|
+
Rswag::Specs::ExampleGroupHelpers.module_eval do
|
23
|
+
include AutoRswag
|
24
|
+
end
|
data/lib/doc_writer.rb
CHANGED
@@ -2,37 +2,46 @@
|
|
2
2
|
|
3
3
|
# This class writes the parsed documenation to the swagger_helper file.
|
4
4
|
class DocWriter
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
5
|
+
def docs_path
|
6
|
+
Rails.root.join('spec', 'swagger_helper.rb').to_s
|
7
|
+
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
def write_docs(docs, fragment_title)
|
10
|
+
old_documentation = File.read(docs_path)
|
11
|
+
old_documentation_fragment = example_hash(fragment_title, old_documentation)
|
12
|
+
return if old_documentation_fragment.nil?
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
new_documentation_fragment = apply_original_indentation(docs, fragment_title)
|
15
|
+
new_documentation = old_documentation.sub(old_documentation_fragment, new_documentation_fragment)
|
16
|
+
File.open(docs_path, 'w') { |f| f.write(new_documentation) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def example_hash(fragment_title, old_documentation)
|
20
|
+
match = ''
|
21
|
+
iterator = 1
|
22
|
+
loop do
|
23
|
+
regex = /(#{fragment_title})(.*?\}){#{iterator}}/
|
24
|
+
new_match_data = old_documentation.match(regex)
|
25
|
+
new_match = new_match_data.present? ? new_match_data[0] : nil
|
26
|
+
return unless new_match != match
|
18
27
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
regex = /(#{fragment_title})(.*?\}){#{iterator}}/
|
24
|
-
new_match_data = old_documentation.match(regex)
|
25
|
-
new_match = new_match_data.present? ? new_match_data[0] : nil
|
26
|
-
return unless new_match != match
|
27
|
-
|
28
|
-
match = new_match
|
29
|
-
begin
|
30
|
-
return match if eval("{#{match}}")
|
31
|
-
rescue SyntaxError
|
32
|
-
end
|
33
|
-
|
34
|
-
iterator += 1
|
28
|
+
match = new_match
|
29
|
+
begin
|
30
|
+
return match if eval("{#{match}}")
|
31
|
+
rescue SyntaxError
|
35
32
|
end
|
33
|
+
|
34
|
+
iterator += 1
|
36
35
|
end
|
37
36
|
end
|
37
|
+
|
38
|
+
def apply_original_indentation(new_documentation, fragment_title)
|
39
|
+
indentation = ' ' * original_indent_level(fragment_title)
|
40
|
+
new_documentation.split("\n").join("\n" + indentation)
|
41
|
+
end
|
42
|
+
|
43
|
+
def original_indent_level(fragment_title)
|
44
|
+
file = File.read(docs_path)
|
45
|
+
file.match(/\s+(?=#{fragment_title})/)[0].size - 1
|
46
|
+
end
|
38
47
|
end
|
data/lib/printer.rb
CHANGED
@@ -39,13 +39,13 @@ class SwaggerPrinter
|
|
39
39
|
|
40
40
|
output = wrap_hash
|
41
41
|
object.each_with_index do |(key, val), i|
|
42
|
-
line =
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
42
|
+
line = if val[:type] == :object
|
43
|
+
print_hash(key, val)
|
44
|
+
elsif val[:type] == :array
|
45
|
+
print_array(key, val)
|
46
|
+
else
|
47
|
+
print_line(key, val)
|
48
|
+
end
|
49
49
|
comma = i == object.keys.size - 1 ? '' : ','
|
50
50
|
line += "#{comma}\n"
|
51
51
|
output += line
|
@@ -79,7 +79,8 @@ class SwaggerPrinter
|
|
79
79
|
def print_line(key, val)
|
80
80
|
line = ' ' * @indent + "#{key}: { "
|
81
81
|
val.each_with_index do |(val_key, val_val), j|
|
82
|
-
next if val_key == :type && val.any? { |k, v| k == :example && v
|
82
|
+
next if val_key == :type && val.any? { |k, v| k == :example && v.nil? }
|
83
|
+
|
83
84
|
val_comma = j == val.keys.size - 1 ? '' : ','
|
84
85
|
line += "#{escape_key(val_key)}: #{prettify_value(val, val_key, val_val)}#{val_comma} "
|
85
86
|
end
|
@@ -88,8 +89,8 @@ class SwaggerPrinter
|
|
88
89
|
|
89
90
|
def escape_key(key)
|
90
91
|
return key unless key.to_s.include?('-')
|
91
|
-
|
92
|
-
"'#{key
|
92
|
+
|
93
|
+
"'#{key}'"
|
93
94
|
end
|
94
95
|
|
95
96
|
def prettify_value(type, key, val)
|