class Shaf::ApiDoc::Document
Attributes
attributes[RW]
curies[RW]
embeds[RW]
links[RW]
model[W]
policy[RW]
serializer_class[RW]
Public Class Methods
new()
click to toggle source
# File lib/shaf/api_doc/document.rb, line 12 def initialize @model = nil @serializer_class = nil @policy = nil @attributes = {} @links = {} @curies = {} @embeds = {} @md = {} end
Public Instance Methods
attribute(attr, comment)
click to toggle source
# File lib/shaf/api_doc/document.rb, line 27 def attribute(attr, comment) @attributes[attr] = comment unless comment.empty? end
curie(rel, comment)
click to toggle source
# File lib/shaf/api_doc/document.rb, line 35 def curie(rel, comment) @curies[rel] = comment unless comment.empty? end
embedded(name, comment)
click to toggle source
# File lib/shaf/api_doc/document.rb, line 39 def embedded(name, comment) @embeds[strip_curie(name)] = comment unless comment.empty? end
generate_markdown!()
click to toggle source
# File lib/shaf/api_doc/document.rb, line 48 def generate_markdown! return @md unless @md.empty? generate_title! generate_policy! generate_section!(key: :attributes, heading: "Attributes") generate_section!(key: :curies, heading: "Curies", sub_title: "rel") generate_section!(key: :links, heading: "Links", sub_title: "rel") generate_section!(key: :embeds, heading: "Embedded resources", sub_title: "rel") @md[:doc] end
generate_yaml!()
click to toggle source
# File lib/shaf/api_doc/document.rb, line 60 def generate_yaml! generate_markdown! renderer = Redcarpet::Markdown.new(Redcarpet::Render::StripDown) hash = {} hash['policy'] = renderer.render(@md[:policy]).chomp if @md[:policy] [:attributes, :curies, :links, :embeds].each do |key| hash[key.to_s] = @md[key].map { |k, v| [k.to_s, renderer.render(v).chomp] }.to_h end hash.to_yaml end
link(rel, comment)
click to toggle source
# File lib/shaf/api_doc/document.rb, line 31 def link(rel, comment) @links[strip_curie(rel)] = comment unless comment.empty? end
markdown()
click to toggle source
# File lib/shaf/api_doc/document.rb, line 73 def markdown generate_markdown! @md[:doc] end
model()
click to toggle source
# File lib/shaf/api_doc/document.rb, line 23 def model @model || @serializer_class && @serializer_class.sub("Serializer", "") end
to_html()
click to toggle source
# File lib/shaf/api_doc/document.rb, line 78 def to_html options = {autolink: true, fenced_code_blocks: true} markdown_renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, options) html = markdown_renderer.render markdown # For some reason redcarpet don't like to surround my markdown code blocks # with <pre> tags, so let's fix that here. html.gsub!("<code>", "<pre><code>") html.gsub!("</code>", "</code></pre>") html end
valid?()
click to toggle source
# File lib/shaf/api_doc/document.rb, line 43 def valid? return false unless model attributes.merge(links).merge(curies).any? end
write_html(output)
click to toggle source
# File lib/shaf/api_doc/document.rb, line 89 def write_html(output) FileUtils.mkdir_p(output) unless Dir.exist? output File.open(File.join(output, "#{model.downcase}.html"), "w") do |file| file.write(to_html) end end
write_yaml(output)
click to toggle source
# File lib/shaf/api_doc/document.rb, line 96 def write_yaml(output) FileUtils.mkdir_p(output) unless Dir.exist? output File.open(File.join(output, "#{model.downcase}.yml"), "w") do |file| file.write(generate_yaml!) end end
Private Instance Methods
generate_policy!()
click to toggle source
# File lib/shaf/api_doc/document.rb, line 115 def generate_policy! return if policy.nil? @md[:doc] << "## Policy\n#{policy}\n" @md[:policy] = policy end
generate_section!(key:, heading:, sub_title: "")
click to toggle source
# File lib/shaf/api_doc/document.rb, line 121 def generate_section!(key:, heading:, sub_title: "") list = send(key) @md[:doc] << "## #{heading}\n" @md[key] = {} if list.empty? @md[:doc] << "This resource does not have any documented #{heading.downcase}\n" else sub_title += ": " unless sub_title.empty? list.each do |name, comment| @md[:doc] << "- *#{sub_title}#{name.tr('_', '-')}*\n #{comment.to_s.gsub("\n", "\n ")}\n\n" @md[key][name] = comment.to_s.chomp end end end
generate_title!()
click to toggle source
# File lib/shaf/api_doc/document.rb, line 110 def generate_title! @md[:doc] = "# #{model.capitalize}\n" @md[:title] = model.capitalize end
strip_curie(rel)
click to toggle source
# File lib/shaf/api_doc/document.rb, line 106 def strip_curie(rel) rel.split(':', 2)[1] || rel end