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.
52 53 54 55 |
# File 'lib/json-schema/schema/reader.rb', line 52 def initialize( = {}) @accept_uri = .fetch(:accept_uri, true) @accept_file = .fetch(:accept_file, true) end |
Instance Method Details
#accept_file?(pathname) ⇒ Boolean
86 87 88 89 90 91 92 |
# File 'lib/json-schema/schema/reader.rb', line 86 def accept_file?(pathname) if @accept_file.respond_to?(:call) @accept_file.call(pathname) else @accept_file end end |
#accept_uri?(uri) ⇒ Boolean
76 77 78 79 80 81 82 |
# File 'lib/json-schema/schema/reader.rb', line 76 def accept_uri?(uri) if @accept_uri.respond_to?(:call) @accept_uri.call(uri) else @accept_uri end end |
#read(location) ⇒ JSON::Schema
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/json-schema/schema/reader.rb', line 62 def read(location) uri = Addressable::URI.parse(location.to_s) body = if uri.scheme.nil? || uri.scheme == 'file' uri = Addressable::URI.convert_path(uri.path) read_file(Pathname.new(uri.path).) else read_uri(uri) end JSON::Schema.new(JSON::Validator.parse(body), uri) end |