Class: RdfContext::URIRef

Inherits:
Resource show all
Defined in:
lib/rdf_context/uriref.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Resource

#bnode?, #graph?, #literal?, #resource?

Constructor Details

- (URIRef) initialize(*args)

Create a URIRef from a URI or a fragment and a URI

Last argument may be an options hash to set:

Examples:

u = URIRef.new("http://example.com")
u = URIRef.new("foo", u) => "http://example.com/foo"

Parameters:

  • args (Array<#to_s>)

    One or two URIs to create. Last is joined with first

  • options[Boolean] (Hash)

    a customizable set of options

  • options[Namespace] (Hash)

    a customizable set of options

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rdf_context/uriref.rb', line 18

def initialize (*args)
  options = args.last.is_a?(Hash) ? args.pop : { :normalize => false }
  @normalize = options[:normalize]
  @namespace = options[:namespace]

  args.each {|s| test_string(s)}
  if args.size == 1
    uri = Addressable::URI.parse(args[0].to_s.rdf_unescape.rdf_escape)
  else
    uri = Addressable::URI.join(*args.compact.map{|s| s.to_s.rdf_unescape.rdf_escape}.reverse)
  end

  raise ParserException, "<" + uri.to_s + "> is a relative URI" if uri.relative?

  # Unique URI through class hash to ensure that URIRefs can be easily compared
  @@uri_hash ||= {}
  @uri = @@uri_hash["#{uri}#{@normalize}"] ||= begin
    # Special case if URI has no path, and the authority ends with a '#'
    #uri = Addressable::URI.parse($1) if @normalize && uri.to_s.match(/^(.*)\#$/)

    #@normalize ? uri.normalize : uri
    uri
  end.freeze
end

Instance Attribute Details

- (Namespace) namespace(namespaces = []) (readonly)

Look at namespaces and find first that matches this URI, ordering by longest URI first

Parameters:

Returns:



142
143
144
145
# File 'lib/rdf_context/uriref.rb', line 142

def namespace(namespaces = [])
  @namespace ||=
    namespaces.sort_by {|ns| -ns.uri.to_s.length}.detect {|ns| self.to_s.index(ns.uri.to_s) == 0}
end

- (Object) uri

Returns the value of attribute uri



5
6
7
# File 'lib/rdf_context/uriref.rb', line 5

def uri
  @uri
end

Class Method Details

+ (Object) intern(*args)

Internalized version of URIRef



57
58
59
60
# File 'lib/rdf_context/uriref.rb', line 57

def self.intern(*args)
  @@uri_hash ||= {}
  @@uri_hash[args.to_s] ||= URIRef.new(*args)
end

+ (Object) parse(str)

Parse a URIRef



52
53
54
# File 'lib/rdf_context/uriref.rb', line 52

def self.parse(str)
  URIRef.new($1) if str =~ /^<?([^>]*)>?$/ rescue nil
end

Instance Method Details

- (URIRef) +(input)

Create a URI, either by appending a fragment, or using the input URI

Parameters:

  • input (#to_s)

Returns:



65
66
67
68
# File 'lib/rdf_context/uriref.rb', line 65

def + (input)
  input_uri = Addressable::URI.parse(input.to_s)
  return URIRef.intern(input_uri, self.to_s)
end

- (-1, 0 1) <=>(other)

Parameters:

  • other (#to_s)

Returns:

  • (-1, 0 1)


108
109
110
# File 'lib/rdf_context/uriref.rb', line 108

def <=>(other)
  self.to_s <=> other.to_s
end

- (String) base

base of URI for creating QNames.

"#{base]{#short_name}}" == uri

Returns:



91
92
93
94
95
96
97
# File 'lib/rdf_context/uriref.rb', line 91

def base
  @base ||= begin
    uri_base = self.to_s
    sn = short_name ? short_name.to_s : ""
    uri_base[0, uri_base.length - sn.length]
  end
end

- (Boolean) eql?(other) Also known as: ==

Parameters:

Returns:

  • (Boolean)


101
102
103
# File 'lib/rdf_context/uriref.rb', line 101

def eql?(other)
  self.to_s == other.to_s
end

- (String) hash

Needed for uniq

Returns:



114
# File 'lib/rdf_context/uriref.rb', line 114

def hash; to_s.hash; end

- (Object) inspect



147
148
149
# File 'lib/rdf_context/uriref.rb', line 147

def inspect
  "#{self.class}[#{self.to_n3}, ns=#{namespace.inspect}]"
end

- (String, false) short_name

short_name of URI for creating QNames.

"#{base]{#short_name}}" == uri

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rdf_context/uriref.rb', line 73

def short_name
  @short_name ||= begin
    path = @uri.path.split("/")
    if @namespace
      self.to_s.sub(@namespace.uri.to_s, "")
    elsif @uri.fragment
      @uri.fragment
    elsif path && path.length > 1 && path.last.class == String && path.last.length > 0 && path.last.index("/") != 0
      path.last
    else
      false
    end
  end
end

- (Object) test_string(string) (protected)



158
159
160
161
162
163
164
# File 'lib/rdf_context/uriref.rb', line 158

def test_string (string)
  string.to_s.each_byte do |b|
    if b >= 0 and b <= 31
      raise ParserException, "URI '#{string}' must not contain control characters"
    end
  end
end

- (String) to_n3 Also known as: to_ntriples

Returns:



122
123
124
# File 'lib/rdf_context/uriref.rb', line 122

def to_n3
  "<" + self.to_s + ">"
end

- (String) to_qname(uri_binding = [])

Output URI as QName using URI binding

Returns:



129
130
131
132
133
134
135
136
137
# File 'lib/rdf_context/uriref.rb', line 129

def to_qname(uri_binding = [])
  namespaces = case uri_binding
  when Hash then uri_binding.values
  when Array then uri_binding
  else []
  end
  ns = namespace(namespaces)
  "#{ns.prefix}:#{short_name}" if ns
end

- (String) to_s

Returns:



117
118
119
# File 'lib/rdf_context/uriref.rb', line 117

def to_s
  @to_s ||= @uri.to_s
end

- (Boolean) uri?

Returns `true`

Returns:

  • (Boolean)


47
48
49
# File 'lib/rdf_context/uriref.rb', line 47

def uri?
  true
end

- (Array<Hash{String => String}>) xml_args

Output URI as resource reference for RDF/XML

Returns:



153
154
155
# File 'lib/rdf_context/uriref.rb', line 153

def xml_args
  [{"rdf:resource" => self.to_s}]
end