Class: RDF::Microdata::Registry

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb

Overview

Interface to registry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefixURI, properties = {}) ⇒ Registry

Initialize registry for a particular prefix URI

Parameters:

  • prefixURI (RDF::URI)
  • properties (Hash) (defaults to: {})

    ({})



35
36
37
38
39
40
41
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb', line 35

def initialize(prefixURI, properties = {})
  @uri = prefixURI
  @properties = properties
  @property_base = prefixURI.to_s
  # Append a '#' for fragment if necessary
  @property_base += '#' unless %w(/ #).include?(@property_base[-1,1])
end

Instance Attribute Details

#propertiesHash (readonly)

Returns properties.

Returns:

  • (Hash)

    properties



10
11
12
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb', line 10

def properties
  @properties
end

#uriRDF::URI (readonly)

Returns Prefix of vocabulary.

Returns:



7
8
9
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb', line 7

def uri
  @uri
end

Class Method Details

.find(type) ⇒ Registry

Find a registry entry given a type URI

Parameters:

Returns:



48
49
50
51
52
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb', line 48

def self.find(type) 
  @prefixes ||= {}
  k = @prefixes.keys.detect {|key| type.to_s.index(key) == 0 }
  @prefixes[k] if k
end

.load_registry(registry_uri) ⇒ Object

Initialize the registry from a URI or file path

Parameters:



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb', line 16

def self.load_registry(registry_uri)
  return if @registry_uri == registry_uri

  json = RDF::Util::File.open_file(registry_uri) { |f| ::JSON.load(f) }

  @prefixes = {}
  json.each do |prefix, elements|
    next unless elements.is_a?(Hash)
    properties = elements.fetch("properties", {})
    @prefixes[prefix] = Registry.new(prefix, properties)
  end
  @registry_uri = registry_uri
end

Instance Method Details

#expand(predicateURI) {|equiv| ... } ⇒ Object

Yield a equivalentProperty or subPropertyOf if appropriate

Parameters:

Yields:

  • equiv

Yield Parameters:



84
85
86
87
88
89
90
91
92
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb', line 84

def expand(predicateURI)
  tok = tokenize(predicateURI)
  if @properties[tok].is_a?(Hash)
    value = @properties[tok].fetch("subPropertyOf", nil)
    value ||= @properties[tok].fetch("equivalentProperty", nil)

    Array(value).each {|equiv| yield RDF::URI(equiv)}
  end
end

#frag_escape(name) ⇒ Object

Fragment escape a name



104
105
106
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb', line 104

def frag_escape(name)
  name.to_s.gsub(/["#%<>\[\\\]^{|}]/) {|c| '%' + c.unpack('H2' * c.bytesize).join('%').upcase}
end

#predicateURI(name, base_uri) ⇒ RDF::URI

Generate a predicateURI given a name

Parameters:

  • name (#to_s)
  • base_uri (RDF::URI)

    base URI for resolving name.

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb', line 60

def predicateURI(name, base_uri)
  u = RDF::URI(name)
  # 1) If _name_ is an _absolute URL_, return _name_ as a _URI reference_
  return u if u.absolute?
  
  n = frag_escape(name)
  if uri.nil?
    # 2) If current vocabulary from context is null, there can be no current vocabulary.
    #    Return the URI reference that is the document base with its fragment set to the fragment-escaped value of name
    u = RDF::URI(base_uri.to_s)
    u.fragment = frag_escape(name)
    u
  else
    # 4) If scheme is vocabulary return the URI reference constructed by appending the fragment escaped value of name to current vocabulary, separated by a U+0023 NUMBER SIGN character (#) unless the current vocabulary ends with either a U+0023 NUMBER SIGN character (#) or SOLIDUS U+002F (/).
    RDF::URI(@property_base + n)
  end
end

#tokenize(predicateURI) ⇒ String

Turn a predicateURI into a simple token

Parameters:

Returns:



98
99
100
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/registry.rb', line 98

def tokenize(predicateURI)
  predicateURI.to_s.sub(@property_base, '')
end