class Dictum::MarkdownWriter

Attributes

output_file[R]
output_path[R]
temp_json[R]
temp_path[R]

Public Class Methods

new(output_path, temp_path, config) click to toggle source
# File lib/dictum/markdown_writer.rb, line 7
def initialize(output_path, temp_path, config)
  @output_path = "#{output_path}.md"
  File.delete(@output_path) if File.exist?(@output_path)
  @temp_path = temp_path
  @temp_json = JSON.parse(File.read(temp_path))
  @config = config
end

Public Instance Methods

write() click to toggle source
# File lib/dictum/markdown_writer.rb, line 15
def write
  @output_file = File.open(output_path, 'a+')
  write_index
  write_temp_path
  write_error_codes
  output_file.close
end

Private Instance Methods

error_codes() click to toggle source
# File lib/dictum/markdown_writer.rb, line 87
def error_codes
  temp_json['error_codes']
end
print_subsubtitle(subtitle, contents) click to toggle source
print_subsubtitle_json(subtitle, contents) click to toggle source
write_endpoint_description(endpoint) click to toggle source
# File lib/dictum/markdown_writer.rb, line 61
def write_endpoint_description(endpoint)
  print_subsubtitle('Description', endpoint['description'])
  print_subsubtitle_json('Request headers', endpoint['request_headers'])
  print_subsubtitle_json('Path parameters', endpoint['request_path_parameters'])
  print_subsubtitle_json('Body Parameters', endpoint['request_body_parameters'])
end
write_endpoint_response(endpoint) click to toggle source
# File lib/dictum/markdown_writer.rb, line 68
def write_endpoint_response(endpoint)
  print_subsubtitle('Response status', endpoint['response_status'])
  print_subsubtitle_json('Response headers', endpoint['response_headers'])
  print_subsubtitle_json('Response body', endpoint['response_body'])
end
write_endpoints(endpoints) click to toggle source
# File lib/dictum/markdown_writer.rb, line 53
def write_endpoints(endpoints)
  endpoints.each do |endpoint|
    output_file.puts "## #{endpoint['http_verb']} #{endpoint['endpoint']}\n\n"
    write_endpoint_description(endpoint)
    write_endpoint_response(endpoint)
  end
end
write_error_codes() click to toggle source

rubocop:disable AbcSize

# File lib/dictum/markdown_writer.rb, line 42
def write_error_codes
  return if error_codes.empty?
  output_file.puts '# Error codes'
  output_file.puts '|Code|Message|Description|'
  output_file.puts '|----|----|----|'
  error_codes.each do |error|
    output_file.puts "|#{error['code']}|#{error['message']}|#{error['description']}|"
  end
  output_file.puts "\n"
end
write_index() click to toggle source
# File lib/dictum/markdown_writer.rb, line 25
def write_index
  output_file.puts "# #{@config[:index_title]}"
  @temp_json['resources'].each_key do |resource_name|
    output_file.puts "- #{resource_name}"
  end
  output_file.puts "\n"
end
write_temp_path() click to toggle source
# File lib/dictum/markdown_writer.rb, line 33
def write_temp_path
  @temp_json['resources'].each do |resource_name, information|
    output_file.puts "# #{resource_name}"
    output_file.puts "#{information['description']}\n\n"
    write_endpoints(information['endpoints'])
  end
end