Class: RDF::URI
Overview
A Uniform Resource Identifier (URI).
‘RDF::URI` supports all the instance methods of `Addressable::URI`.
Constant Summary collapse
- CACHE_SIZE =
Defines the maximum number of interned URI references that can be held cached in memory at any one time.
-1 # unlimited by default
Class Method Summary collapse
- .cache ⇒ RDF::Util::Cache
-
.intern(str) ⇒ RDF::URI
Returns an interned ‘RDF::URI` instance based on the given `uri` string.
-
.parse(str) ⇒ RDF::URI
Creates a new ‘RDF::URI` instance based on the given `uri` string.
Instance Method Summary collapse
-
#+(other) ⇒ RDF::URI
Simple concatenation operator.
-
#/(fragment) ⇒ RDF::URI
‘Smart separator’ URI builder.
-
#==(other) ⇒ Boolean
Checks whether this URI is equal to ‘other` (type checking).
-
#===(other) ⇒ Boolean
Checks for case equality to the given ‘other` object.
-
#=~(pattern) ⇒ Integer
Performs a pattern match using the given regular expression.
-
#anonymous? ⇒ Boolean
Returns ‘false`.
-
#canonicalize ⇒ RDF::URI
Returns a copy of this URI converted into its canonical lexical representation.
-
#canonicalize! ⇒ RDF::URI
Converts this URI into its canonical lexical representation.
-
#dup ⇒ RDF::URI
Returns a duplicate copy of ‘self`.
-
#end_with?(string) ⇒ Boolean
(also: #ends_with?)
Returns ‘true` if this URI ends with the given `string`.
-
#eql?(other) ⇒ Boolean
Checks whether this URI the same term as ‘other’.
- #freeze ⇒ Object
-
#has_parent? ⇒ Boolean
Returns ‘true` if this URI’s path component isn’t equal to ‘/`.
-
#hash ⇒ Fixnum
Returns a hash code for this URI.
-
#initialize(uri_or_options) ⇒ URI
constructor
A new instance of URI.
-
#join(*uris) ⇒ RDF::URI
Joins several URIs together.
-
#length ⇒ Integer
(also: #size)
Returns the string length of this URI.
-
#parent ⇒ RDF::URI
Returns a copy of this URI with the path component ascended to the parent directory, if any.
-
#qname ⇒ Array(Symbol, Symbol)
Returns a qualified name (QName) for this URI, if possible.
-
#respond_to?(symbol) ⇒ Boolean
Returns ‘true` if this URI instance supports the `symbol` method.
-
#root ⇒ RDF::URI
Returns a copy of this URI with the path component set to ‘/`.
-
#root? ⇒ Boolean
Returns ‘true` if this URI’s path component is equal to ‘/`.
-
#start_with?(string) ⇒ Boolean
(also: #starts_with?)
Returns ‘true` if this URI starts with the given `string`.
-
#to_str ⇒ String
(also: #to_s)
Returns the string representation of this URI.
-
#to_uri ⇒ RDF::URI
Returns ‘self`.
-
#uri? ⇒ Boolean
Returns ‘true`.
-
#url? ⇒ Boolean
Returns ‘true` if this URI is a URL.
-
#urn? ⇒ Boolean
Returns ‘true` if this URI is a URN.
-
#validate! ⇒ RDF::URI
(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_quad, #to_rdf, #type_error, #variable?
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) { ... } ⇒ Object (protected)
558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/rdf/model/uri.rb', line 558 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
.cache ⇒ RDF::Util::Cache
34 35 36 37 |
# File 'lib/rdf/model/uri.rb', line 34 def self.cache require 'rdf/util/cache' unless defined?(::RDF::Util::Cache) @cache ||= RDF::Util::Cache.new(CACHE_SIZE) end |
.intern(str) ⇒ RDF::URI
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 |
.parse(str) ⇒ RDF::URI
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
#+(other) ⇒ RDF::URI
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 |
#/(fragment) ⇒ RDF::URI
‘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) frag = fragment.respond_to?(:to_uri) ? fragment.to_uri : RDF::URI(fragment.to_s) raise ArgumentError, "Non-absolute URI or string required, got #{frag}" unless frag.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 |
#==(other) ⇒ Boolean
Checks whether this URI is equal to ‘other` (type checking).
Per SPARQL data-r2/expr-equal/eq-2-2, numeric can’t be compared with other types
464 465 466 467 468 469 470 471 472 473 |
# File 'lib/rdf/model/uri.rb', line 464 def ==(other) case other when Literal # If other is a Literal, reverse test to consolodate complex type checking logic other == self 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 |
#===(other) ⇒ Boolean
Checks for case equality to the given ‘other` object.
488 489 490 491 492 493 |
# File 'lib/rdf/model/uri.rb', line 488 def ===(other) case other when Regexp then other === to_s else self == other end end |
#=~(pattern) ⇒ Integer
Performs a pattern match using the given regular expression.
506 507 508 509 510 511 |
# File 'lib/rdf/model/uri.rb', line 506 def =~(pattern) case pattern when Regexp then to_s =~ pattern else super # `Object#=~` returns `false` end end |
#anonymous? ⇒ Boolean
Returns ‘false`.
93 94 95 |
# File 'lib/rdf/model/uri.rb', line 93 def anonymous? false end |
#canonicalize ⇒ RDF::URI
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 |
#canonicalize! ⇒ RDF::URI
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 |
#dup ⇒ RDF::URI
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 |
#end_with?(string) ⇒ Boolean 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 |
#eql?(other) ⇒ Boolean
Checks whether this URI the same term as ‘other’.
447 448 449 |
# File 'lib/rdf/model/uri.rb', line 447 def eql?(other) other.is_a?(URI) && self == other end |
#freeze ⇒ Object
400 401 402 403 |
# File 'lib/rdf/model/uri.rb', line 400 def freeze @uri.freeze super end |
#has_parent? ⇒ Boolean
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 |
#hash ⇒ Fixnum
Returns a hash code for this URI.
537 538 539 |
# File 'lib/rdf/model/uri.rb', line 537 def hash @uri.hash end |
#join(*uris) ⇒ RDF::URI
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 |
#length ⇒ Integer 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 |
#parent ⇒ RDF::URI
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 |
#qname ⇒ Array(Symbol, Symbol)
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 |
#respond_to?(symbol) ⇒ Boolean
Returns ‘true` if this URI instance supports the `symbol` method.
546 547 548 |
# File 'lib/rdf/model/uri.rb', line 546 def respond_to?(symbol) @uri.respond_to?(symbol) || super end |
#root ⇒ RDF::URI
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 |
#root? ⇒ Boolean
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 |
#start_with?(string) ⇒ Boolean 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 |
#to_str ⇒ String Also known as: to_s
Returns the string representation of this URI.
528 529 530 |
# File 'lib/rdf/model/uri.rb', line 528 def to_str @uri.to_s end |
#to_uri ⇒ RDF::URI
Returns ‘self`.
517 518 519 |
# File 'lib/rdf/model/uri.rb', line 517 def to_uri self end |
#uri? ⇒ Boolean
Returns ‘true`.
102 103 104 |
# File 'lib/rdf/model/uri.rb', line 102 def uri? true end |
#url? ⇒ Boolean
Returns ‘true` if this URI is a URL.
128 129 130 |
# File 'lib/rdf/model/uri.rb', line 128 def url? !urn? end |
#urn? ⇒ Boolean
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 |
#validate! ⇒ RDF::URI 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 |