Class: RdfaParser::URIRef

Inherits:
Object
  • Object
show all
Defined in:
lib/rdfa_parser/uriref.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ URIRef

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

Example

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


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rdfa_parser/uriref.rb', line 13

def initialize (*args)
  args.each {|s| test_string(s)}
  if args.size == 1
    @uri = Addressable::URI.parse(args[0].to_s)
  else
    @uri = Addressable::URI.join(*args.map{|s| s.to_s}.reverse)
  end
  if @uri.relative?
    raise UriRelativeException, "<" + @uri.to_s + "> is a relative URI"
  end
  if !@uri.to_s.match(/^javascript/).nil?
    raise ParserException, "Javascript pseudo-URIs are not acceptable"
  end
  
  # Unique URI through class hash to ensure that URIRefs can be easily compared
  @@uri_hash ||= {}
  @@uri_hash[@uri.to_s] ||= @uri
  @uri = @@uri_hash[@uri.to_s]
end

Instance Attribute Details

#uriObject

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#+(input) ⇒ Object



33
34
35
36
# File 'lib/rdfa_parser/uriref.rb', line 33

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

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

Returns:

  • (Boolean)


48
49
50
# File 'lib/rdfa_parser/uriref.rb', line 48

def eql?(other)
  @uri.to_s == other.to_s
end

#hashObject

Needed for uniq



54
# File 'lib/rdfa_parser/uriref.rb', line 54

def hash; to_s.hash; end

#short_nameObject



38
39
40
41
42
43
44
45
46
# File 'lib/rdfa_parser/uriref.rb', line 38

def short_name
  if @uri.fragment()
    return @uri.fragment()
  elsif @uri.path.split("/").last.class == String and @uri.path.split("/").last.length > 0
    return @uri.path.split("/").last
  else
    return false
  end
end

#test_string(string) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/rdfa_parser/uriref.rb', line 81

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

#to_ntriplesObject



60
61
62
# File 'lib/rdfa_parser/uriref.rb', line 60

def to_ntriples
  "<" + @uri.to_s + ">"
end

#to_qname(uri_binding = {}) ⇒ Object

Output URI as QName using URI binding



65
66
67
68
69
70
71
72
73
74
# File 'lib/rdfa_parser/uriref.rb', line 65

def to_qname(uri_binding = {})
  uri_base = @uri.to_s
  sn = short_name.to_s
  uri_base = uri_base[0, uri_base.length - sn.length]
  if uri_binding.has_key?(uri_base)
    "#{uri_binding[uri_base]}:#{sn}"
  else
    raise ParserException, "Couldn't find QName for #{@uri}"
  end
end

#to_sObject



56
57
58
# File 'lib/rdfa_parser/uriref.rb', line 56

def to_s
  @uri.to_s
end

#xml_argsObject

Output URI as resource reference for RDF/XML



77
78
79
# File 'lib/rdfa_parser/uriref.rb', line 77

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