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

#initialize(*args) ⇒ URIRef

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"

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(namespaces = []) ⇒ Namespace (readonly)

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



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

#uriObject

Returns the value of attribute uri



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

def uri
  @uri
end

Class Method Details

.intern(*args) ⇒ Object

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

.parse(str) ⇒ Object

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

#+(input) ⇒ URIRef

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



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

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



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

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

#baseString

base of URI for creating QNames.

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


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

#eql?(other) ⇒ Boolean Also known as: ==



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

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

#hashString

Needed for uniq



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

def hash; to_s.hash; end

#inspectObject



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

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

#short_nameString, false

short_name of URI for creating QNames.

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


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

#test_string(string) ⇒ Object (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

#to_n3String Also known as: to_ntriples



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

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

#to_qname(uri_binding = []) ⇒ String

Output URI as QName using URI binding



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

#to_sString



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

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

#uri?Boolean

Returns `true`



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

def uri?
  true
end

#xml_argsArray<Hash{String => String}>

Output URI as resource reference for RDF/XML



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

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