class DiscoveryDoc::DocBuilder

Public Instance Methods

from_file(filepath) click to toggle source

Returns Markdown document from a file that specified by given filepath.

@param filepath [String] File path to a discovery file. @return String

# File lib/discovery_doc/discovery_doc.rb, line 9
def from_file(filepath)
  discovery = YAML.load_file(filepath)
  parse(discovery)
end
parse(discovery) click to toggle source

Returns built Markdown document.

@param discovery [Hash] @return String

# File lib/discovery_doc/discovery_doc.rb, line 18
def parse(discovery)
  doc = ""
  doc << parse_service(discovery["service"])
  doc << parse_endpoints(discovery["endpoint"])
  doc << parse_entities(discovery["entity"])
  doc
end
parse_endpoint(name, endpoint) click to toggle source

@param name [String] Endpoint’s name @param endpoint [Hash] @return String

# File lib/discovery_doc/discovery_doc.rb, line 78
def parse_endpoint(name, endpoint)
  doc = ""
  path = endpoint["path"]
  parameters = endpoint["params"]
  response = endpoint["response"]
  content_type = endpoint["content_type"]

  doc << "### `#{path}` (#{name}) \n\n"

  if content_type
    doc << "Content-Type: #{content_type}\n\n"
  end
  doc << parse_parameters(parameters)

  doc << parse_response(response)

  doc
end
parse_endpoints(endpoints) click to toggle source

@param endpoints [Hash]

# File lib/discovery_doc/discovery_doc.rb, line 64
def parse_endpoints(endpoints)
  doc = ""
  doc << "## Endpoint\n\n"

  endpoints.each do |name, endpoint|
    doc << parse_endpoint(name, endpoint)
  end

  doc
end
parse_entities(entities) click to toggle source

Working with Entity

# File lib/discovery_doc/discovery_doc.rb, line 47
def parse_entities(entities)
  doc = ""

  doc << "## Entity\n\n"

  entities.each do |name, entity|
    doc << "### #{name} \n\n"

    doc << table_from_field_type_hash(entity)
  end

  doc
end
parse_parameters(parameters) click to toggle source

@param parameters [Hash]

# File lib/discovery_doc/discovery_doc.rb, line 98
def parse_parameters(parameters)
  return "" unless parameters

  doc = ""
  doc << "#### Parameters \n\n"

  if parameters.is_a?(String) or parameters.is_a?(Array)
    doc << link_to_entity(parameters)
  elsif parameters.is_a?(Hash)
    doc << table_from_field_type_hash(parameters)
  end

  doc
end
parse_response(response) click to toggle source

@param response [String|Hash|Array]

# File lib/discovery_doc/discovery_doc.rb, line 114
def parse_response(response)
  doc = ""

  doc << "#### Response \n\n"

  if response.is_a?(String)
    doc << "[#{response}](##{response.downcase})\n\n"
  elsif response.is_a?(Array)
    doc << "[Array<#{response[0]}>](##{response[0].downcase})\n\n"
  else
    doc << table_from_field_type_hash(response)
  end

  doc
end
parse_service(service) click to toggle source

Working with Service

# File lib/discovery_doc/discovery_doc.rb, line 28
def parse_service(service)
  name = service["name"]
  base_url = service["base_url"]

  doc = ""

  doc << "## #{name} Service \n\n"

  doc << "### Base URL \n\n"

  doc << base_url

  doc << "\n\n"

  doc
end
table_from_field_type_hash(field_type_hash) click to toggle source

Returns table markdown with Field to Type hash.

@param field_type_hash [Hash] @return String

# File lib/discovery_doc/discovery_doc.rb, line 146
def table_from_field_type_hash(field_type_hash)
  doc = ""
  doc << "|Field|Type|\n"
  doc << "|-----|----|\n"
  field_type_hash.each do |field, type|
    type = "Array<#{type[0]}>" if type.is_a?(Array) ## For [Entity] literal.
    doc << "|#{field}|`#{type}`|\n"
  end
  doc << "\n"

  doc
end