Sha256: b9b42bb829a3870b27fdd88e4397ce0baac27c7cdd96f83d2ae155cc5ab3abad

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'json'

module Swagger2md
  module Converter
    def self.to_markdown file_path
      docs_file = File.read(file_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)
    end

    def self.export(content, output='docs.md')
      File.open(output, 'w') do |file|
        file.write(content)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
swagger2md-0.1.0 lib/swagger2md/converter.rb