Class: GovukTechDocs::ApiReference::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_tech_docs/api_reference/api_reference_renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, document) ⇒ Renderer

Returns a new instance of Renderer.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 7

def initialize(app, document)
  @app = app
  @document = document

  # Load template files
  @template_api_full = get_renderer('api_reference_full.html.erb')
  @template_path = get_renderer('path.html.erb')
  @template_schema = get_renderer('schema.html.erb')
  @template_operation = get_renderer('operation.html.erb')
  @template_parameters = get_renderer('parameters.html.erb')
  @template_responses = get_renderer('responses.html.erb')
end

Instance Method Details

#api_full(info, servers) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 20

def api_full(info, servers)
  paths = ''
  paths_data = @document.paths
  paths_data.each do |path_data|
    # For some reason paths.each returns an array of arrays [title, object]
    # instead of an array of objects
    text = path_data[0]
    paths += path(text)
  end
  schemas = ''
  schemas_data = @document.components.schemas
  schemas_data.each do |schema_data|
    text = schema_data[0]
    schemas += schema(text)
  end
  @template_api_full.result(binding)
end

#json_output(schema) ⇒ Object



165
166
167
168
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 165

def json_output(schema)
  properties =  schema_properties(schema)
  JSON.pretty_generate(properties)
end

#json_prettyprint(data) ⇒ Object



170
171
172
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 170

def json_prettyprint(data)
  JSON.pretty_generate(data)
end

#markdown(text) ⇒ Object



159
160
161
162
163
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 159

def markdown(text)
  if text
    Tilt['markdown'].new(context: @app) { text }.render
  end
end

#operations(path, path_id) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 133

def operations(path, path_id)
  output = ''
  operations = get_operations(path)
  operations.compact.each do |key, operation|
    id = "#{path_id}-#{key.parameterize}"
    parameters = parameters(operation, id)
    responses = responses(operation, id)
    output += @template_operation.result(binding)
  end
  output
end

#parameters(operation, operation_id) ⇒ Object



145
146
147
148
149
150
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 145

def parameters(operation, operation_id)
  parameters = operation.parameters
  id = "#{operation_id}-parameters"
  output = @template_parameters.result(binding)
  output
end

#path(text) ⇒ Object



38
39
40
41
42
43
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 38

def path(text)
  path = @document.paths[text]
  id = text.parameterize
  operations = operations(path, id)
  @template_path.result(binding)
end

#responses(operation, operation_id) ⇒ Object



152
153
154
155
156
157
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 152

def responses(operation, operation_id)
  responses = operation.responses
  id = "#{operation_id}-responses"
  output = @template_responses.result(binding)
  output
end

#schema(text) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 45

def schema(text)
  schemas = ''
  schemas_data = @document.components.schemas
  schemas_data.each do |schema_data|
    all_of = schema_data[1]["allOf"]
    properties = []
    if !all_of.blank?
      all_of.each do |schema_nested|
        schema_nested.properties.each do |property|
          properties.push property
        end
      end
    end

    schema_data[1].properties.each do |property|
      properties.push property
    end

    if schema_data[0] == text
      title = schema_data[0]
      schema = schema_data[1]
      return @template_schema.result(binding)
    end
  end
end

#schema_properties(schema_data) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 174

def schema_properties(schema_data)
  properties = Hash.new
  if defined? schema_data.properties
    schema_data.properties.each do |key, property|
      properties[key] = property
    end
  end
  properties.merge! get_all_of_hash(schema_data)
  properties_hash = Hash.new
  properties.each do |pkey, property|
    if property.type == 'object'
      properties_hash[pkey] = Hash.new
      items = property.items
      if !items.blank?
        properties_hash[pkey] = schema_properties(items)
      end
      if !property.properties.blank?
        properties_hash[pkey] = schema_properties(property)
      end
    elsif property.type == 'array'
      properties_hash[pkey] = Array.new
      items = property.items
      if !items.blank?
        properties_hash[pkey].push schema_properties(items)
      end
    else
      properties_hash[pkey] = !property.example.nil? ? property.example : property.type
    end
  end

  properties_hash
end

#schemas_from_path(text) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 71

def schemas_from_path(text)
  path = @document.paths[text]
  operations = get_operations(path)
  # Get all referenced schemas
  schemas = []
  operations.compact.each_value do |operation|
    responses = operation.responses
    responses.each do |_rkey, response|
      if response.content['application/json']
        schema = response.content['application/json'].schema
        schema_name = get_schema_name(schema.node_context.source_location.to_s)
        if !schema_name.nil?
          schemas.push schema_name
        end
        schemas.concat(schemas_from_schema(schema))
      end
    end
  end
  # Render all referenced schemas
  output = ''
  schemas.uniq.each do |schema_name|
    output += schema(schema_name)
  end
  if !output.empty?
    output.prepend('<h2 id="schemas">Schemas</h2>')
  end
  output
end

#schemas_from_schema(schema) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 100

def schemas_from_schema(schema)
  schemas = []
  properties = []
  schema.properties.each do |property|
    properties.push property[1]
  end
  if schema.type == 'array'
    properties.push schema.items
  end
  all_of = schema["allOf"]
  if !all_of.blank?
    all_of.each do |schema_nested|
      schema_nested.properties.each do |property|
        properties.push property[1]
      end
    end
  end
  properties.each do |property|
    # Must be a schema be referenced by another schema
    # And not a property of a schema
    if property.node_context.referenced_by.to_s.include?('#/components/schemas') &&
        !property.node_context.source_location.to_s.include?('/properties/')
      schema_name = get_schema_name(property.node_context.source_location.to_s)
    end
    if !schema_name.nil?
      schemas.push schema_name
    end
    # Check sub-properties for references
    schemas.concat(schemas_from_schema(property))
  end
  schemas
end