module Swagger2md::Converter

Public Class Methods

export(content, output) click to toggle source
# File lib/swagger2md/converter.rb, line 39
def self.export(content, output)
  File.open(output, 'w') do |file|
    file.write(content)
  end
end
to_markdown(input_path, output_path) click to toggle source
# File lib/swagger2md/converter.rb, line 6
def self.to_markdown input_path, output_path
  docs_file = File.read(input_path)
  docs_hash = JSON.parse(docs_file)
  content = "#{docs_hash['info']['title']}\n"\
            "======\n"\
            "**Version:** #{docs_hash['info']['version']}\n\n"
  content << "**Description:** #{docs_hash['info']['description']}\n\n" if docs_hash['info']['description']

  docs_hash['paths'].each do |path, infor|
    content << "### #{path}\n\n"
    content << "---\n"
    infor.each do |method, doc|
      content << "##### ***#{method.upcase}***\n"

      request_info = doc['parameters'][0]
      if request_info
        content << "**Parameters**\n"
        if request_info['schema']
          definition_name = (request_info['schema']['$ref'] || request_info['schema']["items"]["$ref"]).split("/").last
          request_params = docs_hash['definitions'][definition_name]['properties']
          content << Swagger2md::Utils.wrap_into_block(JSON.pretty_generate(request_params))
        end
      end

      if doc['responses']
        content << "**Responses**\n"
        content << Swagger2md::Utils.wrap_into_block(JSON.pretty_generate(doc['responses']))
      end
    end
  end
  export(content, output_path)
end