Class: Curie

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf_objects/curies.rb

Constant Summary collapse

@@namespace_counter =
0

Class Method Summary collapse

Class Method Details

.create_from_uri(uri_string, prefix = nil) ⇒ Object

Automatically tries to build a safe curie from a uri string. Assumes an RDF Schema and a flat hierarchy.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rdf_objects/curies.rb', line 28

def self.create_from_uri(uri_string, prefix=nil)
  if curie = self.curie_from_uri(uri_string)
    return curie
  end
  uri = URI.parse(uri_string)
  ns = nil
  elem = nil    
  if uri.fragment
    ns, elem = uri.to_s.split('#')
    ns << '#'
  else
    elem = uri.path.split('/').last
    ns = uri.to_s.sub(/#{elem}$/, '')
  end  
  unless prefix
    prefix = "n#{@@namespace_counter}"
    @@namespace_counter += 1
  end
  Curie.add_prefixes!  prefix.to_s => ns
  self.curie_from_uri(uri_string)
end

.curie_from_uri(uri_string) ⇒ Object

Returns a Curie object from a fully qualified uri (assuming it is registered)



7
8
9
10
11
12
13
14
# File 'lib/rdf_objects/curies.rb', line 7

def self.curie_from_uri(uri_string)
  @@mappings.each do | prefix, uri |
    if m = uri_string.match(/^#{uri}(.*)/)
      return self.new(prefix, m[1]) if m[1]
    end
  end
  false
end

.get_mappingsObject



50
51
52
# File 'lib/rdf_objects/curies.rb', line 50

def self.get_mappings
  return @@mappings
end

.new_from_curie(curie_string) ⇒ Object

Return a Curie object from a safe curie string.



55
56
57
58
59
60
61
# File 'lib/rdf_objects/curies.rb', line 55

def self.new_from_curie(curie_string)
  unless curie_string.could_be_a_safe_curie?
    raise "not a real curie"
  end
  prefix, resource = curie_string.curie_parts
  return Curie.new(prefix, resource)
end

.prefix_for(uri_string) ⇒ Object

Returns the Curie prefix for a URI



17
18
19
20
21
22
23
24
# File 'lib/rdf_objects/curies.rb', line 17

def self.prefix_for(uri_string)
  @@mappings.each do | prefix, uri |
    if m = uri_string.match(/^#{uri}(.*)/)
      return prefix
    end
  end
  false
end