class DocWriter

This class writes the parsed documenation to the swagger_helper file.

Public Instance Methods

apply_original_indentation(new_documentation, fragment_title) click to toggle source
# File lib/doc_writer.rb, line 38
def apply_original_indentation(new_documentation, fragment_title)
  indentation = ' ' * original_indent_level(fragment_title)
  new_documentation.split("\n").join("\n" + indentation)
end
docs_path() click to toggle source
# File lib/doc_writer.rb, line 5
def docs_path
  Rails.root.join('spec', 'swagger_helper.rb').to_s
end
example_hash(fragment_title, old_documentation) click to toggle source
# File lib/doc_writer.rb, line 19
def example_hash(fragment_title, old_documentation)
  match = ''
  iterator = 1
  loop do
    regex = /(#{fragment_title})(.*?\}){#{iterator}}/
    new_match_data = old_documentation.match(regex)
    new_match = new_match_data.present? ? new_match_data[0] : nil
    return unless new_match != match

    match = new_match
    begin
      return match if eval("{#{match}}")
    rescue SyntaxError
    end

    iterator += 1
  end
end
original_indent_level(fragment_title) click to toggle source
# File lib/doc_writer.rb, line 43
def original_indent_level(fragment_title)
  file = File.read(docs_path)
  file.match(/\s+(?=#{fragment_title})/)[0].size - 1
end
write_docs(docs, fragment_title) click to toggle source
# File lib/doc_writer.rb, line 9
def write_docs(docs, fragment_title)
  old_documentation = File.read(docs_path)
  old_documentation_fragment = example_hash(fragment_title, old_documentation)
  return if old_documentation_fragment.nil?

  new_documentation_fragment = apply_original_indentation(docs, fragment_title)
  new_documentation = old_documentation.sub(old_documentation_fragment, new_documentation_fragment)
  File.open(docs_path, 'w') { |f| f.write(new_documentation) }
end