Class: RDF::URI
Overview
A Uniform Resource Identifier (URI).
RDF::URI supports all the instance methods of Addressable::URI.
Constant Summary
- CACHE_SIZE =
Defines the maximum number of interned URI references that can be held cached in memory at any one time.
-1
Class Method Summary (collapse)
-
+ (RDF::URI) intern(str)
Returns an interned
RDF::URIinstance based on the givenuristring. -
+ (RDF::URI) parse(str)
Creates a new
RDF::URIinstance based on the givenuristring.
Instance Method Summary (collapse)
-
- (RDF::URI) +(other)
Simple concatenation operator.
-
- (RDF::URI) /(fragment)
'Smart separator' URI builder.
-
- (Boolean) ==(other)
Checks whether this URI is equal to
other. -
- (Boolean) ===(other)
Checks for case equality to the given
otherobject. -
- (Integer) =~(pattern)
Performs a pattern match using the given regular expression.
-
- (Boolean) anonymous?
Returns
false. -
- (RDF::URI) canonicalize
Returns a copy of this URI converted into its canonical lexical representation.
-
- (RDF::URI) canonicalize!
Converts this URI into its canonical lexical representation.
-
- (RDF::URI) dup
Returns a duplicate copy of
self. -
- (Boolean) end_with?(string)
(also: #ends_with?)
Returns
trueif this URI ends with the givenstring. -
- (Boolean) eql?(other)
Checks whether this URI is equal to
other. -
- (Boolean) has_parent?
Returns
trueif this URI's path component isn't equal to/. -
- (Fixnum) hash
Returns a hash code for this URI.
-
- (URI) initialize(uri_or_options)
constructor
A new instance of URI.
-
- (RDF::URI) join(*uris)
Joins several URIs together.
-
- (Integer) length
(also: #size)
Returns the string length of this URI.
-
- (RDF::URI) parent
Returns a copy of this URI with the path component ascended to the parent directory, if any.
-
- (Array(Symbol, Symbol)) qname
Returns a qualified name (QName) for this URI, if possible.
-
- (Boolean) respond_to?(symbol)
Returns
trueif this URI instance supports thesymbolmethod. -
- (RDF::URI) root
Returns a copy of this URI with the path component set to
/. -
- (Boolean) root?
Returns
trueif this URI's path component is equal to/. -
- (Boolean) start_with?(string)
(also: #starts_with?)
Returns
trueif this URI starts with the givenstring. -
- (String) to_str
(also: #to_s)
Returns the string representation of this URI.
-
- (RDF::URI) to_uri
Returns
self. -
- (Boolean) uri?
Returns
true. -
- (Boolean) url?
Returns
trueif this URI is a URL. -
- (Boolean) urn?
Returns
trueif this URI is a URN. -
- (RDF::URI) validate!
(also: #validate)
Validates this URI, raising an error if it is invalid.
Methods included from Resource
Methods included from Term
Methods included from Value
#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_rdf, #variable?
Constructor Details
- (Object) URI.new(uri) - (Object) URI.new(options = {})
A new instance of URI
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rdf/model/uri.rb', line 78 def initialize() case when Hash @uri = Addressable::URI.new() when Addressable::URI @uri = else @uri = Addressable::URI.parse(.to_s) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(symbol, *args) { ... } (protected)
552 553 554 555 556 557 558 559 560 561 562 |
# File 'lib/rdf/model/uri.rb', line 552 def method_missing(symbol, *args, &block) if @uri.respond_to?(symbol) case result = @uri.send(symbol, *args, &block) when Addressable::URI self.class.new(result) else result end else super end end |
Class Method Details
+ (RDF::URI) intern(str)
Returns an interned RDF::URI instance based on the given uri
string.
The maximum number of cached interned URI references is given by the
CACHE_SIZE constant. This value is unlimited by default, in which
case an interned URI object will be purged only when the last strong
reference to it is garbage collected (i.e., when its finalizer runs).
Excepting special memory-limited circumstances, it should always be
safe and preferred to construct new URI references using
RDF::URI.intern instead of RDF::URI.new, since if an interned
object can't be returned for some reason, this method will fall back
to returning a freshly-allocated one.
56 57 58 |
# File 'lib/rdf/model/uri.rb', line 56 def self.intern(str) (cache[str = str.to_s] ||= self.new(str)).freeze end |
+ (RDF::URI) parse(str)
Creates a new RDF::URI instance based on the given uri string.
This is just an alias for RDF::URI.new for compatibity
with Addressable::URI.parse.
68 69 70 |
# File 'lib/rdf/model/uri.rb', line 68 def self.parse(str) self.new(str) end |
Instance Method Details
- (RDF::URI) +(other)
Simple concatenation operator. Returns a URI formed from concatenating the string form of two elements.
For building URIs from fragments, you may want to use the smart
separator, #/. #join implements another set of URI building
semantics.
289 290 291 |
# File 'lib/rdf/model/uri.rb', line 289 def +(other) RDF::URI.intern(self.to_s + other.to_s) end |
- (RDF::URI) /(fragment)
'Smart separator' URI builder
This method attempts to use some understanding of the most common use cases for URLs and URNs to create a simple method for building new URIs from fragments. This means that it will always insert a separator of some sort, will remove duplicate seperators, will always assume that a fragment argument represents a relative and not absolute path, and throws an exception when an absolute URI is received for a fragment argument.
This is separate from the semantics for #join, which are well-defined by
RFC3986 section 5.2 as part of the merging and normalization process;
this method does not perform any normalization, removal of spurious
paths, or removal of parent directory references (/../).
See also #+, which concatenates the string forms of two URIs without
any sort of checking or processing.
For an up-to-date list of edge case behavior, see the shared examples for RDF::URI in the rdf-spec project.
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/rdf/model/uri.rb', line 246 def /(fragment) fragment = fragment.respond_to?(:to_uri) ? fragment.to_uri : RDF::URI.intern(fragment.to_s) raise ArgumentError, "Non-absolute URI or string required, got #{fragment}" unless fragment.relative? if urn? RDF::URI.intern(to_s.sub(/:+$/,'') + ':' + fragment.to_s.sub(/^:+/,'')) else # !urn? case to_s[-1].chr when '#' case fragment.to_s[0].chr when '/' then # Base ending with '#', fragment beginning with '/'. The fragment wins, we use '/'. RDF::URI.intern(to_s.sub(/#+$/,'') + '/' + fragment.to_s.sub(/^\/+/,'')) else RDF::URI.intern(to_s.sub(/#+$/,'') + '#' + fragment.to_s.sub(/^#+/,'')) end else # includes '/'. Results from bases ending in '/' are the same as if there were no trailing slash. case fragment.to_s[0].chr when '#' then # Base ending with '/', fragment beginning with '#'. The fragment wins, we use '#'. RDF::URI.intern(to_s.sub(/\/+$/,'') + '#' + fragment.to_s.sub(/^#+/,'')) else RDF::URI.intern(to_s.sub(/\/+$/,'') + '/' + fragment.to_s.sub(/^\/+/,'')) end end end end |
- (Boolean) ==(other)
Checks whether this URI is equal to other.
461 462 463 464 465 466 467 |
# File 'lib/rdf/model/uri.rb', line 461 def ==(other) case other when String then to_s == other when URI, Addressable::URI then to_s == other.to_s else other.respond_to?(:to_uri) && to_s == other.to_uri.to_s end end |
- (Boolean) ===(other)
Checks for case equality to the given other object.
482 483 484 485 486 487 |
# File 'lib/rdf/model/uri.rb', line 482 def ===(other) case other when Regexp then other === to_s else self == other end end |
- (Integer) =~(pattern)
Performs a pattern match using the given regular expression.
500 501 502 503 504 505 |
# File 'lib/rdf/model/uri.rb', line 500 def =~(pattern) case pattern when Regexp then to_s =~ pattern else super # `Object#=~` returns `false` end end |
- (Boolean) anonymous?
Returns false.
93 94 95 |
# File 'lib/rdf/model/uri.rb', line 93 def anonymous? false end |
- (RDF::URI) canonicalize
Returns a copy of this URI converted into its canonical lexical representation.
163 164 165 |
# File 'lib/rdf/model/uri.rb', line 163 def canonicalize self.dup.canonicalize! end |
- (RDF::URI) canonicalize!
Converts this URI into its canonical lexical representation.
172 173 174 175 |
# File 'lib/rdf/model/uri.rb', line 172 def canonicalize! # TODO: canonicalize this URI self end |
- (RDF::URI) dup
Returns a duplicate copy of self.
394 395 396 |
# File 'lib/rdf/model/uri.rb', line 394 def dup self.class.new(@uri.dup) end |
- (Boolean) end_with?(string) Also known as: ends_with?
Returns true if this URI ends with the given string.
432 433 434 |
# File 'lib/rdf/model/uri.rb', line 432 def end_with?(string) to_s.end_with?(string.to_s) end |
- (Boolean) eql?(other)
Checks whether this URI is equal to other.
447 448 449 |
# File 'lib/rdf/model/uri.rb', line 447 def eql?(other) other.is_a?(URI) && self == other end |
- (Boolean) has_parent?
Returns true if this URI's path component isn't equal to /.
331 332 333 |
# File 'lib/rdf/model/uri.rb', line 331 def has_parent? !root? end |
- (Fixnum) hash
Returns a hash code for this URI.
531 532 533 |
# File 'lib/rdf/model/uri.rb', line 531 def hash @uri.hash end |
- (RDF::URI) join(*uris)
Joins several URIs together.
This method conforms to join normalization semantics as per RFC3986, section 5.2. This method normalizes URIs, removes some duplicate path information, such as double slashes, and other behavior specified in the RFC.
Other URI building methods are #/ and #+.
For an up-to-date list of edge case behavior, see the shared examples for RDF::URI in the rdf-spec project.
199 200 201 202 203 204 205 |
# File 'lib/rdf/model/uri.rb', line 199 def join(*uris) result = @uri.dup uris.each do |uri| result = result.join(uri) end self.class.new(result) end |
- (Integer) length Also known as: size
Returns the string length of this URI.
140 141 142 |
# File 'lib/rdf/model/uri.rb', line 140 def length to_s.length end |
- (RDF::URI) parent
Returns a copy of this URI with the path component ascended to the parent directory, if any.
344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/rdf/model/uri.rb', line 344 def parent case when root? then nil else require 'pathname' unless defined?(Pathname) if path = Pathname.new(self.path).parent uri = self.dup uri.path = path.to_s uri.path << '/' unless uri.root? uri end end end |
- (Array(Symbol, Symbol)) qname
Returns a qualified name (QName) for this URI, if possible.
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/rdf/model/uri.rb', line 367 def qname if self.to_s =~ %r([:/#]([^:/#]*)$) local_name = $1 vocab_uri = local_name.empty? ? self.to_s : self.to_s[0...-(local_name.length)] Vocabulary.each do |vocab| if vocab.to_uri == vocab_uri prefix = vocab.equal?(RDF) ? :rdf : vocab.__prefix__ return [prefix, local_name.empty? ? nil : local_name.to_sym] end end else Vocabulary.each do |vocab| vocab_uri = vocab.to_uri if self.start_with?(vocab_uri) prefix = vocab.equal?(RDF) ? :rdf : vocab.__prefix__ local_name = self.to_s[vocab_uri.length..-1] return [prefix, local_name.empty? ? nil : local_name.to_sym] end end end return nil # no QName found end |
- (Boolean) respond_to?(symbol)
Returns true if this URI instance supports the symbol method.
540 541 542 |
# File 'lib/rdf/model/uri.rb', line 540 def respond_to?(symbol) @uri.respond_to?(symbol) || super end |
- (RDF::URI) root
Returns a copy of this URI with the path component set to /.
313 314 315 316 317 318 319 320 321 |
# File 'lib/rdf/model/uri.rb', line 313 def root if root? self else uri = self.dup uri.path = '/' uri end end |
- (Boolean) root?
Returns true if this URI's path component is equal to /.
301 302 303 |
# File 'lib/rdf/model/uri.rb', line 301 def root? self.path == '/' || self.path.empty? end |
- (Boolean) start_with?(string) Also known as: starts_with?
Returns true if this URI starts with the given string.
416 417 418 |
# File 'lib/rdf/model/uri.rb', line 416 def start_with?(string) to_s.start_with?(string.to_s) end |
- (String) to_str Also known as: to_s
Returns the string representation of this URI.
522 523 524 |
# File 'lib/rdf/model/uri.rb', line 522 def to_str @uri.to_s end |
- (RDF::URI) to_uri
Returns self.
511 512 513 |
# File 'lib/rdf/model/uri.rb', line 511 def to_uri self end |
- (Boolean) uri?
Returns true.
102 103 104 |
# File 'lib/rdf/model/uri.rb', line 102 def uri? true end |
- (Boolean) url?
Returns true if this URI is a URL.
128 129 130 |
# File 'lib/rdf/model/uri.rb', line 128 def url? !urn? end |
- (Boolean) urn?
Returns true if this URI is a URN.
115 116 117 |
# File 'lib/rdf/model/uri.rb', line 115 def urn? self.start_with?('urn:') end |
- (RDF::URI) validate! Also known as: validate
Validates this URI, raising an error if it is invalid.
151 152 153 154 |
# File 'lib/rdf/model/uri.rb', line 151 def validate! # TODO: raise error if the URI fails validation self end |