-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_values_spira.rb
70 lines (59 loc) · 2.51 KB
/
print_values_spira.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require "equivalent-xml"
require "rdf"
require "rdf/rdfxml"
require 'rest_client'
require "spira"
require "addressable/uri"
require 'rubygems'
require 'sparql'
url = 'http://www.worldcat.org/oclc/82671871'
# Make the HTTP request for the data
resource = RestClient::Resource.new url
response, result = resource.get(:user_agent => "Example Ruby Linked Data code",
:accept => 'application/rdf+xml') do |response, request, result|
[response, result]
end
if result.kind_of? Net::HTTPRedirection
resource = RestClient::Resource.new response.headers[:location]
response, result = resource.get(:user_agent => "Example Ruby Linked Data code",
:accept => 'application/rdf+xml') do |response, request, result|
[response, result]
end
end
if result.class == Net::HTTPOK
# Load the data into an in-memory RDF repository, get the GenericResource and its Bib
Spira.repository = RDF::Repository.new.from_rdfxml(response)
#name
puts Spira.repository.query(:subject => RDF::URI.new(url), :predicate => RDF::URI.new('http://schema.org/name')).first.object.to_s
#author
authors = Spira.repository.query(:subject => RDF::URI.new(url), :predicate => RDF::URI.new('http://schema.org/author'))
authors.each { |author|
author_name = Spira.repository.query(:subject => author.object, :predicate => RDF::URI.new('http://schema.org/name')).first
puts author_name.object
}
creators = Spira.repository.query(:subject => RDF::URI.new(url), :predicate => RDF::URI.new('http://schema.org/creator'))
creators.each { |creator|
creator_name = Spira.repository.query(:subject => creator.object, :predicate => RDF::URI.new('http://schema.org/name')).first
puts creator_name.object
}
#about
subjects = Spira.repository.query(:subject => RDF::URI.new(url), :predicate => RDF::URI.new('http://schema.org/about'))
subjects.each { |subject|
subject_name = Spira.repository.query(:subject => subject.object, :predicate => RDF::URI.new('http://schema.org/name')).first
if subject_name
puts subject_name.object
else
puts subject.object
end
}
#description
descriptions = Spira.repository.query(:subject => RDF::URI.new(url), :predicate => RDF::URI.new('http://schema.org/description'))
descriptions.each { |description|
puts description.object
}
#date
puts Spira.repository.query(:subject => RDF::URI.new(url), :predicate => RDF::URI.new('http://schema.org/datePublished')).first.object.to_s
else
puts response.code
puts response
end