Class: Rack::JsonSchema::Schema

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

Overview

Utility wrapper class for JsonSchema::Schema

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ Schema

Returns a new instance of Schema.

Examples:

hash = JSON.parse("schema.json")
schema = Rack::JsonSchema::Schema.new(hash)

Parameters:

  • schema (Hash)

Raises:

  • (JsonSchema::SchemaError)


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

Recursively extracts all links in given JSON schema

Parameters:

Returns:

  • (Array)

    An array of JsonSchema::Schema::Link



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

Returns Link defined for the given method and path.

Examples:

schema.has_link_for?(method: "GET", path: "/recipes/{+id}") #=> nil

Parameters:

  • method (String) (defaults to: nil)

    Uppercase HTTP method name (e.g. GET, POST)

  • path (String) (defaults to: nil)

    Path string, which may include URI template

Returns:

  • (JsonSchema::Scheam::Link, nil)

    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

Returns All links defined in given JSON schema.

Examples:

schema.links #=> [#<JsonSchema::Schema::Link>]

Returns:

  • (Array)

    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