Class: Rack::JsonSchema::Schema
- Inherits:
-
Object
- Object
- Rack::JsonSchema::Schema
- Defined in:
- lib/rack/json_schema/schema.rb
Overview
Utility wrapper class for JsonSchema::Schema
Class Method Summary collapse
-
.extract_links(json_schema) ⇒ Array
Recursively extracts all links in given JSON schema.
Instance Method Summary collapse
-
#initialize(schema) ⇒ Schema
constructor
A new instance of Schema.
-
#link_for(method: nil, path: nil) ⇒ JsonSchema::Scheam::Link?
Link defined for the given method and path.
-
#links ⇒ Array
All links defined in given JSON schema.
Constructor Details
#initialize(schema) ⇒ Schema
Returns a new instance of Schema.
19 20 21 |
# File 'lib/rack/json_schema/schema.rb', line 19 def initialize(schema) @json_schema = ::JsonSchema.parse!(schema).tap(&:expand_references!) end |
Class Method Details
.extract_links(json_schema) ⇒ Array
Recursively extracts all links in given JSON schema
9 10 11 12 |
# File 'lib/rack/json_schema/schema.rb', line 9 def self.extract_links(json_schema) links = json_schema.links.select {|link| link.method && link.href } links + json_schema.properties.map {|key, schema| extract_links(schema) }.flatten end |
Instance Method Details
#link_for(method: nil, path: nil) ⇒ JsonSchema::Scheam::Link?
Returns Link defined for the given method and path.
28 29 30 31 32 |
# File 'lib/rack/json_schema/schema.rb', line 28 def link_for(method: nil, path: nil) links_indexed_by_method[method].find do |link| %r<^#{link.href.gsub(/(?:\{.*?\})|(?::\w+)/, "[^/]+")}$> === path end end |
#links ⇒ Array
Returns All links defined in given JSON schema.
37 38 39 |
# File 'lib/rack/json_schema/schema.rb', line 37 def links @links ||= self.class.extract_links(@json_schema) end |