Class: GovukTechDocs::ApiReference::Renderer
- Inherits:
-
Object
- Object
- GovukTechDocs::ApiReference::Renderer
- Defined in:
- lib/govuk_tech_docs/api_reference/api_reference_renderer.rb
Instance Method Summary collapse
- #api_full(info, servers) ⇒ Object
-
#initialize(app, document) ⇒ Renderer
constructor
A new instance of Renderer.
- #json_output(schema) ⇒ Object
- #json_prettyprint(data) ⇒ Object
- #operations(path, path_id) ⇒ Object
- #parameters(operation, operation_id) ⇒ Object
- #path(text) ⇒ Object
- #responses(operation, operation_id) ⇒ Object
- #schema(title) ⇒ Object
- #schema_properties(schema_data) ⇒ Object
- #schemas_from_path(text) ⇒ Object
- #schemas_from_schema(schema) ⇒ Object
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 |
# 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) ⇒ Object
110 111 112 113 |
# 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) ⇒ Object
115 116 117 |
# 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) ⇒ Object
89 90 91 92 93 94 95 96 |
# 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) ⇒ Object
98 99 100 101 102 |
# 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) ⇒ Object
33 34 35 36 37 38 |
# 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) ⇒ Object
104 105 106 107 108 |
# 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) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# 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) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# 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) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# 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 |