/
fetching.rb
38 lines (31 loc) · 1.3 KB
/
fetching.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
# Author:: Eyal Oren
# Copyright:: (c) 2005-2006 Eyal Oren
# License:: LGPL
# FetchingAdapter is an extension to rdflite for fetching RDF from online sources.
module ActiveRDF
class FetchingAdapter < RDFLite
ConnectionPool.register_adapter(:fetching, self)
# TODO: check that rapper is installed
# fetches RDF/XML data from given url and adds it to the datastore, using the
# source url as context identifier.
def fetch(url, syntax = nil)
# check if url starts with http://
return unless url.match(/http:\/\/(.*)/)
ActiveRdfLogger::log_debug(self) { "Fetching from #{url}" }
#model = Redland::Model.new
#parser = Redland::Parser.new('rdfxml')
#scan = Redland::Uri.new('http://feature.librdf.org/raptor-scanForRDF')
#enable = Redland::Literal.new('1')
#Redland::librdf_parser_set_feature(parser, scan.uri, enable.node)
#parser.parse_into_model(model, url)
#triples = Redland::Serializer.ntriples.model_to_string(nil, model)
opts = syntax ? "-i #{syntax}" : "--scan"
triples = `rapper #{opts} --quiet "#{url}"`
lines = triples.split($/)
ActiveRdfLogger::log_debug(self) { "Found #{lines.size} triples" }
context = Query.resource_class.new(url)
add_ntriples(triples, context)
end
alias :load :fetch
end
end