Class: Jimmy::SchemaWithURI
- Inherits:
-
Object
- Object
- Jimmy::SchemaWithURI
- Defined in:
- lib/jimmy/schema_with_uri.rb
Overview
Represents a schema with a URI.
Instance Attribute Summary collapse
- #schema ⇒ Schema readonly
- #uri ⇒ Json::URI readonly
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Returns true if
other
has a matching URI and Schema. - #as_json ⇒ Hash{String => Object}
-
#initialize(uri, schema) ⇒ SchemaWithURI
constructor
A new instance of SchemaWithURI.
-
#resolve(uri) ⇒ SchemaWithURI?
Attempt to resolve URI using #schema.
- #to_json(**opts) ⇒ String
Constructor Details
#initialize(uri, schema) ⇒ SchemaWithURI
Returns a new instance of SchemaWithURI.
13 14 15 16 17 |
# File 'lib/jimmy/schema_with_uri.rb', line 13 def initialize(uri, schema) @uri = Json::URI.new(uri) @schema = schema freeze end |
Instance Attribute Details
#schema ⇒ Schema (readonly)
9 10 11 |
# File 'lib/jimmy/schema_with_uri.rb', line 9 def schema @schema end |
Instance Method Details
#==(other) ⇒ true, false
Returns true if other
has a matching URI and Schema.
32 33 34 |
# File 'lib/jimmy/schema_with_uri.rb', line 32 def ==(other) other.is_a?(self.class) && uri == other.uri && schema == other.schema end |
#as_json ⇒ Hash{String => Object}
20 21 22 |
# File 'lib/jimmy/schema_with_uri.rb', line 20 def as_json(*) @schema.as_json id: @uri end |
#resolve(uri) ⇒ SchemaWithURI?
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jimmy/schema_with_uri.rb', line 41 def resolve(uri) uri = Json::URI.new(uri) raise Error::BadArgument, 'Cannot resolve relative URIs' if uri.relative? raise Error::BadArgument, 'Wrong URI base' unless uri + '#' == @uri + '#' pointer = uri.pointer.remove_prefix(@uri.pointer) or return return unless (fragment = @schema.get_fragment(pointer)) self.class.new uri, fragment end |
#to_json(**opts) ⇒ String
25 26 27 |
# File 'lib/jimmy/schema_with_uri.rb', line 25 def to_json(**opts) ::JSON.generate as_json, **opts end |