class RDF::Microdata::Registry

Interface to registry

Attributes

properties[R]

@return [Hash] properties

uri[R]

@return [RDF::URI] Prefix of vocabulary

Public Class Methods

find(type) click to toggle source

Find a registry entry given a type URI

@param [RDF::URI] type @return [Registry]

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

Initialize the registry from a URI or file path

@param [String] registry_uri

# File 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
new(prefixURI, properties = {}) click to toggle source

Initialize registry for a particular prefix URI

@param [RDF::URI] prefixURI @param [Hash] properties ({})

# File 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

Public Instance Methods

expand(predicateURI) { |URI(equiv)| ... } click to toggle source

Yield a equivalentProperty or subPropertyOf if appropriate

@param [RDF::URI] predicateURI @yield equiv @yieldparam [RDF::URI] equiv

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

Fragment escape a name

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

Generate a predicateURI given a ‘name`

@param [#to_s] name @param [RDF::URI] base_uri base URI for resolving ‘name`. @return [RDF::URI]

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

Turn a predicateURI into a simple token @param [RDF::URI] predicateURI @return [String]

# File lib/rdf/microdata/registry.rb, line 98
def tokenize(predicateURI)
  predicateURI.to_s.sub(@property_base, '')
end