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
19
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 7

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

  # 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



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

def api_full(info, servers)
  paths = @document.paths.keys.inject("") do |memo, text|
    memo + path(text)
  end

  schema_names = @document.components.schemas.keys
  schemas = schema_names.inject("") do |memo, schema_name|
    memo + schema(schema_name)
  end

  @template_api_full.result(binding)
end

#json_output(schema) ⇒ Object



111
112
113
114
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 111

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

#json_prettyprint(data) ⇒ Object



116
117
118
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 116

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

#operations(path, path_id) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 90

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

#parameters(operation, operation_id) ⇒ Object



99
100
101
102
103
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 99

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

#path(text) ⇒ Object



34
35
36
37
38
39
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 34

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

#responses(operation, operation_id) ⇒ Object



105
106
107
108
109
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 105

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

#schema(title) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 41

def schema(title)
  schema = @document.components.schemas[title]
  return unless schema

  properties = if schema.all_of
                 schema.all_of.each_with_object({}) do |nested, memo|
                   memo.merge!(nested.properties.to_h)
                 end
               else
                 {}
               end

  properties.merge!(schema.properties.to_h)
  @template_schema.result(binding)
end

#schema_properties(schema_data) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 120

def schema_properties(schema_data)
  properties = schema_data.properties.to_h

  schema_data.all_of.to_a.each do |all_of_schema|
    properties.merge!(all_of_schema.properties.to_h)
  end

  properties.transform_values do |schema|
    case schema.type
    when "object"
      schema_properties(schema.items || schema)
    when "array"
      schema.items ? [schema_properties(schema.items)] : []
    else
      schema.example || schema.type
    end
  end
end

#schemas_from_path(text) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 57

def schemas_from_path(text)
  operations = get_operations(@document.paths[text])
  schemas = operations.flat_map do |_, operation|
    operation.responses.inject([]) do |memo, (_, response)|
      next memo unless response.content["application/json"]

      schema = response.content["application/json"].schema

      memo << schema.name if schema.name
      memo + schemas_from_schema(schema)
    end
  end

  # Render all referenced schemas
  output = schemas.uniq.inject("") do |memo, schema_name|
    memo + schema(schema_name)
  end

  output.prepend('<h2 id="schemas">Schemas</h2>') unless output.empty?
  output
end

#schemas_from_schema(schema) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/govuk_tech_docs/api_reference/api_reference_renderer.rb', line 79

def schemas_from_schema(schema)
  schemas = schema.properties.map(&:last)
  schemas << schema.items if schema.items && schema.type == "array"
  schemas += schema.all_of.to_a.flat_map { |s| s.properties.map(&:last) }

  schemas.flat_map do |nested|
    sub_schemas = schemas_from_schema(nested)
    nested.name ? [nested.name] + sub_schemas : sub_schemas
  end
end