currmap-sesame 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +7 -0
- data/README.rdoc +20 -0
- data/Rakefile +43 -0
- data/VERSION +1 -0
- data/lib/sesame.rb +84 -0
- data/spec/sesame_spec.rb +63 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +93 -0
data/.gitignore
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
= Sesame ORM
|
2
|
+
|
3
|
+
This is a simple object mapping system for a Sesame triple store database.
|
4
|
+
|
5
|
+
Although it aims to be generally useful, it is designed specifically for use
|
6
|
+
with the CurrMap project, and hence there may be some manner of coupling to
|
7
|
+
the aforementioned.
|
8
|
+
|
9
|
+
== Testing
|
10
|
+
|
11
|
+
To run the tests, you'll have to create a file in the "spec" directory named
|
12
|
+
tests-config that specificies the host, port, sesame directory, repository,
|
13
|
+
username, and password used to access the test server.
|
14
|
+
|
15
|
+
You'll also need to completely change the test results to be what you want.
|
16
|
+
Like I said, pretty tightly coupled. Suck it.
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "currmap-sesame"
|
8
|
+
gem.summary = "Sesame RDF database ORM"
|
9
|
+
gem.email = "james.cash@occasionallycogent.com"
|
10
|
+
gem.homepage = "http://github.com/jamesnvc/currmap-sesame"
|
11
|
+
gem.description = "Establish an object-database mapping to an OpenRDF Sesame Triple Store database"
|
12
|
+
gem.authors = ["James N. V. Cash"]
|
13
|
+
gem.add_dependency 'nokogiri', ">= 0"
|
14
|
+
gem.add_development_dependency 'rspec', '>= 0'
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :default => :spec
|
34
|
+
|
35
|
+
require 'rake/rdoctask'
|
36
|
+
Rake::RDocTask.new do |rdoc|
|
37
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
38
|
+
|
39
|
+
rdoc.rdoc_dir = 'rdoc'
|
40
|
+
rdoc.title = "currmap-sesame #{version}"
|
41
|
+
rdoc.rdoc_files.include('README*')
|
42
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
43
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/sesame.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'nokogiri'
|
5
|
+
|
6
|
+
module Sesame
|
7
|
+
|
8
|
+
class Server
|
9
|
+
attr_reader :uri, :repo
|
10
|
+
|
11
|
+
def initialize(host, port, sesamedir, repo, user, pass, options=nil)
|
12
|
+
@host = host
|
13
|
+
@port = port
|
14
|
+
@sesamedir = sesamedir
|
15
|
+
@repo = repo
|
16
|
+
@user = user
|
17
|
+
@pass = pass
|
18
|
+
@options = options
|
19
|
+
@uri =
|
20
|
+
URI.parse("http://#{@host}:#{@port}/#{@sesamedir}/repositories")
|
21
|
+
end
|
22
|
+
|
23
|
+
def select(query_str, query_lang="sparql")
|
24
|
+
params = { "query" => query_str, "queryLn" => query_lang }
|
25
|
+
query_params = params.collect { |k,v| "#{k}=#{URI.escape(v.to_s)}" }.
|
26
|
+
reverse.join '&'
|
27
|
+
req = Net::HTTP::Get.new("#{@uri.path}/#{@repo}?#{query_params}",
|
28
|
+
{'Accept' => 'application/sparql-results+xml' })
|
29
|
+
req.basic_auth @user, @pass
|
30
|
+
xml = Nokogiri::XML.parse(request(req).body)
|
31
|
+
res = Hash.new
|
32
|
+
# Get variables from query
|
33
|
+
xml.xpath('//xmlns:head/xmlns:variable').each do |var|
|
34
|
+
res[var['name']] = Array.new
|
35
|
+
end
|
36
|
+
# Populate result hash by variable
|
37
|
+
xml.xpath('//xmlns:result/xmlns:binding').each do |binding|
|
38
|
+
res[binding['name']] << (binding / 'uri')[0].content
|
39
|
+
end
|
40
|
+
return res
|
41
|
+
end
|
42
|
+
|
43
|
+
def list_repos
|
44
|
+
req = Net::HTTP::Get.new(@uri.path,
|
45
|
+
{'Accept' => "application/sparql-results+xml" })
|
46
|
+
xml = Nokogiri::XML.parse(request(req).body)
|
47
|
+
res = Array.new
|
48
|
+
xml.xpath('//xmlns:binding[@name="id"]/xmlns:literal').each do |repo|
|
49
|
+
res << repo.content
|
50
|
+
end
|
51
|
+
return res
|
52
|
+
end
|
53
|
+
|
54
|
+
def list_namespaces repo
|
55
|
+
req = Net::HTTP::Get.new("#{@uri.path}/#{repo}/namespaces",
|
56
|
+
{'Accept' => "application/sparql-results+xml" })
|
57
|
+
xml = Nokogiri::XML.parse(request(req).body)
|
58
|
+
res = Hash.new
|
59
|
+
xml.xpath('//xmlns:result').each do |result|
|
60
|
+
prefix = result.xpath('.//xmlns:binding[@name="prefix"]/xmlns:literal')[0].content
|
61
|
+
namespc = result.xpath('.//xmlns:binding[@name="namespace"]/xmlns:literal')[0].content
|
62
|
+
res[prefix] = namespc
|
63
|
+
end
|
64
|
+
return res
|
65
|
+
end
|
66
|
+
|
67
|
+
def request(req)
|
68
|
+
res = Net::HTTP.start(@host, @port) {|http|
|
69
|
+
http.request(req)
|
70
|
+
}
|
71
|
+
if (not res.kind_of?(Net::HTTPSuccess))
|
72
|
+
handle_error(req, res)
|
73
|
+
end
|
74
|
+
res
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def handle_error(req, res)
|
80
|
+
e = RuntimeError.new("#{res.code}:#{res.message}\nMETHOD:#{req.method}\nURI:#{req.path}\n#{res.body}")
|
81
|
+
raise e
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/spec/sesame_spec.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'rubygems'
|
3
|
+
require 'parseconfig'
|
4
|
+
|
5
|
+
describe Sesame::Server do
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
config = ParseConfig.new "#{File.dirname(__FILE__)}/tests-config"
|
9
|
+
@host = config.get_value('host')
|
10
|
+
@port = config.get_value('port').to_i
|
11
|
+
@sesame_dir = config.get_value('sesame_dir')
|
12
|
+
@repo = config.get_value('repo')
|
13
|
+
@user = config.get_value('username')
|
14
|
+
@pass = config.get_value('password')
|
15
|
+
end
|
16
|
+
|
17
|
+
before :each do
|
18
|
+
@serv = Sesame::Server.new(@host, @port, @sesame_dir, @repo, @user, @pass)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should get a list of repos" do
|
22
|
+
repos = @serv.list_repos
|
23
|
+
repos.should == [ "SYSTEM", "CurrMap" ]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should get a list of all namespaces" do
|
27
|
+
namespaces = @serv.list_namespaces "CurrMap"
|
28
|
+
namespaces.should == {
|
29
|
+
"rdfs"=>"http://www.w3.org/2000/01/rdf-schema#",
|
30
|
+
"xsd"=>"http://www.w3.org/2001/XMLSchema#",
|
31
|
+
"rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#",
|
32
|
+
"base"=>"file://schema.xml#"
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should run a SeRQL query" do
|
37
|
+
query = <<EOF
|
38
|
+
SELECT C
|
39
|
+
FROM {C} rdf:type {base:Course}
|
40
|
+
USING NAMESPACE base = <file://schema.xml#>
|
41
|
+
EOF
|
42
|
+
res = @serv.select(query, "serql")
|
43
|
+
res.keys.should == ["C"]
|
44
|
+
res["C"].length.should == 1000
|
45
|
+
res
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should run a SPARQL query" do
|
49
|
+
query = <<EOF
|
50
|
+
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
|
51
|
+
PREFIX base:<file://schema.xml#>
|
52
|
+
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
|
53
|
+
PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>
|
54
|
+
SELECT ?x
|
55
|
+
WHERE
|
56
|
+
{ ?x rdf:type base:Course }
|
57
|
+
EOF
|
58
|
+
res = @serv.select(query, "sparql")
|
59
|
+
res.keys.should == ["x"]
|
60
|
+
res["x"].length.should == 1000
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: currmap-sesame
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- James N. V. Cash
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-13 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: nokogiri
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: rspec
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :development
|
43
|
+
version_requirements: *id002
|
44
|
+
description: Establish an object-database mapping to an OpenRDF Sesame Triple Store database
|
45
|
+
email: james.cash@occasionallycogent.com
|
46
|
+
executables: []
|
47
|
+
|
48
|
+
extensions: []
|
49
|
+
|
50
|
+
extra_rdoc_files:
|
51
|
+
- README.rdoc
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- README.rdoc
|
55
|
+
- Rakefile
|
56
|
+
- VERSION
|
57
|
+
- lib/sesame.rb
|
58
|
+
- spec/sesame_spec.rb
|
59
|
+
- spec/spec.opts
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: http://github.com/jamesnvc/currmap-sesame
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options:
|
67
|
+
- --charset=UTF-8
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.3.6
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Sesame RDF database ORM
|
91
|
+
test_files:
|
92
|
+
- spec/sesame_spec.rb
|
93
|
+
- spec/spec_helper.rb
|