to-rdf 0.0.1
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/lib/to_rdf.rb +96 -0
- metadata +46 -0
data/lib/to_rdf.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'date'
|
3
|
+
|
4
|
+
module ToRdf
|
5
|
+
|
6
|
+
def self.to_rdf(objects)
|
7
|
+
namespaces = {}
|
8
|
+
rdf = ""
|
9
|
+
objects.each do |object|
|
10
|
+
namespaces.merge!(object.namespaces)
|
11
|
+
rdf += object.to_rdf_without_namespaces if object.respond_to? :to_rdf_without_namespaces
|
12
|
+
end
|
13
|
+
turtle_namespaces(namespaces) + rdf
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.turtle_namespaces(namespaces)
|
17
|
+
turtle = ""
|
18
|
+
namespaces.each do |k, v|
|
19
|
+
turtle += "@prefix #{k}: <#{v.to_s}> .\n"
|
20
|
+
end
|
21
|
+
turtle += "\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_rdf
|
25
|
+
rdf = to_rdf_without_namespaces
|
26
|
+
rdf ? ToRdf.turtle_namespaces(namespaces) + rdf : nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_rdf_without_namespaces
|
30
|
+
return unless uri
|
31
|
+
return if rdf_mapping.empty? and not type_uri
|
32
|
+
rdf = node_to_s(uri) + "\n"
|
33
|
+
rdf += "\ta #{node_to_s(type_uri)} ;\n" if type_uri
|
34
|
+
rdf_mapping.keys.each do |prop|
|
35
|
+
ns = prop.split(':').first
|
36
|
+
raise Exception.new("Namespace \"#{ns}\" is not defined, please override the namespaces method") unless namespaces.has_key? ns
|
37
|
+
value = rdf_mapping[prop]
|
38
|
+
next unless value
|
39
|
+
if value.respond_to? :each
|
40
|
+
value.each { |v| rdf += "\t#{prop} " + node_to_s(v) + " ;\n" }
|
41
|
+
else
|
42
|
+
rdf += "\t#{prop} " + node_to_s(value) + " ;\n"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
rdf += "\t.\n"
|
46
|
+
end
|
47
|
+
|
48
|
+
def node_to_s(node)
|
49
|
+
if node.class == String
|
50
|
+
s = (namespaces.has_key? node.split(':').first) ? node : "\"\"\"#{node.gsub('"', '\"')}\"\"\""
|
51
|
+
elsif node.class == Fixnum
|
52
|
+
s = "#{node}"
|
53
|
+
elsif node.class == Float
|
54
|
+
s = "#{node}"
|
55
|
+
elsif node.class == DateTime
|
56
|
+
s = "\"#{node}\"^^xsd:dateTime"
|
57
|
+
elsif node.kind_of? URI::Generic
|
58
|
+
s = "<#{node.to_s}>"
|
59
|
+
elsif node.class == TrueClass
|
60
|
+
s = "\"true\"^^xsd:boolean"
|
61
|
+
elsif node.class == FalseClass
|
62
|
+
s = "\"false\"^^xsd:boolean"
|
63
|
+
elsif node.respond_to? :uri
|
64
|
+
s = "<#{node.uri}>"
|
65
|
+
else
|
66
|
+
raise Exception.new("No mapping for #{node}")
|
67
|
+
end
|
68
|
+
s
|
69
|
+
end
|
70
|
+
|
71
|
+
# To be overridden
|
72
|
+
|
73
|
+
def namespaces
|
74
|
+
{
|
75
|
+
'dc11' => URI('http://purl.org/dc/elements/1.1/'),
|
76
|
+
'rdf' => URI('http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
|
77
|
+
'rdfs' => URI('http://www.w3.org/2000/01/rdf-schema#'),
|
78
|
+
'xsd' => URI('http://www.w3.org/2001/XMLSchema#'),
|
79
|
+
'owl' => URI('http://www.w3.org/2002/07/owl#'),
|
80
|
+
'foaf' => URI('http://xmlns.com/foaf/0.1/'),
|
81
|
+
'dc' => URI('http://purl.org/dc/terms/'),
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
def uri
|
86
|
+
nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def type_uri
|
90
|
+
nil
|
91
|
+
end
|
92
|
+
|
93
|
+
def rdf_mapping
|
94
|
+
{}
|
95
|
+
end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to-rdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Yves Raimond
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-11 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A small library for serializing Ruby objects as RDF/Turtle
|
15
|
+
email: yves.raimond@bbc.co.uk
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/to_rdf.rb
|
21
|
+
homepage: http://github.com/bbcrd/to-rdf
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.23
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Serialize Ruby objects as RDF/Turtle
|
45
|
+
test_files: []
|
46
|
+
has_rdoc: false
|