class Fdoc::SchemaPresenter
An BasePresenter for a JSON Schema fragment. Like most JSON schema things, has a tendency to recurse.
Constants
- FORMATTED_KEYS
Public Class Methods
new(schema, options)
click to toggle source
Calls superclass method
Fdoc::BasePresenter::new
# File lib/fdoc/presenters/schema_presenter.rb, line 17 def initialize(schema, options) super(options) @schema = schema end
Public Instance Methods
deprecated?()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 120 def deprecated? @schema["deprecated"] end
enum_html()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 128 def enum_html enum = @schema["enum"] return unless enum list = enum.map do |e| '<tt>%s</tt>' % e end.join(", ") html = StringIO.new html << '<li>Enum: ' html << list html << '</li>' html.string end
example()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 114 def example return unless e = @schema["example"] Fdoc::JsonPresenter.new(e) end
format()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 110 def format @schema["format"] end
items_html()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 143 def items_html return unless items = @schema["items"] html = "" html << '<li>Items' sub_options = options.merge(:nested => true) if items.kind_of? Array item.compact.each do |item| html << self.class.new(item, sub_options).to_html end else html << self.class.new(items, sub_options).to_html end html << '</li>' html end
nested?()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 26 def nested? options[:nested] end
properties_html()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 163 def properties_html return unless properties = @schema["properties"] html = "" properties.each do |key, property| next if property.nil? html << '<li>' html << tag_with_anchor( 'span', '<tt>%s</tt>' % key, schema_slug(key, property) ) html << self.class.new(property, options.merge(:nested => true)).to_html html << '</li>' end html end
request?()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 22 def request? options[:request] end
required?()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 124 def required? @schema["required"] ? "yes" : "no" end
schema_slug(key, property)
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 183 def schema_slug(key, property) "#{key}-#{property.hash}" end
to_html()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 30 def to_html html = StringIO.new html << '<span class="deprecated">Deprecated</span>' if deprecated? html << '<div class="schema">' html << render_markdown(@schema["description"]) html << '<ul>' begin html << '<li>Required: %s</li>' % required? if nested? html << '<li>Type: %s</li>' % type if type html << '<li>Format: %s</li>' % format if format html << '<li>Example: %s</li>' % example.to_html if example html << enum_html (@schema.keys - FORMATTED_KEYS).each do |key| html << '<li>%s: %s</li>' % [ key, @schema[key] ] end html << items_html html << properties_html end html << '</ul>' html << '</div>' html.string end
to_markdown(prefix = "")
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 61 def to_markdown(prefix = "") md = StringIO.new md << 'Deprecated' if deprecated? md << "\n#{@schema["description"]}" md << "\n#{prefix}* __Required__: #{required?}" if nested? md << "\n#{prefix}* __Type__: #{type}" if type md << "\n#{prefix}* __Format__: #{format}" if format md << "\n#{prefix}* __Example__: <tt>#{example.to_markdown}</tt>" if example md << "\n#{@schema['enum']}" (@schema.keys - Fdoc::SchemaPresenter::FORMATTED_KEYS).each do |key| md << "\n#{prefix}* %{key} %{@schema[key]}" end if items = @schema["items"] md << "\n#{prefix}* Items" if items.kind_of? Array item.compact.each do |item| md << Fdoc::SchemaPresenter.new(item, options.merge(:nested => true)).to_markdown(prefix + "\t") end else md << Fdoc::SchemaPresenter.new(@schema["items"], options.merge(:nested => true)).to_markdown(prefix + "\t") end end if properties = @schema["properties"] properties.each do |key, property| next if property.nil? md << "\n#{prefix}* __#{key}__:" md << Fdoc::SchemaPresenter.new(property, options.merge(:nested => true)).to_markdown(prefix + "\t") end end md.string end
type()
click to toggle source
# File lib/fdoc/presenters/schema_presenter.rb, line 93 def type t = @schema["type"] if t.kind_of? Array types = t.map do |type| if type.kind_of? Hash '<li>%s</li>' % self.class.new(type, options).to_html else '<li>%s</li>' % type end end.join('') '<ul>%s</ul>' % types elsif t != "object" t end end