ppe-4store-ruby 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/4store-ruby.gemspec +15 -0
- data/README +5 -0
- data/lib/4store-ruby.rb +1 -0
- data/lib/four_store/namespace.rb +49 -0
- data/lib/four_store/store.rb +103 -0
- metadata +59 -0
data/4store-ruby.gemspec
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "ppe-4store-ruby"
|
3
|
+
s.version = "0.0.3"
|
4
|
+
s.date = "2009-10-01"
|
5
|
+
s.summary = "4Store Ruby library"
|
6
|
+
s.email = "yves@dbtune.org"
|
7
|
+
s.homepage = "http://github.com/moustaki/4store-ruby"
|
8
|
+
s.description = "A Ruby library to interact with a 4store instance (a scalable RDF store), see http://4store.org/"
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ['Yves Raimond']
|
11
|
+
s.files = ["README", "4store-ruby.gemspec", "lib", "examples", "lib/4store-ruby.rb", "lib/four_store/store.rb", "lib/four_store/namespace.rb"]
|
12
|
+
#s.test_files = ["spec/activerdf_reddy_spec.rb"]
|
13
|
+
#s.rdoc_options = ["--main", "README.txt"]
|
14
|
+
#s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
15
|
+
end
|
data/README
ADDED
data/lib/4store-ruby.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.join(File.expand_path(File.dirname(__FILE__)), 'four_store', 'store.rb')
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module FourStore
|
2
|
+
|
3
|
+
class Namespace
|
4
|
+
|
5
|
+
@namespaces = {
|
6
|
+
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
|
7
|
+
'dc' => 'http://purl.org/dc/elements/1.1/',
|
8
|
+
'foaf' => 'http://xmlns.com/foaf/0.1/',
|
9
|
+
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
|
10
|
+
'xsd' => 'http://www.w3.org/2001/XMLSchema#',
|
11
|
+
'activ' => 'http://www.bbc.co.uk/ontologies/activity/',
|
12
|
+
'event' => 'http://purl.org/NET/c4dm/event.owl#',
|
13
|
+
'po' => 'http://purl.org/ontology/po/',
|
14
|
+
'timeline' => 'http://purl.org/NET/c4dm/timeline.owl#',
|
15
|
+
'skos' => 'http://www.w3.org/2008/05/skos#',
|
16
|
+
'time' => 'http://www.w3.org/2006/time#',
|
17
|
+
'mo' => 'http://purl.org/ontology/mo/',
|
18
|
+
'dcterms' => 'http://purl.org/dc/terms/',
|
19
|
+
'wgs84_pos' => 'http://www.w3.org/2003/01/geo/wgs84_pos#',
|
20
|
+
'owl' => 'http://www.w3.org/2002/07/owl#'
|
21
|
+
}
|
22
|
+
|
23
|
+
def self.add(short, long)
|
24
|
+
@namespaces[short] = long
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.get(short)
|
28
|
+
@namespaces[short]
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.to_sparql()
|
32
|
+
sparql = ""
|
33
|
+
@namespaces.keys.each do |short|
|
34
|
+
sparql += "PREFIX #{short}: <#{@namespaces[short]}>\n"
|
35
|
+
end
|
36
|
+
sparql
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.to_turtle()
|
40
|
+
turtle = ""
|
41
|
+
@namespaces.keys.each do |short|
|
42
|
+
turtle += "@prefix #{short}: <#{@namespaces[short]}>.\n"
|
43
|
+
end
|
44
|
+
turtle
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'rexml/document'
|
4
|
+
require File.expand_path(File.dirname(__FILE__)) + '/namespace'
|
5
|
+
|
6
|
+
module FourStore
|
7
|
+
|
8
|
+
class Store
|
9
|
+
|
10
|
+
def initialize(endpoint, options = nil)
|
11
|
+
raise "4Store SPARQL end-point URI must end by '/sparql/'" if endpoint.split("/sparql/").size != 1
|
12
|
+
@endpoint = URI.parse(endpoint)
|
13
|
+
@proxy = URI.parse(ENV['HTTP_PROXY']) if ENV['HTTP_PROXY']
|
14
|
+
@certificate = options["certificate"] if options
|
15
|
+
@key = options["key"] if options
|
16
|
+
@softlimit = options["soft-limit"] if options
|
17
|
+
end
|
18
|
+
|
19
|
+
def select(query)
|
20
|
+
http.start do |h|
|
21
|
+
request = Net::HTTP::Post.new(@endpoint.path)
|
22
|
+
request.set_form_data({ 'query' => Namespace::to_sparql + query, 'soft-limit' => @softlimit })
|
23
|
+
response = h.request(request)
|
24
|
+
parse_sparql_xml_results(response.body)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def set(graph, turtle)
|
29
|
+
http.start do |h|
|
30
|
+
request = Net::HTTP::Put.new(@endpoint.path + graph)
|
31
|
+
request.body = Namespace::to_turtle + turtle
|
32
|
+
request.content_type = 'application/x-turtle'
|
33
|
+
response = h.request(request)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def add(graph, turtle)
|
38
|
+
http.start do |h|
|
39
|
+
request = Net::HTTP::Post.new((@endpoint.path.split("/sparql/")[0] or "") + "/data/")
|
40
|
+
request.set_form_data({
|
41
|
+
'graph' => graph,
|
42
|
+
'data' => Namespace::to_turtle + turtle,
|
43
|
+
'mime-type' => 'application/x-turtle'
|
44
|
+
})
|
45
|
+
response = h.request(request)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def delete(graph)
|
50
|
+
http.start do |h|
|
51
|
+
request = Net::HTTP::Delete.new(@endpoint.path + graph)
|
52
|
+
response = h.request(request)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def load(uri)
|
57
|
+
# WARNING - relies on the -U flag being set when running 4s-httpd
|
58
|
+
http.start do |h|
|
59
|
+
request = Net::HTTP::Post.new((@endpoint.path.split("/sparql/")[0] or "") + "/update/")
|
60
|
+
request.set_form_data({
|
61
|
+
'update' => "LOAD <#{uri}>"
|
62
|
+
})
|
63
|
+
response = h.request(request)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def http
|
70
|
+
if @proxy
|
71
|
+
h = Net::HTTP::Proxy(@proxy.host, @proxy.port).new(@endpoint.host, @endpoint.port)
|
72
|
+
else
|
73
|
+
h = Net::HTTP.new(@endpoint.host, @endpoint.port)
|
74
|
+
end
|
75
|
+
if @certificate && @key
|
76
|
+
require 'net/https'
|
77
|
+
h.use_ssl = true
|
78
|
+
h.cert = OpenSSL::X509::Certificate.new( File.read(@certificate) )
|
79
|
+
h.key = OpenSSL::PKey::RSA.new( File.read(@key) )
|
80
|
+
h.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
81
|
+
end
|
82
|
+
h
|
83
|
+
end
|
84
|
+
|
85
|
+
def parse_sparql_xml_results(xml)
|
86
|
+
results = []
|
87
|
+
doc = REXML::Document.new xml
|
88
|
+
doc.elements.each("*/results/result") do |result|
|
89
|
+
result_hash = {}
|
90
|
+
result.elements.each do |binding|
|
91
|
+
key = binding.attributes["name"]
|
92
|
+
value = binding.elements[1].text
|
93
|
+
type = binding.elements[1].name
|
94
|
+
result_hash[key] = value
|
95
|
+
end
|
96
|
+
results.push result_hash
|
97
|
+
end
|
98
|
+
results
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ppe-4store-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yves Raimond
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-01 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A Ruby library to interact with a 4store instance (a scalable RDF store), see http://4store.org/
|
17
|
+
email: yves@dbtune.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- 4store-ruby.gemspec
|
27
|
+
- lib/4store-ruby.rb
|
28
|
+
- lib/four_store/store.rb
|
29
|
+
- lib/four_store/namespace.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/moustaki/4store-ruby
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
requirements: []
|
52
|
+
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 1.3.5
|
55
|
+
signing_key:
|
56
|
+
specification_version: 3
|
57
|
+
summary: 4Store Ruby library
|
58
|
+
test_files: []
|
59
|
+
|