Class: Sinatra::Schema::JsonSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/sinatra/schema/json_schema.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#rootObject

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

.dump(root) ⇒ Object



6
7
8
# File 'lib/sinatra/schema/json_schema.rb', line 6

def self.dump(root)
  new(root).dump_root
end

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


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_rootObject



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