class Sinatra::Schema::JsonSchema

Attributes

root[RW]

Public Class Methods

dump(root) click to toggle source
# File lib/sinatra/schema/json_schema.rb, line 6
def self.dump(root)
  new(root).dump_root
end
new(root) click to toggle source
# File lib/sinatra/schema/json_schema.rb, line 10
def initialize(root)
  @root = root
end

Public Instance Methods

dump_definition(definition) click to toggle source
# File lib/sinatra/schema/json_schema.rb, line 35
def dump_definition(definition)
  schema_type, schema_format = json_schema_type_and_format(definition.type)
  attrs = { type: schema_type }
  if schema_format
    attrs[:format] = schema_format
  end
  if definition.description
    attrs[:description] = definition.description
  end
  attrs
end
dump_resource(resource) click to toggle source
# File lib/sinatra/schema/json_schema.rb, line 23
def dump_resource(resource)
  {
    title: resource.title,
    description: resource.description,
    type: "object",
    definitions: resource.defs.inject({}) { |h, (id, definition)|
      h.merge(id => dump_definition(definition))
    },
    links: resource.links.map { |link| dump_link(link) }
  }
end
dump_root() click to toggle source
# File lib/sinatra/schema/json_schema.rb, line 14
def dump_root
  {
    "$schema" => "http://json-schema.org/draft-04/hyper-schema",
    "definitions" => root.resources.inject({}) { |result, (id, resource)|
      result.merge(id => dump_resource(resource))
    }
  }
end

Protected Instance Methods

json_schema_type_and_format(type) click to toggle source
# File lib/sinatra/schema/json_schema.rb, line 57
def json_schema_type_and_format(type)
  case type
  when "boolean"
    "boolean"
  when "datetime"
    ["string", "date-time"]
  when "email"
    ["string", "email"]
  when "integer"
    "integer"
  when "string"
    "string"
  when "uuid"
    ["string", "uuid"]
  end
end