class Dictum::HtmlWriter
rubocop:disable ClassLength
Constants
- BACK_TEXT
- ERROR_CODE_TEXT
- ERROR_CODE_URL
- RESOURCES_TEXT
Attributes
header_title[R]
output_dir[R]
output_file[R]
temp_json[R]
temp_path[R]
Public Class Methods
new(output_dir, temp_path, config)
click to toggle source
# File lib/dictum/html_writer.rb, line 15 def initialize(output_dir, temp_path, config) @output_dir = output_dir @temp_path = temp_path @temp_json = JSON.parse(File.read(temp_path)) @config = config @header_title = config[:header_title] end
Public Instance Methods
write()
click to toggle source
# File lib/dictum/html_writer.rb, line 23 def write Dir.mkdir(output_dir) unless Dir.exist?(output_dir) write_index write_pages end
Private Instance Methods
error_code_table_header()
click to toggle source
# File lib/dictum/html_writer.rb, line 125 def error_code_table_header %w[Code Description Message] end
error_codes()
click to toggle source
# File lib/dictum/html_writer.rb, line 62 def error_codes temp_json['error_codes'] end
error_codes_as_rows()
click to toggle source
# File lib/dictum/html_writer.rb, line 129 def error_codes_as_rows error_codes.map { |a| [a['code'], a['description'], a['message']] } end
resource_header_and_endpoints(resource_name, description, endpoints, builder)
click to toggle source
# File lib/dictum/html_writer.rb, line 77 def resource_header_and_endpoints(resource_name, description, endpoints, builder) builder.title(resource_name, 'title') + builder.paragraph(description) + write_endpoints(endpoints, builder) end
resources()
click to toggle source
# File lib/dictum/html_writer.rb, line 58 def resources temp_json['resources'] end
write_codeblock(text, json, builder)
click to toggle source
# File lib/dictum/html_writer.rb, line 109 def write_codeblock(text, json, builder) return unless text && json && builder sanitized_json = json.empty? ? {} : json builder.code_block(text, JSON.pretty_generate(sanitized_json)) end
write_endpoints(endpoints, builder)
click to toggle source
# File lib/dictum/html_writer.rb, line 82 def write_endpoints(endpoints, builder) answer = '' endpoints.each do |endpoint| answer += builder.subtitle("#{endpoint['http_verb']} #{endpoint['endpoint']}") answer += builder.paragraph(endpoint['description']) answer += write_request_parameters(endpoint, builder) answer += write_response(endpoint, builder) end answer end
write_error_codes_page()
click to toggle source
# File lib/dictum/html_writer.rb, line 115 def write_error_codes_page html = HtmlHelpers.build do |b| content = b.title(ERROR_CODE_TEXT, 'title') content += b.table(error_code_table_header, error_codes_as_rows) container = b.container(b.row(content) + b.row(b.button(BACK_TEXT))) b.html_header(header_title, container, @config[:inline_css]) end write_to_file("#{output_dir}/#{ERROR_CODE_URL}.html", html) end
write_index()
click to toggle source
rubocop:disable LineLength rubocop:disable AbcSize
# File lib/dictum/html_writer.rb, line 39 def write_index html = HtmlHelpers.build do |b| content = b.jumbotron(b.title(@config[:index_title], 'title')) content += b.title(RESOURCES_TEXT) content += b.unordered_list(resources.keys) content += b.link("#{ERROR_CODE_URL}.html", b.subtitle(ERROR_CODE_TEXT)) unless error_codes.empty? container = b.container(b.row(content)) b.html_header(header_title, container, @config[:inline_css]) end write_to_file("#{output_dir}/index.html", html) end
write_page(resource_name, information)
click to toggle source
# File lib/dictum/html_writer.rb, line 66 def write_page(resource_name, information) html = HtmlHelpers.build do |b| content = resource_header_and_endpoints( resource_name, information['description'], information['endpoints'], b ) container = b.container(b.row(content) + b.row(b.button(BACK_TEXT))) b.html_header(header_title, container, @config[:inline_css]) end write_to_file("#{output_dir}/#{resource_name.downcase}.html", html) end
write_pages()
click to toggle source
# File lib/dictum/html_writer.rb, line 51 def write_pages resources.each do |resource_name, information| write_page(resource_name, information) end write_error_codes_page unless error_codes.empty? end
write_request_parameters(endpoint, builder)
click to toggle source
# File lib/dictum/html_writer.rb, line 93 def write_request_parameters(endpoint, builder) write_codeblock('Request headers', endpoint['request_headers'], builder) + write_codeblock('Request path parameters', endpoint['request_path_parameters'], builder) + write_codeblock('Request body parameters', endpoint['request_body_parameters'], builder) end
write_response(endpoint, builder)
click to toggle source
# File lib/dictum/html_writer.rb, line 99 def write_response(endpoint, builder) answer = builder.code_block('Status', endpoint['response_status']) answer += write_codeblock('Response headers', endpoint['response_headers'], builder) if endpoint['response_headers'] if endpoint['response_body'] param = endpoint['response_body'] == 'no_content' ? {} : endpoint['response_body'] answer += write_codeblock('Response body', param, builder) end answer end
write_to_file(file_path, content)
click to toggle source
# File lib/dictum/html_writer.rb, line 31 def write_to_file(file_path, content) index = File.open(file_path, 'w+') index.puts(Nokogiri::HTML(content).to_html) index.close end