class Jimmy::SchemaWithURI

Represents a schema with a URI.

Attributes

schema[R]

@return [Schema]

uri[R]

@return [Json::URI]

Public Class Methods

new(uri, schema) click to toggle source

@param [URI, String, Json::URI] uri @param [Schema] schema

# File lib/jimmy/schema_with_uri.rb, line 13
def initialize(uri, schema)
  @uri    = Json::URI.new(uri)
  @schema = schema
  freeze
end

Public Instance Methods

==(other) click to toggle source

Returns true if other has a matching URI and Schema. @param [SchemaWithURI] other @return [true, false]

# 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(*) click to toggle source

@return [Hash{String => Object}]

# File lib/jimmy/schema_with_uri.rb, line 20
def as_json(*)
  @schema.as_json id: @uri
end
resolve(uri) click to toggle source

Attempt to resolve URI using {#schema}. This will only succeed if uri represents a fragment of {#schema}. @raise [Error::BadArgument] Raised if the URI is outside {#uri}. @param [String, URI, Json::URI] uri @return [SchemaWithURI, nil]

# 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) click to toggle source

@return [String]

# File lib/jimmy/schema_with_uri.rb, line 25
def to_json(**opts)
  ::JSON.generate as_json, **opts
end