Class: Lurker::SchemaPresenter

Inherits:
BasePresenter show all
Defined in:
lib/lurker/presenters/schema_presenter.rb

Overview

An BasePresenter for a JSON Schema fragment. Like most JSON schema things, has a tendency to recurse.

Constant Summary collapse

FORMATTED_KEYS =
%w(
  description
  type
  required
  example
  deprecated
  default
  format
  enum
  items
  properties
  $ref
)

Instance Attribute Summary collapse

Attributes inherited from BasePresenter

#options

Instance Method Summary collapse

Methods inherited from BasePresenter

#asset_path, #assets, #html_directory, #index_path, #markup, #tag_with_anchor, #url_base_path

Constructor Details

#initialize(schema, options) ⇒ SchemaPresenter

Returns a new instance of SchemaPresenter.



20
21
22
23
24
# File 'lib/lurker/presenters/schema_presenter.rb', line 20

def initialize(schema, options)
  options[:nested] ||= 0
  super(options)
  @schema = schema
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



4
5
6
# File 'lib/lurker/presenters/schema_presenter.rb', line 4

def schema
  @schema
end

Instance Method Details

#deprecated?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/lurker/presenters/schema_presenter.rb', line 94

def deprecated?
  @schema["deprecated"]
end

#enum_htmlObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lurker/presenters/schema_presenter.rb', line 102

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

#exampleObject



88
89
90
91
92
# File 'lib/lurker/presenters/schema_presenter.rb', line 88

def example
  return unless @schema.key?("example")

  Lurker::JsonPresenter.new(@schema["example"])
end

#formatObject



84
85
86
# File 'lib/lurker/presenters/schema_presenter.rb', line 84

def format
  @schema["format"]
end

#items_htmlObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/lurker/presenters/schema_presenter.rb', line 117

def items_html
  return unless items = @schema["items"]
  return if items.size == 0

  html = ""
  html << '<li>Items'

  sub_options = options.merge(:nested => options[:nested] + 1, :parent => self)

  if items.respond_to?(:each_pair)
    html << self.class.new(items, sub_options).to_html
  else
    items.each do |item|
      html << self.class.new(item, sub_options).to_html if item
    end
  end

  html << '</li>'
  html
end

#nested?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/lurker/presenters/schema_presenter.rb', line 30

def nested?
  options[:nested] > 0
end

#properties_htmlObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/lurker/presenters/schema_presenter.rb', line 138

def properties_html
  properties = if (props = @schema["properties"]).present?
    props
  elsif (ref_path = @schema["$ref"]).present?
    ref = Lurker::RefObject.new(ref_path, options[:root_path])
    options[:root_path] = options[:root_path].merge(ref_path.sub(/#[^#]*?$/, ''))
    ref.schema["properties"]
  else
    nil
  end

  return unless 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 => options[:nested] + 1, :parent => self)).to_html(key)
    html << '</li>'
  end

  html
end

#request?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/lurker/presenters/schema_presenter.rb', line 26

def request?
  options[:request]
end

#required?(parent_key = nil) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/lurker/presenters/schema_presenter.rb', line 98

def required?(parent_key=nil)
  ((options[:parent].schema['required'] || []).include?(parent_key)) ? "yes" : "no"
end

#schema_slug(key, property) ⇒ Object



168
169
170
# File 'lib/lurker/presenters/schema_presenter.rb', line 168

def schema_slug(key, property)
  "#{key}-#{options[:nested]}-#{property.hash}"
end

#to_html(parent_key = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lurker/presenters/schema_presenter.rb', line 34

def to_html(parent_key=nil)
  html = StringIO.new

  html << '<span class="deprecated">Deprecated</span>' if deprecated?

  html << '<div class="schema">'
  html << @schema["description"]

  html << '<ul>'
  begin
    html << '<li>Required: %s</li>' % required?(parent_key) 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|
      value = @schema[key]
      value = value.to_hash if value.is_a?(Lurker::Json::Schema)

      html << '<li>%s: %s</li>' % [key, value]
    end

    html << items_html
    html << properties_html
  end

  html << '</ul>'
  html << '</div>'

  html.string
end

#typeObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lurker/presenters/schema_presenter.rb', line 67

def type
  t = @schema["type"]
  if t.is_a? Array
    types = t.map do |type|
      if type.is_a? Hash
        '<li>%s</li>' % self.class.new(type, options.merge(parent: self)).to_html
      else
        '<li>%s</li>' % type
      end
    end.join('')

    '<ul>%s</ul>' % types
  elsif t != "object"
    t
  end
end