iq_rdf 0.1.16 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +4 -1
- data/lib/iq_rdf/blank_node.rb +22 -2
- data/lib/iq_rdf/collection.rb +2 -2
- data/lib/iq_rdf/document.rb +25 -8
- data/lib/iq_rdf/literal/boolean.rb +1 -1
- data/lib/iq_rdf/literal/numeric.rb +1 -1
- data/lib/iq_rdf/literal/uri.rb +1 -1
- data/lib/iq_rdf/literal.rb +2 -2
- data/lib/iq_rdf/rails/iq_rdf.rb +14 -3
- data/lib/iq_rdf/uri.rb +28 -2
- data/lib/iq_rdf/version.rb +1 -1
- data/test/ntriples_test.rb +11 -11
- data/test/turtle_test.rb +57 -11
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 21e26c36ccb3dfca97f5c1224df1b95dec59c9672d837d2c3f4259a02acbbb36
|
4
|
+
data.tar.gz: 7f235328b11b0b864f3a283a715f86d498e6babf4d2a7e28457146b296fac78b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae3b1524338f266611e3d8976a20ba7ca5b6e8646cbc31c1ced591543714547f89cdeddcab4899ab92eff0109d13d46141394146b16e6b148a03cedf6b2ac432
|
7
|
+
data.tar.gz: 24f87a96f0995040ed7b8473b7102bd1e6bf490462384b196db0bbc85d5904ce63ecab75c4556bd9c7a25118957e657ccf5919796ee8bdb2050959625b3e207c
|
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
-
# IqRdf - RDF Renderering for Ruby and Rails
|
1
|
+
# IqRdf - RDF Renderering for Ruby and Rails
|
2
|
+
|
3
|
+
![CI](https://github.com/innoq/iq_rdf/workflows/CI/badge.svg)
|
4
|
+
|
2
5
|
IqRdf is a RDF renderer for Ruby and Rails. You can use it in any Ruby
|
3
6
|
environment to render Trurtle-, N-Triple- (not implemented yet) or XML-RDF.
|
4
7
|
|
data/lib/iq_rdf/blank_node.rb
CHANGED
@@ -26,14 +26,24 @@ module IqRdf
|
|
26
26
|
|
27
27
|
end
|
28
28
|
|
29
|
-
def to_s(
|
30
|
-
|
29
|
+
def to_s(options = {})
|
30
|
+
base_indent = options[:indent] || ""
|
31
|
+
predicate_indent = base_indent + "".ljust(4)
|
32
|
+
|
33
|
+
predicates = nodes.map { |p| format_predicate(p, indent: predicate_indent, lang: options[:lang]) }
|
34
|
+
.join(";\n")
|
35
|
+
|
36
|
+
"[\n#{predicates}\n#{base_indent} ]"
|
31
37
|
end
|
32
38
|
|
33
39
|
def build_predicate(*args, &block)
|
34
40
|
IqRdf::PredicateNamespace.new(self, IqRdf::Default).build_predicate(*args, &block)
|
35
41
|
end
|
36
42
|
|
43
|
+
def build_predicate_with_ns(namespace, *args, &block)
|
44
|
+
IqRdf::PredicateNamespace.new(self, namespace).build_predicate(*args, &block)
|
45
|
+
end
|
46
|
+
|
37
47
|
def build_xml(xml, &block)
|
38
48
|
block.call({}, lambda {
|
39
49
|
attrs = {}
|
@@ -46,5 +56,15 @@ module IqRdf
|
|
46
56
|
})
|
47
57
|
end
|
48
58
|
|
59
|
+
private
|
60
|
+
|
61
|
+
def format_predicate(predicate, options = {})
|
62
|
+
subjects = predicate.nodes
|
63
|
+
.map { |object| object.to_s(indent: options[:indent], lang: predicate.lang || options[:lang]) }
|
64
|
+
.join(", ")
|
65
|
+
|
66
|
+
"#{options[:indent]} #{predicate} #{subjects}"
|
67
|
+
end
|
68
|
+
|
49
69
|
end
|
50
70
|
end
|
data/lib/iq_rdf/collection.rb
CHANGED
data/lib/iq_rdf/document.rb
CHANGED
@@ -25,6 +25,7 @@ module IqRdf
|
|
25
25
|
@document_language = options[:lang]
|
26
26
|
|
27
27
|
@nodes = []
|
28
|
+
@config = {}
|
28
29
|
end
|
29
30
|
|
30
31
|
def namespaces(namespaces)
|
@@ -39,6 +40,10 @@ module IqRdf
|
|
39
40
|
self
|
40
41
|
end
|
41
42
|
|
43
|
+
def config(config = {})
|
44
|
+
@config = config
|
45
|
+
end
|
46
|
+
|
42
47
|
def <<(node)
|
43
48
|
return if node.nil?
|
44
49
|
raise ArgumentError, "Node must be an IqRdf::Uri and a Subject!" unless node.is_a?(IqRdf::Uri) and node.is_subject?
|
@@ -143,17 +148,29 @@ module IqRdf
|
|
143
148
|
s << "@prefix #{namespace.turtle_token}: <#{namespace.uri_prefix}>.\n"
|
144
149
|
end
|
145
150
|
s << "\n"
|
146
|
-
@nodes.each do |
|
147
|
-
pref =
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
+
@nodes.each do |subject|
|
152
|
+
pref = subject.to_s(@document_language)
|
153
|
+
indent = "".ljust(pref.length)
|
154
|
+
|
155
|
+
# Render subject, if it is defined as a RDF-type.
|
156
|
+
if subject.rdf_type
|
157
|
+
s << "#{pref} a #{subject.rdf_type}"
|
158
|
+
pref = ";\n" + indent
|
151
159
|
end
|
152
|
-
|
153
|
-
|
154
|
-
|
160
|
+
|
161
|
+
# Render all predicates and their associated objects
|
162
|
+
subject.nodes.each do |predicate|
|
163
|
+
objects = predicate.nodes.map { |object| object.to_s(indent: indent, lang: predicate.lang || subject.lang || @document_language) }
|
164
|
+
.join(", ")
|
165
|
+
|
166
|
+
s << "#{pref} #{predicate.to_s} #{objects}"
|
167
|
+
|
168
|
+
pref = ";\n" + indent
|
155
169
|
end
|
156
170
|
s << ".\n"
|
171
|
+
if @config[:empty_line_between_triples]
|
172
|
+
s << "\n"
|
173
|
+
end
|
157
174
|
end
|
158
175
|
s
|
159
176
|
end
|
data/lib/iq_rdf/literal/uri.rb
CHANGED
data/lib/iq_rdf/literal.rb
CHANGED
@@ -34,8 +34,8 @@ module IqRdf
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
def to_s(
|
38
|
-
lang = @lang ||
|
37
|
+
def to_s(options = {})
|
38
|
+
lang = @lang || options[:lang] # Use the Literals lang when given
|
39
39
|
lang = (lang && lang != :none) ? "@#{lang}" : ""
|
40
40
|
quote = @obj.to_s.include?("\n") ? '"""' : '"'
|
41
41
|
datatype = if @datatype.is_a?(::URI)
|
data/lib/iq_rdf/rails/iq_rdf.rb
CHANGED
@@ -12,10 +12,12 @@
|
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
14
|
|
15
|
-
def IqRdf.rails_template(template)
|
15
|
+
def IqRdf.rails_template(template, source = nil)
|
16
|
+
source ||= template.source
|
17
|
+
|
16
18
|
<<-EOV
|
17
19
|
document = IqRdf::Document.new()
|
18
|
-
#{
|
20
|
+
#{source}
|
19
21
|
if params[:format].to_s == "ttl"
|
20
22
|
controller.response.headers["Content-Type"] ||= 'text/turtle;charset=utf-8'
|
21
23
|
document.to_turtle
|
@@ -34,7 +36,16 @@ end
|
|
34
36
|
|
35
37
|
module ActionView
|
36
38
|
|
37
|
-
if Rails.version >= "
|
39
|
+
if Rails.version >= "6"
|
40
|
+
class Template::Handlers::IqRdf
|
41
|
+
# see https://github.com/rails/rails/commit/28f88e0074f473f58c2d3fd54cb3daff81027c12
|
42
|
+
def self.call(template, source)
|
43
|
+
IqRdf.rails_template(template, source)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
ActionView::Template.register_template_handler('iqrdf', ActionView::Template::Handlers::IqRdf)
|
47
|
+
|
48
|
+
elsif Rails.version >= "3"
|
38
49
|
|
39
50
|
class Template::Handlers::IqRdf
|
40
51
|
def self.call(template)
|
data/lib/iq_rdf/uri.rb
CHANGED
@@ -31,7 +31,7 @@ module IqRdf
|
|
31
31
|
"#{self.namespace.uri_prefix.to_s}#{self.uri_postfix.to_s}"
|
32
32
|
end
|
33
33
|
|
34
|
-
def to_s(
|
34
|
+
def to_s(options = {})
|
35
35
|
if namespace.token # There is a dummy_empty_namespace without token => postfix is a full uri!
|
36
36
|
"#{namespace.turtle_token}:#{self.uri_postfix.to_s}"
|
37
37
|
else
|
@@ -39,7 +39,7 @@ module IqRdf
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
def is_subject?
|
42
|
+
def is_subject?
|
43
43
|
if (nodes.size > 0) # walk through the nodes: a blank node is an object (but has nodes)
|
44
44
|
nodes.each do |node|
|
45
45
|
return true unless node.is_a?(BlankNode)
|
@@ -65,5 +65,31 @@ module IqRdf
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
|
+
# Method to add a blank node for the given predicate to the current subject
|
69
|
+
# In contrast to the "method_missing" implementation, this method allows one
|
70
|
+
# to choose a different namespace for the predicate.
|
71
|
+
#
|
72
|
+
# Usage:
|
73
|
+
# document << IqRdf::Eg.build_uri('test', IqRdf::Eg.build_uri('testType')) do |uri|
|
74
|
+
# uri.add_blank_node(IqRdf::Eg, :component) do |blank_node|
|
75
|
+
# blank_node.build_predicate_with_ns(IqRdf::Eg, :order, 1)
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# Resulting in the following turtle syntax:
|
80
|
+
# eg:test a eg:testType
|
81
|
+
# eg:component [
|
82
|
+
# eg:order 1
|
83
|
+
# ]
|
84
|
+
def add_blank_node(namespace, postfix, &block)
|
85
|
+
predicate = IqRdf::Predicate.new(namespace, postfix, self)
|
86
|
+
blank_node = IqRdf::BlankNode.new
|
87
|
+
|
88
|
+
block.call(blank_node)
|
89
|
+
|
90
|
+
predicate << blank_node
|
91
|
+
self << predicate
|
92
|
+
end
|
93
|
+
|
68
94
|
end
|
69
95
|
end
|
data/lib/iq_rdf/version.rb
CHANGED
data/test/ntriples_test.rb
CHANGED
@@ -206,18 +206,18 @@ _:b2 <http://www.umweltprobenbank.de/title> "blubb"@de .
|
|
206
206
|
over two lines"""@de .
|
207
207
|
<http://www.test.de/testemann> <http://www.test.de/some_literal> "text"@de .
|
208
208
|
rdf
|
209
|
-
|
210
|
-
|
211
|
-
def test_supress_if_empty_option
|
212
|
-
document = IqRdf::Document.new('http://www.test.de/')
|
213
|
-
document.namespaces :foaf => 'http://xmlns.com/foaf/0.1/'
|
209
|
+
end
|
214
210
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
document << IqRdf::testemann.Foaf::knows([], :suppress_if_empty => true)
|
211
|
+
def test_supress_if_empty_option
|
212
|
+
document = IqRdf::Document.new('http://www.test.de/')
|
213
|
+
document.namespaces :foaf => 'http://xmlns.com/foaf/0.1/'
|
219
214
|
|
220
|
-
|
221
|
-
|
215
|
+
document << IqRdf::testemann.Foaf::knows(:suppress_if_empty => true)
|
216
|
+
document << IqRdf::testemann.Foaf::knows(nil, :suppress_if_empty => true)
|
217
|
+
document << IqRdf::testemann.Foaf::knows("", :suppress_if_empty => true)
|
218
|
+
document << IqRdf::testemann.Foaf::knows([], :suppress_if_empty => true)
|
222
219
|
|
220
|
+
assert_equal("", document.to_ntriples.strip)
|
223
221
|
end
|
222
|
+
|
223
|
+
end
|
data/test/turtle_test.rb
CHANGED
@@ -117,11 +117,11 @@ upb:xkfkrl a skos:Concept;
|
|
117
117
|
:testIt (:hello :goodbye "bla"@de), "blubb"@de;
|
118
118
|
:anotherTest <http://www.test.de/foo>.
|
119
119
|
skos:testnode :test32 [
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
].
|
120
|
+
:title "dies ist ein test"@de;
|
121
|
+
:sub [
|
122
|
+
:title "blubb"@de
|
123
|
+
]
|
124
|
+
].
|
125
125
|
rdf
|
126
126
|
end
|
127
127
|
|
@@ -207,12 +207,58 @@ rdf
|
|
207
207
|
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
|
208
208
|
|
209
209
|
:testnode :test32 [
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
].
|
210
|
+
:title "dies ist ein test";
|
211
|
+
:test "Another test";
|
212
|
+
:sub [
|
213
|
+
:title "blubb"
|
214
|
+
]
|
215
|
+
].
|
216
|
+
rdf
|
217
|
+
end
|
218
|
+
|
219
|
+
def test_blank_nodes_with_different_ns
|
220
|
+
document = IqRdf::Document.new('http://www.test.de/')
|
221
|
+
document.namespaces(qb: 'http://purl.org/linked-data/cube#')
|
222
|
+
document.namespaces(eg: 'http://www.example.com')
|
223
|
+
|
224
|
+
document << IqRdf::Eg.build_uri('dsd', IqRdf::Qb.build_uri('DataStructureDefinition')) do |ds|
|
225
|
+
ds.add_blank_node(IqRdf::Qb, :component) do |blank_node|
|
226
|
+
blank_node.build_predicate_with_ns(IqRdf::Qb, :dimension, IqRdf::Eg.build_uri('refArea'))
|
227
|
+
blank_node.build_predicate_with_ns(IqRdf::Qb, :order, 1)
|
228
|
+
blank_node.build_predicate_with_ns(IqRdf::Eg, :cost, 2.25)
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
assert_equal(<<rdf, document.to_turtle)
|
233
|
+
@prefix : <http://www.test.de/>.
|
234
|
+
@prefix eg: <http://www.example.com>.
|
235
|
+
@prefix qb: <http://purl.org/linked-data/cube#>.
|
236
|
+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
|
237
|
+
|
238
|
+
eg:dsd a qb:DataStructureDefinition;
|
239
|
+
qb:component [
|
240
|
+
qb:dimension eg:refArea;
|
241
|
+
qb:order 1;
|
242
|
+
eg:cost 2.25
|
243
|
+
].
|
244
|
+
rdf
|
245
|
+
end
|
246
|
+
|
247
|
+
def test_config
|
248
|
+
document = IqRdf::Document.new('http://www.test.de/')
|
249
|
+
document.config(empty_line_between_triples: true)
|
250
|
+
|
251
|
+
document << IqRdf::testemann.myCustomNote("This is an example", :lang => :en)
|
252
|
+
document << IqRdf::testemann.myCustomNote("Zweites Beispiel", :lang => :de)
|
253
|
+
|
254
|
+
assert_equal(<<rdf, document.to_turtle)
|
255
|
+
@prefix : <http://www.test.de/>.
|
256
|
+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
|
257
|
+
|
258
|
+
:testemann :myCustomNote "This is an example"@en.
|
259
|
+
|
260
|
+
:testemann :myCustomNote "Zweites Beispiel"@de.
|
261
|
+
|
216
262
|
rdf
|
217
263
|
end
|
218
264
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iq_rdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Till Schulte-Coerne
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -104,7 +104,7 @@ files:
|
|
104
104
|
homepage: http://github.com/innoq/iq_rdf
|
105
105
|
licenses: []
|
106
106
|
metadata: {}
|
107
|
-
post_install_message:
|
107
|
+
post_install_message:
|
108
108
|
rdoc_options: []
|
109
109
|
require_paths:
|
110
110
|
- lib
|
@@ -119,9 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
- !ruby/object:Gem::Version
|
120
120
|
version: '0'
|
121
121
|
requirements: []
|
122
|
-
|
123
|
-
|
124
|
-
signing_key:
|
122
|
+
rubygems_version: 3.4.10
|
123
|
+
signing_key:
|
125
124
|
specification_version: 4
|
126
125
|
summary: IqRdf - A builder like rdf library for ruby and rails
|
127
126
|
test_files:
|