Class: JsonSchemaDocs::Parser
- Inherits:
-
Object
- Object
- JsonSchemaDocs::Parser
- Includes:
- Helpers
- Defined in:
- lib/json-schema-docs/parser.rb
Constant Summary
Constants included from Helpers
Helpers::SLUGIFY_PRETTY_REGEXP
Instance Attribute Summary collapse
-
#processed_schema ⇒ Object
readonly
Returns the value of attribute processed_schema.
Attributes included from Helpers
Instance Method Summary collapse
-
#initialize(schema, options) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
Methods included from Helpers
#include, #markdownify, #schemata, #slugify, #types
Constructor Details
#initialize(schema, options) ⇒ Parser
Returns a new instance of Parser.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/json-schema-docs/parser.rb', line 11 def initialize(schema, ) = if schema.is_a?(Prmd::Schema) @schema = schema else # FIXME: Multiloader has issues: https://github.com/interagent/prmd/issues/279 # for now just always assume a JSON file data = Prmd::MultiLoader::Json.load_data(schema) @schema = Prmd::Schema.new(data) end @processed_schema = {} end |
Instance Attribute Details
#processed_schema ⇒ Object (readonly)
Returns the value of attribute processed_schema.
9 10 11 |
# File 'lib/json-schema-docs/parser.rb', line 9 def processed_schema @processed_schema end |
Instance Method Details
#parse ⇒ Object
27 28 29 30 31 32 33 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/json-schema-docs/parser.rb', line 27 def parse @schema['properties'].each_key do |key| resource, property = key, @schema['properties'][key] begin _, schemata = @schema.dereference(property) # establish condensed object description if schemata['properties'] && !schemata['properties'].empty? schemata['property_refs'] = [] refs = extract_schemata_refs(@schema, schemata['properties']).map { |v| v && v.split('/') } extract_attributes(@schema, schemata['properties']).each_with_index do |(key, type, description, example), i| property_ref = { type: type, description: description, example: example} if refs[i] && refs[i][1] == 'definitions' && refs[i][2] != resource property_ref[:name] = '[%s](#%s)' % [key, 'resource-' + refs[i][2]] else property_ref[:name] = key end schemata['property_refs'].push(property_ref) schemata['example'] = pretty_json(@schema.schemata_example(resource)) end end schemata['links'] ||= [] # establish full link description schemata['links'].each do |link, datum| link_path = build_link_path(@schema, link) response_example = link['response_example'] if link.has_key?('schema') && link['schema'].has_key?('properties') required, optional = Prmd::Link.new(link).required_and_optional_parameters unless required.empty? link_schema_required_properties = extract_attributes(@schema, required).map do |(name, type, description, example)| { name: name, type: type, description: description, example: example} end end unless optional.empty? link_schema_optional_properties = extract_attributes(@schema, optional).map do |(name, type, description, example)| { name: name, type: type, description: description, example: example} end end end link['link_path'] = link_path link['required_properties'] = link_schema_required_properties || [] link['optional_properties'] = link_schema_optional_properties || [] link['example'] = generate_example(link, link_path) link['response'] = { header: generate_response_header(response_example, link), example: generate_response_example(response_example, link, resource) } end @processed_schema[resource] = schemata rescue => e $stdout.puts("Error in resource: #{resource}") raise e end end @processed_schema end |