Class: Sinatra::Schema::JsonSchema
- Inherits:
-
Object
- Object
- Sinatra::Schema::JsonSchema
- Defined in:
- lib/sinatra/schema/json_schema.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #dump_definition(definition) ⇒ Object
- #dump_link(link) ⇒ Object
- #dump_resource(resource) ⇒ Object
- #dump_root ⇒ Object
-
#initialize(root) ⇒ JsonSchema
constructor
A new instance of JsonSchema.
Constructor Details
#initialize(root) ⇒ JsonSchema
Returns a new instance of JsonSchema.
10 11 12 |
# File 'lib/sinatra/schema/json_schema.rb', line 10 def initialize(root) @root = root end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
4 5 6 |
# File 'lib/sinatra/schema/json_schema.rb', line 4 def root @root end |
Class Method Details
Instance Method Details
#dump_definition(definition) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# 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_link(link) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/sinatra/schema/json_schema.rb', line 47 def dump_link(link) { description: link.description, href: link.href, method: link.method.to_s.upcase, } end |
#dump_resource(resource) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# 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 ⇒ Object
14 15 16 17 18 19 20 21 |
# 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 |