class Jekyll::RamlSchemaGenerator

Utility class for creating schema (current JSON, perhaps XML someday) based on existing RAML formParameters

Public Class Methods

new(site, title=nil) click to toggle source
# File lib/utils.rb, line 27
def initialize(site, title=nil)
  @site = site
  @title = title
  @current_method = nil
end

Public Instance Methods

generate_json_schema(obj) click to toggle source

Creates JSON Schema - as a string - based on obj['application/x-www-form-urlencoded’]

# File lib/utils.rb, line 66
def generate_json_schema(obj)

  # JSON Schema spec: http://json-schema.org/latest/json-schema-validation.html
  schema_hash = {}
  schema_hash['$schema'] = @site.config['json_schema_schema_uri']
  schema_hash['title'] = @title if @title
  schema_hash['description'] = Jekyll::sanatize_json_string(obj['description']) if obj.include?('description')
  schema_hash['type'] = 'object'

  required_properties = []
  schema_hash['properties'] = DeepClone.clone obj['body']['application/x-www-form-urlencoded']['formParameters']
  schema_hash['properties'].each do |name, param|
    if param.include?('required')
      required_properties << name if param['required'] == true
      param.delete('required')
    end

    if param.include?('description')
      param['description'] = Jekyll::sanatize_json_string(param['description']) 
    end

    # Repeat and displayName are not supported keywords in JSON Schema
    param.delete('repeat')
    param.delete('displayName')
  end
  schema_hash['required'] = required_properties if not required_properties.empty?
  
  JSON.pretty_generate(schema_hash)
end
insert_json_schema(obj, schema) click to toggle source

Inserts provided JSON Schema into obj['application/json’]

# File lib/utils.rb, line 60
def insert_json_schema(obj, schema)
  obj['body']['application/json'] = {} if obj['body']['application/json'].nil?
  obj['body']['application/json']['schema'] = schema 
end
insert_json_schema?(obj) click to toggle source
# File lib/utils.rb, line 96
def insert_json_schema?(obj)
  return false if !obj['body'].include?('application/json')
  json_hash = obj['body']['application/json']
  json_hash = {} if json_hash.nil?
  !(json_hash.include?('schema'))
end
insert_schemas(obj) click to toggle source

Creates a schema attribute sibling of any formParameter attribute found, based on the found formParameters attribute.

Existing schema siblings of formParameter attributes are not modified.

Modifys obj, and returns the modified obj

# File lib/utils.rb, line 39
def insert_schemas(obj)
  if obj.is_a?(Array)
    obj.map!{|method| insert_schemas(method)}
  elsif obj.is_a?(Hash)
    @current_method = obj['method'] if obj.include?('method')
    
    obj.each { |k, v| obj[k] = insert_schemas(v)}

    if obj.include?('body')
      if obj['body'].fetch('application/x-www-form-urlencoded', {}).include?('formParameters')
        if insert_json_schema?(obj)
          insert_json_schema(obj, generate_json_schema(obj))
        end
      end
    end
  end

  obj
end