Class: JSON::Schema::Reader
- Inherits:
-
Object
- Object
- JSON::Schema::Reader
- Defined in:
- lib/json-schema/schema/reader.rb
Overview
Instance Method Summary collapse
- #accept_file?(pathname) ⇒ Boolean
- #accept_uri?(uri) ⇒ Boolean
-
#initialize(options = {}) ⇒ Reader
constructor
The behavior of the schema reader can be controlled by providing callbacks to determine whether to permit reading referenced schemas.
- #read(location) ⇒ JSON::Schema
Constructor Details
#initialize(options = {}) ⇒ Reader
The behavior of the schema reader can be controlled by providing callbacks to determine whether to permit reading referenced schemas. The options accept_uri
and accept_file
should be procs which accept a URI
or Pathname
object, and return a boolean value indicating whether to read the referenced schema.
URIs using the file
scheme will be normalized into Pathname
objects and passed to the accept_file
callback.
73 74 75 76 |
# File 'lib/json-schema/schema/reader.rb', line 73 def initialize( = {}) @accept_uri = .fetch(:accept_uri, true) @accept_file = .fetch(:accept_file, true) end |
Instance Method Details
#accept_file?(pathname) ⇒ Boolean
109 110 111 112 113 114 115 |
# File 'lib/json-schema/schema/reader.rb', line 109 def accept_file?(pathname) if @accept_file.respond_to?(:call) @accept_file.call(pathname) else @accept_file end end |
#accept_uri?(uri) ⇒ Boolean
99 100 101 102 103 104 105 |
# File 'lib/json-schema/schema/reader.rb', line 99 def accept_uri?(uri) if @accept_uri.respond_to?(:call) @accept_uri.call(uri) else @accept_uri end end |
#read(location) ⇒ JSON::Schema
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/json-schema/schema/reader.rb', line 85 def read(location) uri = JSON::Util::URI.parse(location.to_s) body = if uri.scheme.nil? || uri.scheme == 'file' uri = JSON::Util::URI.file_uri(uri) read_file(Pathname.new(uri.path).) else read_uri(uri) end JSON::Schema.new(JSON::Validator.parse(body), uri) end |