class GovukTechDocs::ApiReference::Renderer

Public Class Methods

new(app, document) click to toggle source
# 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

Public Instance Methods

api_full(info, servers) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 20
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) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 110
def json_output(schema)
  properties = schema_properties(schema)
  JSON.pretty_generate(properties)
end
json_prettyprint(data) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 115
def json_prettyprint(data)
  JSON.pretty_generate(data)
end
operations(path, path_id) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 89
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) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 98
def parameters(operation, operation_id)
  parameters = operation.parameters
  id = "#{operation_id}-parameters"
  @template_parameters.result(binding)
end
path(text) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 33
def path(text)
  path = @document.paths[text]
  id = text.parameterize
  operations = operations(path, id)
  @template_path.result(binding)
end
responses(operation, operation_id) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 104
def responses(operation, operation_id)
  responses = operation.responses
  id = "#{operation_id}-responses"
  @template_responses.result(binding)
end
schema(title) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 40
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) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 119
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.each_with_object({}) do |(name, schema), memo|
    memo[name] = 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) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 56
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) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 78
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

Private Instance Methods

get_operations(path) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 146
def get_operations(path)
  {
    "get" => path.get,
    "put" => path.put,
    "post" => path.post,
    "delete" => path.delete,
    "patch" => path.patch,
  }.compact
end
get_renderer(file) click to toggle source
# File lib/govuk_tech_docs/api_reference/api_reference_renderer.rb, line 140
def get_renderer(file)
  template_path = File.join(File.dirname(__FILE__), "templates/" + file)
  template = File.open(template_path, "r").read
  ERB.new(template)
end