Class: Jimmy::SchemaWithURI

Inherits:
Object
  • Object
show all
Defined in:
lib/jimmy/schema_with_uri.rb

Overview

Represents a schema with a URI.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, schema) ⇒ SchemaWithURI

Returns a new instance of SchemaWithURI.

Parameters:



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

#schemaSchema (readonly)

Returns:



9
10
11
# File 'lib/jimmy/schema_with_uri.rb', line 9

def schema
  @schema
end

#uriJson::URI (readonly)

Returns:



7
8
9
# File 'lib/jimmy/schema_with_uri.rb', line 7

def uri
  @uri
end

Instance Method Details

#==(other) ⇒ true, false

Returns true if other has a matching URI and Schema.

Parameters:

Returns:

  • (true, false)


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_jsonHash{String => Object}

Returns:

  • (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?

Attempt to resolve URI using #schema. This will only succeed if uri represents a fragment of #schema.

Parameters:

Returns:

Raises:



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

Returns:

  • (String)


25
26
27
# File 'lib/jimmy/schema_with_uri.rb', line 25

def to_json(**opts)
  ::JSON.generate as_json, **opts
end