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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7bd7f5d1bb6327a80a89497075356aafd2cdcba798b8d7258f2d31c682666a95
4
- data.tar.gz: b509baea5844020859126427811398fa40e79496a0d5e66ef3a881c08c841c39
3
+ metadata.gz: 06f50ff94bb14438b211685cdb3d54144978cef3a42379c02ca1b5a954337a8a
4
+ data.tar.gz: 43b6f985bf697b4944a6e018c71ce1bd4daeb8072984205c731d76e650fd2930
5
5
  SHA512:
6
- metadata.gz: db3929e25c816dcd3710c081eed70c089112149d1befb1570de3a84b9306dd4897191b6643a1c47e4c74e9391a4d1b150195172be6d2fbf05f50e33bbc5fdc2e
7
- data.tar.gz: 4ba7b0c799057de66e6ed2c8cc852cd67e8c15053344da60a453c0aa3dfeae861e4bacf14e07161cfd876d8f2a138145234afff7efc31cec7d741afc000992ef
6
+ metadata.gz: 3c405a46499c9984ab08e2eb17fba5cf5f89bd307c539f65c96edcc52f4b7041886ea01cf99047cbdb4cd8568f8475f71febc41742c0b264085d17b4c1ab82de
7
+ data.tar.gz: 84cdad3bde8f9fac167a492321fd41047d97f5c17f581efdccde57b77f9e823f14a6280dec652acb1e40fce6db6fbe6cc580b6e4d8aebba5fe5dfb2c6f183325
@@ -1,21 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
- require_relative 'printer.rb'
5
- require_relative 'auto_rswag_helper.rb'
6
- require_relative 'doc_writer.rb'
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
- $title = metadata[:response][:schema]['$ref'].split('/').last
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, $title)
18
- DocWriter.write_docs(docs, $title)
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
@@ -2,37 +2,46 @@
2
2
 
3
3
  # This class writes the parsed documenation to the swagger_helper file.
4
4
  class DocWriter
5
- class << self
6
- def default_docs_path
7
- Rails.root.join('spec', 'swagger_helper.rb').to_s
8
- end
5
+ def docs_path
6
+ Rails.root.join('spec', 'swagger_helper.rb').to_s
7
+ end
9
8
 
10
- def write_docs(docs, fragment_title)
11
- old_documentation = File.read(default_docs_path)
12
- new_documentation_fragment = example_hash(fragment_title, old_documentation)
13
- return if new_documentation_fragment.nil?
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
- new_documentation = old_documentation.sub(new_documentation_fragment, docs)
16
- File.open(default_docs_path, 'w') { |f| f.write(new_documentation) }
17
- end
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
- def example_hash(fragment_title, old_documentation)
20
- match = ''
21
- iterator = 1
22
- while true 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
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
@@ -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 = 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
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 == nil }
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.to_s}'"
92
+
93
+ "'#{key}'"
93
94
  end
94
95
 
95
96
  def prettify_value(type, key, val)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto-rswag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Porter