Class: Marc2LinkedData::Resource
- Inherits:
-
Object
- Object
- Marc2LinkedData::Resource
show all
- Defined in:
- lib/marc2linkeddata/resource.rb
Constant Summary
collapse
- @@config =
nil
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(uri = nil) ⇒ Resource
Returns a new instance of Resource.
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/marc2linkeddata/resource.rb', line 11
def initialize(uri=nil)
@@config ||= Marc2LinkedData.configuration
if uri =~ /\A#{URI::regexp}\z/
uri = Addressable::URI.parse(uri.to_s) rescue nil
end
if uri.to_s.end_with? '/'
uri = uri.to_s.gsub(/\/$/,'')
uri = Addressable::URI.parse(uri.to_s) rescue nil
end
raise 'invalid uri' unless uri.instance_of? Addressable::URI
@iri = uri
end
|
Instance Attribute Details
#iri ⇒ Object
Returns the value of attribute iri.
6
7
8
|
# File 'lib/marc2linkeddata/resource.rb', line 6
def iri
@iri
end
|
Instance Method Details
#get_rdf(uri4rdf) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/marc2linkeddata/resource.rb', line 40
def get_rdf(uri4rdf)
tries = 0
begin
tries += 1
@rdf = RDF::Graph.load(uri4rdf)
rescue
retry if tries <= 2
binding.pry if @@config.debug
nil
end
end
|
#id ⇒ Object
25
26
27
|
# File 'lib/marc2linkeddata/resource.rb', line 25
def id
@iri.basename
end
|
#iri_types ⇒ Object
60
61
62
63
|
# File 'lib/marc2linkeddata/resource.rb', line 60
def iri_types
q = SPARQL.parse("SELECT * WHERE { <#{@iri}> a ?o }")
rdf.query(q)
end
|
#rdf ⇒ Object
This method is often overloaded in subclasses because RDF services use variations in the URL ‘extension’ patterns; e.g. see Loc#rdf and Viaf#rdf
32
33
34
35
36
37
38
|
# File 'lib/marc2linkeddata/resource.rb', line 32
def rdf
return @rdf unless @rdf.nil?
@rdf = get_rdf(@iri.to_s)
end
|
#rdf_find_object(id) ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/marc2linkeddata/resource.rb', line 65
def rdf_find_object(id)
return nil unless rdf_valid?
rdf.each_statement do |s|
if s.subject == @iri.to_s
return s.object if s.object.to_s =~ Regexp.new(id, Regexp::IGNORECASE)
end
end
nil
end
|
#rdf_find_subject(id) ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/marc2linkeddata/resource.rb', line 76
def rdf_find_subject(id)
return nil unless rdf_valid?
rdf.each_statement do |s|
return s.subject if s.subject.to_s =~ Regexp.new(id, Regexp::IGNORECASE)
end
nil
end
|
#rdf_uri ⇒ Object
52
53
54
|
# File 'lib/marc2linkeddata/resource.rb', line 52
def rdf_uri
RDF::URI.new(@iri)
end
|
#rdf_valid? ⇒ Boolean
56
57
58
|
# File 'lib/marc2linkeddata/resource.rb', line 56
def rdf_valid?
iri_types.length > 0
end
|
#resolve_external_auth(url) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/marc2linkeddata/resource.rb', line 85
def resolve_external_auth(url)
begin
url = Marc2LinkedData.http_head_request(url)
if url.nil?
@@config.logger.warn "#{@iri}\t// #{url}"
else
@@config.logger.debug "Mapped #{@iri}\t-> #{url}"
end
rescue
binding.pry if @@config.debug
@@config.logger.error "unknown http error for #{@iri}"
url = nil
end
url
end
|
#same_as ⇒ Object
102
103
104
105
|
# File 'lib/marc2linkeddata/resource.rb', line 102
def same_as
same_as_url = 'http://sameas.org/rdf?uri=' + URI.encode(@iri.to_s)
RDF::Graph.load(same_as_url)
end
|
#same_as_array ⇒ Object
107
108
109
110
|
# File 'lib/marc2linkeddata/resource.rb', line 107
def same_as_array
q = SPARQL.parse("SELECT * WHERE { <#{@iri}> <http://www.w3.org/2002/07/owl#sameAs> ?o }")
same_as.query(q).collect {|s| s[:o] }
end
|