Class: N::URI

Inherits:
Object
  • Object
show all
Includes:
RDFS::ResourceLike
Defined in:
lib/semantic_naming/uri.rb

Overview

This class contains basic functionality for URIs

Direct Known Subclasses

Namespace, Predicate, SourceClass

Constant Summary collapse

@@registered_uris =

Contains the registered uris

Hash.new
@@inverse_register =

Contains an inverse hash to lookup the shortcuts by uir

Hash.new
@@domainsplit_re =

Regexp that can match the part up until last # or / character, and the part behind that (domain, localname)

Regexp.compile("(.*[/#])([^/#]*)$")
@@lang_re =

Match the language part in labels

/(.*)@(.+)$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_s) ⇒ URI

Create a new URI



29
30
31
32
33
# File 'lib/semantic_naming/uri.rb', line 29

def initialize(uri_s)
  uri_s = uri_s.to_s
  # TODO: More checking
  @uri_s = uri_s
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

See const_missing

Raises:

  • (NoMethodError)


103
104
105
106
107
108
# File 'lib/semantic_naming/uri.rb', line 103

def method_missing(method, *args)
  # Quick sanity check: args make no sense for this
  raise(NoMethodError, "Undefined method: " + method.to_s) if(args && args.size > 0)    
  
  return URI.new(@uri_s + method.to_s)
end

Class Method Details

.[](shortcut) ⇒ Object

Request URI by shortcut. If called on a sublass, this accessor will only return an URI if it’s of the same type as the subclass.



121
122
123
124
125
126
127
128
# File 'lib/semantic_naming/uri.rb', line 121

def self.[](shortcut)
  shortcut = shortcut.to_s.downcase.to_sym
  
  uri = @@registered_uris[shortcut]
  
  # We only return the uri if it's of the same kind as ourselves
  uri.kind_of?(self) ? uri : nil 
end

.from_encoded(encoded_uri) ⇒ Object

Creates a new URI object from a string that was encoded with #safe_encoded



264
265
266
267
# File 'lib/semantic_naming/uri.rb', line 264

def self.from_encoded(encoded_uri)
  uri = Base64.decode64(encoded_uri.gsub(/\Auri_/, '').gsub('-__-', '+').gsub('-___-', '/').gsub('-_-', '='))
  self.new(uri)
end

.is_uri?(uri_str) ⇒ Boolean

Check if a given string is an URI

Returns:

  • (Boolean)


232
233
234
# File 'lib/semantic_naming/uri.rb', line 232

def self.is_uri?(uri_str)
  uri_str =~ /:/
end

.make_uri(str, separator = ":", default_namespace = N::LOCAL) ⇒ Object

Get an URI string from the given string in ns:name notation



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/semantic_naming/uri.rb', line 148

def self.make_uri(str, separator = ":", default_namespace = N::LOCAL)
  type = str.split(separator)
  type = [type[1]] if(type[0] == "")
  if(type.size == 2)
    namespace = N::URI[type[0]]
    raise(ArgumentError, "Unknown namespace #{type[0]} for #{str}") unless(namespace)
    self.new(namespace + type[1])
  else
    default_namespace + type[0]
  end
end

.shortcut(shortcut, uri, force = false) ⇒ Object

Register a shortcut to the given URI. You may force to overwrite an existing shortcut, but this is not recommended. The option exists practically only to override default namespaces if there is a need.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/semantic_naming/uri.rb', line 209

def self.shortcut(shortcut, uri, force = false)
  shortcut = shortcut.to_s.downcase.to_sym
  constant = shortcut.to_s.upcase.to_sym
  
  # make an object of my own type
  uri = self.new(uri)
  
  if(!force && (@@registered_uris[shortcut] || N.const_defined?(constant)))
    raise(NameError, "Shortcut already defined: '#{shortcut.to_s}'")
  end
  
  if(!force && (@@inverse_register[uri.to_s]))
    raise(NameError, "Shortcut for this uri already exists: #{uri.to_s}")
  end
  
  @@registered_uris[shortcut] = uri
  @@inverse_register[uri.to_s] = shortcut
  N.const_set(constant, uri)
  
  return uri
end

.shortcut_exists?(shortcut) ⇒ Boolean

Check if a shortcut is registered

Returns:

  • (Boolean)


143
144
145
# File 'lib/semantic_naming/uri.rb', line 143

def self.shortcut_exists?(shortcut)
  @@registered_uris[shortcut.to_s.downcase.to_sym] != nil
end

.shortcutsObject

Returns a hash with all registered shortcuts. This will only return the shortcuts of the same class (and subclasses) than the one on which the method is called



133
134
135
136
137
138
139
140
# File 'lib/semantic_naming/uri.rb', line 133

def self.shortcuts
  shortcuts = {}
  @@registered_uris.each do |key, value|
    shortcuts[key] = value if(value.kind_of?(self))
  end
  
  shortcuts
end

Instance Method Details

#+(uri) ⇒ Object

Add operator



54
55
56
57
# File 'lib/semantic_naming/uri.rb', line 54

def +(uri)
  new_s = @uri_s + uri.to_s
  return URI.new(new_s)
end

#==(object) ⇒ Object

Compare operator



36
37
38
39
40
# File 'lib/semantic_naming/uri.rb', line 36

def ==(object)
  return true if eql?(object)
  return object == @uri_s if(object.kind_of?(String))
  return false
end

#const_missing(klass) ⇒ Object

This creates a helpers for a nice notation of like my_domain::myid



98
99
100
# File 'lib/semantic_naming/uri.rb', line 98

def const_missing(klass)
  return URI.new(@uri_s + klass.to_s)
end

#domain_of?(uri) ⇒ Boolean

Is true if this object describes the domain of the given uri, and the given uri is a resource in that domain

Returns:

  • (Boolean)


113
114
115
116
117
# File 'lib/semantic_naming/uri.rb', line 113

def domain_of?(uri)
  uri_s = uri.to_s
  
  (uri_s =~ /\A#{@uri_s}\w*/) != nil
end

#domain_partObject

Returns the domain part of the URI



172
173
174
175
176
177
178
179
180
# File 'lib/semantic_naming/uri.rb', line 172

def domain_part
  domainpart = nil
  
  if(md = @@domainsplit_re.match(@uri_s))
    domainpart = md[1]
  end
  
  domainpart ? URI.new(domainpart) : nil
end

#eql?(object) ⇒ Boolean

eql? compare operator

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/semantic_naming/uri.rb', line 43

def eql?(object)
  return object.to_s == @uri_s if(object.kind_of?(URI))
  return false
end

#hashObject

Returns a hash



49
50
51
# File 'lib/semantic_naming/uri.rb', line 49

def hash
  @uri_s.hash
end

#local?Boolean

Checks if the current URI is local

Returns:

  • (Boolean)


60
61
62
# File 'lib/semantic_naming/uri.rb', line 60

def local?
  N::LOCAL.domain_of?(self)
end

#local_nameObject

Returns the local name



161
162
163
164
165
166
167
168
169
# File 'lib/semantic_naming/uri.rb', line 161

def local_name
  localname = nil
  
  if(md = @@domainsplit_re.match(@uri_s))
    localname = md[2]
  end
  
  localname
end

#my_shortcutObject

Returns the shortcut for the current URI, or nil if no shortcut is defined for this URI



184
185
186
# File 'lib/semantic_naming/uri.rb', line 184

def my_shortcut
  @@inverse_register[@uri_s]
end

#namespaceObject

Returns the namespace of this URI, if the URI is part of a namespace. The rule is quite strict: The URI is only part of the namespace if it is the namespace itself or the namespace plus a single local part

If the URI is not part of a namespace, nil is returned



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/semantic_naming/uri.rb', line 194

def namespace
  nspace = nil
  
  domain_shortcut = domain_part ? domain_part.my_shortcut : nil
  
  if(domain_shortcut && URI[domain_shortcut].is_a?(Namespace))
    nspace = domain_shortcut
  end
  
  nspace
end

#rdf_label(language = 'en') ⇒ Object

If the RDF store is active and the class has an rdfs:label, this will be returned. Otherwise, this will return the short name of the current source (like ns:name)

You may give a language, if none is given ‘en’ is used by default. If no label with a language marker is found, the first none-language-label is used



243
244
245
246
247
248
249
# File 'lib/semantic_naming/uri.rb', line 243

def rdf_label(language = 'en')
  @labels ||= {}
  @labels[language] ||= label_by_lang(rdfs_labels, language)
  # The rdf label is cache, while the default may change
  @labels[language] ||= to_name_s
  @labels[language]
end

#remote?Boolean

Redirect for checking if this is remote

Returns:

  • (Boolean)


65
66
67
# File 'lib/semantic_naming/uri.rb', line 65

def remote?
  !local?
end

#safe_encodedObject

Encodes the uri in a modified base64 format that will always form a legal HTML id tag.



258
259
260
# File 'lib/semantic_naming/uri.rb', line 258

def safe_encoded
  'uri_' << Base64.encode64(@uri_s).gsub('=', '-_-').gsub('+', '-__-').gsub('/', '-___-').gsub(/\s/, '')
end

#to_name_s(separator = ':') ⇒ Object

Get a string representation in the form of ‘namespace:name’. It is possible to select a different separator from ‘:’

If this uri is not part of a namespace, the whole uri string will be returned.



87
88
89
90
91
92
93
94
# File 'lib/semantic_naming/uri.rb', line 87

def to_name_s(separator = ':')
  nspace = namespace
  if(nspace)
    "#{nspace}#{separator}#{local_name}"
  else
    @uri_s
  end
end

#to_sObject Also known as: uri

String representation is the uri itself



70
71
72
# File 'lib/semantic_naming/uri.rb', line 70

def to_s
  @uri_s
end

#to_uriObject

to_uri just returns a clone of itself



252
253
254
# File 'lib/semantic_naming/uri.rb', line 252

def to_uri
  self.clone
end

#to_yamlObject

YAML representation is the uri string



75
76
77
# File 'lib/semantic_naming/uri.rb', line 75

def to_yaml
  self.to_s.to_yaml
end