json-ld 3.2.3 → 3.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/json/ld/api.rb +807 -764
  4. data/lib/json/ld/compact.rb +304 -304
  5. data/lib/json/ld/conneg.rb +179 -161
  6. data/lib/json/ld/context.rb +2080 -1945
  7. data/lib/json/ld/expand.rb +745 -666
  8. data/lib/json/ld/extensions.rb +14 -13
  9. data/lib/json/ld/flatten.rb +257 -247
  10. data/lib/json/ld/format.rb +202 -194
  11. data/lib/json/ld/frame.rb +525 -502
  12. data/lib/json/ld/from_rdf.rb +224 -166
  13. data/lib/json/ld/html/nokogiri.rb +123 -121
  14. data/lib/json/ld/html/rexml.rb +151 -147
  15. data/lib/json/ld/reader.rb +107 -100
  16. data/lib/json/ld/resource.rb +224 -205
  17. data/lib/json/ld/streaming_reader.rb +574 -507
  18. data/lib/json/ld/streaming_writer.rb +93 -92
  19. data/lib/json/ld/to_rdf.rb +171 -167
  20. data/lib/json/ld/utils.rb +270 -264
  21. data/lib/json/ld/version.rb +24 -14
  22. data/lib/json/ld/writer.rb +334 -311
  23. data/lib/json/ld.rb +103 -96
  24. metadata +78 -209
  25. data/spec/api_spec.rb +0 -132
  26. data/spec/compact_spec.rb +0 -3482
  27. data/spec/conneg_spec.rb +0 -373
  28. data/spec/context_spec.rb +0 -2036
  29. data/spec/expand_spec.rb +0 -4496
  30. data/spec/flatten_spec.rb +0 -1203
  31. data/spec/format_spec.rb +0 -115
  32. data/spec/frame_spec.rb +0 -2498
  33. data/spec/from_rdf_spec.rb +0 -1005
  34. data/spec/matchers.rb +0 -20
  35. data/spec/rdfstar_spec.rb +0 -25
  36. data/spec/reader_spec.rb +0 -883
  37. data/spec/resource_spec.rb +0 -76
  38. data/spec/spec_helper.rb +0 -281
  39. data/spec/streaming_reader_spec.rb +0 -237
  40. data/spec/streaming_writer_spec.rb +0 -145
  41. data/spec/suite_compact_spec.rb +0 -22
  42. data/spec/suite_expand_spec.rb +0 -36
  43. data/spec/suite_flatten_spec.rb +0 -34
  44. data/spec/suite_frame_spec.rb +0 -29
  45. data/spec/suite_from_rdf_spec.rb +0 -22
  46. data/spec/suite_helper.rb +0 -411
  47. data/spec/suite_html_spec.rb +0 -22
  48. data/spec/suite_http_spec.rb +0 -35
  49. data/spec/suite_remote_doc_spec.rb +0 -22
  50. data/spec/suite_to_rdf_spec.rb +0 -30
  51. data/spec/support/extensions.rb +0 -44
  52. data/spec/test-files/test-1-compacted.jsonld +0 -10
  53. data/spec/test-files/test-1-context.jsonld +0 -7
  54. data/spec/test-files/test-1-expanded.jsonld +0 -5
  55. data/spec/test-files/test-1-input.jsonld +0 -10
  56. data/spec/test-files/test-1-rdf.ttl +0 -8
  57. data/spec/test-files/test-2-compacted.jsonld +0 -20
  58. data/spec/test-files/test-2-context.jsonld +0 -7
  59. data/spec/test-files/test-2-expanded.jsonld +0 -16
  60. data/spec/test-files/test-2-input.jsonld +0 -20
  61. data/spec/test-files/test-2-rdf.ttl +0 -14
  62. data/spec/test-files/test-3-compacted.jsonld +0 -11
  63. data/spec/test-files/test-3-context.jsonld +0 -8
  64. data/spec/test-files/test-3-expanded.jsonld +0 -10
  65. data/spec/test-files/test-3-input.jsonld +0 -11
  66. data/spec/test-files/test-3-rdf.ttl +0 -8
  67. data/spec/test-files/test-4-compacted.jsonld +0 -10
  68. data/spec/test-files/test-4-context.jsonld +0 -7
  69. data/spec/test-files/test-4-expanded.jsonld +0 -6
  70. data/spec/test-files/test-4-input.jsonld +0 -10
  71. data/spec/test-files/test-4-rdf.ttl +0 -5
  72. data/spec/test-files/test-5-compacted.jsonld +0 -13
  73. data/spec/test-files/test-5-context.jsonld +0 -7
  74. data/spec/test-files/test-5-expanded.jsonld +0 -9
  75. data/spec/test-files/test-5-input.jsonld +0 -13
  76. data/spec/test-files/test-5-rdf.ttl +0 -7
  77. data/spec/test-files/test-6-compacted.jsonld +0 -10
  78. data/spec/test-files/test-6-context.jsonld +0 -7
  79. data/spec/test-files/test-6-expanded.jsonld +0 -10
  80. data/spec/test-files/test-6-input.jsonld +0 -10
  81. data/spec/test-files/test-6-rdf.ttl +0 -6
  82. data/spec/test-files/test-7-compacted.jsonld +0 -23
  83. data/spec/test-files/test-7-context.jsonld +0 -4
  84. data/spec/test-files/test-7-expanded.jsonld +0 -20
  85. data/spec/test-files/test-7-input.jsonld +0 -23
  86. data/spec/test-files/test-7-rdf.ttl +0 -14
  87. data/spec/test-files/test-8-compacted.jsonld +0 -34
  88. data/spec/test-files/test-8-context.jsonld +0 -11
  89. data/spec/test-files/test-8-expanded.jsonld +0 -24
  90. data/spec/test-files/test-8-frame.jsonld +0 -18
  91. data/spec/test-files/test-8-framed.jsonld +0 -25
  92. data/spec/test-files/test-8-input.jsonld +0 -30
  93. data/spec/test-files/test-8-rdf.ttl +0 -15
  94. data/spec/test-files/test-9-compacted.jsonld +0 -20
  95. data/spec/test-files/test-9-context.jsonld +0 -13
  96. data/spec/test-files/test-9-expanded.jsonld +0 -14
  97. data/spec/test-files/test-9-input.jsonld +0 -12
  98. data/spec/to_rdf_spec.rb +0 -1551
  99. data/spec/writer_spec.rb +0 -427
data/spec/writer_spec.rb DELETED
@@ -1,427 +0,0 @@
1
- # coding: utf-8
2
- require_relative 'spec_helper'
3
- require 'rdf/spec/writer'
4
-
5
- describe JSON::LD::Writer do
6
- let(:logger) {RDF::Spec.logger}
7
-
8
- after(:each) {|example| puts logger.to_s if example.exception}
9
-
10
- it_behaves_like 'an RDF::Writer' do
11
- let(:writer) {JSON::LD::Writer.new(StringIO.new, logger: logger)}
12
- end
13
-
14
- describe ".for" do
15
- [
16
- :jsonld,
17
- "etc/doap.jsonld",
18
- {file_name: 'etc/doap.jsonld'},
19
- {file_extension: 'jsonld'},
20
- {content_type: 'application/ld+json'},
21
- {content_type: 'application/x-ld+json'},
22
- ].each do |arg|
23
- it "discovers with #{arg.inspect}" do
24
- expect(RDF::Reader.for(arg)).to eq JSON::LD::Reader
25
- end
26
- end
27
- end
28
-
29
- context "simple tests" do
30
- it "should use full URIs without base" do
31
- input = %(<http://a/b> <http://a/c> <http://a/d> .)
32
- expect(serialize(input)).to produce_jsonld([{
33
- '@id' => "http://a/b",
34
- "http://a/c" => [{"@id" => "http://a/d"}]
35
- }], logger)
36
- end
37
-
38
- it "should use qname URIs with standard prefix" do
39
- input = %(<http://xmlns.com/foaf/0.1/b> <http://xmlns.com/foaf/0.1/c> <http://xmlns.com/foaf/0.1/d> .)
40
- expect(serialize(input, standard_prefixes: true)).to produce_jsonld({
41
- '@context' => {
42
- "foaf" => "http://xmlns.com/foaf/0.1/",
43
- },
44
- '@id' => "foaf:b",
45
- "foaf:c" => {"@id" => "foaf:d"}
46
- }, logger)
47
- end
48
-
49
- it "should use qname URIs with parsed prefix" do
50
- input = %(
51
- <https://senet.org/gm> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://vocab.org/frbr/core#Work> .
52
- <https://senet.org/gm> <http://purl.org/dc/terms/title> "Rhythm Paradise"@en .
53
- <https://senet.org/gm> <https://senet.org/ns#unofficialTitle> "Rhythm Tengoku"@en .
54
- <https://senet.org/gm> <https://senet.org/ns#urlkey> "rhythm-tengoku" .
55
- )
56
- expect(serialize(input, prefixes: {
57
- dc: "http://purl.org/dc/terms/",
58
- frbr: "http://vocab.org/frbr/core#",
59
- senet: "https://senet.org/ns#",
60
- })).to produce_jsonld({
61
- '@context' => {
62
- "dc" => "http://purl.org/dc/terms/",
63
- "frbr" => "http://vocab.org/frbr/core#",
64
- "senet" => "https://senet.org/ns#"
65
- },
66
- '@id' => "https://senet.org/gm",
67
- "@type" => "frbr:Work",
68
- "dc:title" => {"@value" => "Rhythm Paradise","@language" => "en"},
69
- "senet:unofficialTitle" => {"@value" => "Rhythm Tengoku","@language" => "en"},
70
- "senet:urlkey" => "rhythm-tengoku"
71
- }, logger)
72
- end
73
-
74
- it "should use CURIEs with empty prefix" do
75
- input = %(<http://xmlns.com/foaf/0.1/b> <http://xmlns.com/foaf/0.1/c> <http://xmlns.com/foaf/0.1/d> .)
76
- begin
77
- expect(serialize(input, prefixes: { "" => RDF::Vocab::FOAF})).
78
- to produce_jsonld({
79
- "@context" => {
80
- "" => "http://xmlns.com/foaf/0.1/"
81
- },
82
- '@id' => ":b",
83
- ":c" => {"@id" => ":d"}
84
- }, logger)
85
- rescue JSON::LD::JsonLdError, JSON::LD::JsonLdError, TypeError => e
86
- fail("#{e.class}: #{e.message}\n" +
87
- "#{logger}\n" +
88
- "Backtrace:\n#{e.backtrace.join("\n")}")
89
- end
90
- end
91
-
92
- it "should not use terms if no suffix" do
93
- input = %(<http://xmlns.com/foaf/0.1/> <http://xmlns.com/foaf/0.1/> <http://xmlns.com/foaf/0.1/> .)
94
- expect(serialize(input, standard_prefixes: true)).
95
- not_to produce_jsonld({
96
- "@context" => {"foaf" => "http://xmlns.com/foaf/0.1/"},
97
- '@id' => "foaf",
98
- "foaf" => {"@id" => "foaf"}
99
- }, logger)
100
- end
101
-
102
- it "should not use CURIE with illegal local part" do
103
- input = %(
104
- @prefix db: <http://dbpedia.org/resource/> .
105
- @prefix dbo: <http://dbpedia.org/ontology/> .
106
- db:Michael_Jackson dbo:artistOf <http://dbpedia.org/resource/%28I_Can%27t_Make_It%29_Another_Day> .
107
- )
108
-
109
- expect(serialize(input, prefixes: {
110
- "db" => RDF::URI("http://dbpedia.org/resource/"),
111
- "dbo" => RDF::URI("http://dbpedia.org/ontology/")})).
112
- to produce_jsonld({
113
- "@context" => {
114
- "db" => "http://dbpedia.org/resource/",
115
- "dbo" => "http://dbpedia.org/ontology/"
116
- },
117
- '@id' => "db:Michael_Jackson",
118
- "dbo:artistOf" => {"@id" => "db:%28I_Can%27t_Make_It%29_Another_Day"}
119
- }, logger)
120
- end
121
-
122
- it "should not use provided node identifiers if :unique_bnodes set" do
123
- input = %(_:a <http://example.com/foo> _:b \.)
124
- result = serialize(input, unique_bnodes: true, context: {})
125
- expect(result.to_json).to match(%r(_:g\w+))
126
- end
127
-
128
- it "serializes multiple subjects" do
129
- input = %q(
130
- @prefix : <http://www.w3.org/2006/03/test-description#> .
131
- @prefix dc: <http://purl.org/dc/terms/> .
132
- <http://example.com/test-cases/0001> a :TestCase .
133
- <http://example.com/test-cases/0002> a :TestCase .
134
- )
135
- expect(serialize(input, prefixes: {"" => "http://www.w3.org/2006/03/test-description#"})).
136
- to produce_jsonld({
137
- '@context' => {
138
- "" => "http://www.w3.org/2006/03/test-description#",
139
- "dc" => RDF::Vocab::DC.to_s
140
- },
141
- '@graph' => [
142
- {'@id' => "http://example.com/test-cases/0001", '@type' => ":TestCase"},
143
- {'@id' => "http://example.com/test-cases/0002", '@type' => ":TestCase"}
144
- ]
145
- }, logger)
146
- end
147
-
148
- it "serializes Wikia OWL example" do
149
- input = %q(
150
- @prefix owl: <http://www.w3.org/2002/07/owl#> .
151
- @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
152
- @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
153
- @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
154
-
155
- <http://data.wikia.com/terms#Character> a owl:Class;
156
- rdfs:subClassOf _:a .
157
- _:a a owl:Restriction;
158
- owl:minQualifiedCardinality "1"^^xsd:nonNegativeInteger;
159
- owl:onClass <http://data.wikia.com/terms#Element>;
160
- owl:onProperty <http://data.wikia.com/terms#characterIn> .
161
- )
162
- expect(serialize(input, rename_bnodes: false, prefixes: {
163
- owl: "http://www.w3.org/2002/07/owl#",
164
- rdfs: "http://www.w3.org/2000/01/rdf-schema#",
165
- xsd: "http://www.w3.org/2001/XMLSchema#"
166
- })).
167
- to produce_jsonld({
168
- '@context' => {
169
- "owl" => "http://www.w3.org/2002/07/owl#",
170
- "rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
171
- "rdfs" => "http://www.w3.org/2000/01/rdf-schema#",
172
- "xsd" => "http://www.w3.org/2001/XMLSchema#"
173
- },
174
- '@graph' => [
175
- {
176
- "@id" => "_:a",
177
- "@type" => "owl:Restriction",
178
- "owl:minQualifiedCardinality" => {"@value" => "1","@type" => "xsd:nonNegativeInteger"},
179
- "owl:onClass" => {"@id" => "http://data.wikia.com/terms#Element"},
180
- "owl:onProperty" => {"@id" => "http://data.wikia.com/terms#characterIn"}
181
- },
182
- {
183
- "@id" => "http://data.wikia.com/terms#Character",
184
- "@type" => "owl:Class",
185
- "rdfs:subClassOf" => {"@id" => "_:a"}
186
- }
187
- ]
188
- }, logger)
189
- end
190
- end
191
-
192
- context "RDF-star" do
193
- {
194
- "subject-iii": {
195
- input: RDF::Statement(
196
- RDF::Statement(
197
- RDF::URI('http://example/s1'),
198
- RDF::URI('http://example/p1'),
199
- RDF::URI('http://example/o1')),
200
- RDF::URI('http://example/p'),
201
- RDF::URI('http://example/o')),
202
- output: %({
203
- "@context": {"ex": "http://example/"},
204
- "@id": {
205
- "@id": "ex:s1",
206
- "ex:p1": {"@id": "ex:o1"}
207
- },
208
- "ex:p": {"@id": "ex:o"}
209
- })
210
- },
211
- "subject-iib": {
212
- input: RDF::Statement(
213
- RDF::Statement(
214
- RDF::URI('http://example/s1'),
215
- RDF::URI('http://example/p1'),
216
- RDF::Node.new('o1')),
217
- RDF::URI('http://example/p'),
218
- RDF::URI('http://example/o')),
219
- output: %({
220
- "@context": {"ex": "http://example/"},
221
- "@id": {
222
- "@id": "ex:s1",
223
- "ex:p1": {"@id": "_:o1"}
224
- },
225
- "ex:p": {"@id": "ex:o"}
226
- })
227
- },
228
- "subject-iil": {
229
- input: RDF::Statement(
230
- RDF::Statement(
231
- RDF::URI('http://example/s1'),
232
- RDF::URI('http://example/p1'),
233
- RDF::Literal('o1')),
234
- RDF::URI('http://example/p'),
235
- RDF::URI('http://example/o')),
236
- output: %({
237
- "@context": {"ex": "http://example/"},
238
- "@id": {
239
- "@id": "ex:s1",
240
- "ex:p1": "o1"
241
- },
242
- "ex:p": {"@id": "ex:o"}
243
- })
244
- },
245
- "subject-bii": {
246
- input: RDF::Statement(
247
- RDF::Statement(
248
- RDF::Node('s1'),
249
- RDF::URI('http://example/p1'),
250
- RDF::URI('http://example/o1')),
251
- RDF::URI('http://example/p'),
252
- RDF::URI('http://example/o')),
253
- output: %({
254
- "@context": {"ex": "http://example/"},
255
- "@id": {
256
- "@id": "_:s1",
257
- "ex:p1": {"@id": "ex:o1"}
258
- },
259
- "ex:p": {"@id": "ex:o"}
260
- })
261
- },
262
- "subject-bib": {
263
- input: RDF::Statement(
264
- RDF::Statement(
265
- RDF::Node('s1'),
266
- RDF::URI('http://example/p1'),
267
- RDF::Node.new('o1')),
268
- RDF::URI('http://example/p'), RDF::URI('http://example/o')),
269
- output: %({
270
- "@context": {"ex": "http://example/"},
271
- "@id": {
272
- "@id": "_:s1",
273
- "ex:p1": {"@id": "_:o1"}
274
- },
275
- "ex:p": {"@id": "ex:o"}
276
- })
277
- },
278
- "subject-bil": {
279
- input: RDF::Statement(
280
- RDF::Statement(
281
- RDF::Node('s1'),
282
- RDF::URI('http://example/p1'),
283
- RDF::Literal('o1')),
284
- RDF::URI('http://example/p'),
285
- RDF::URI('http://example/o')),
286
- output: %({
287
- "@context": {"ex": "http://example/"},
288
- "@id": {
289
- "@id": "_:s1",
290
- "ex:p1": "o1"
291
- },
292
- "ex:p": {"@id": "ex:o"}
293
- })
294
- },
295
- "object-iii": {
296
- input: RDF::Statement(
297
- RDF::URI('http://example/s'),
298
- RDF::URI('http://example/p'),
299
- RDF::Statement(
300
- RDF::URI('http://example/s1'),
301
- RDF::URI('http://example/p1'),
302
- RDF::URI('http://example/o1'))),
303
- output: %({
304
- "@context": {"ex": "http://example/"},
305
- "@id": "ex:s",
306
- "ex:p": {
307
- "@id": {
308
- "@id": "ex:s1",
309
- "ex:p1": {"@id": "ex:o1"}
310
- }
311
- }
312
- })
313
- },
314
- "object-iib": {
315
- input: RDF::Statement(
316
- RDF::URI('http://example/s'),
317
- RDF::URI('http://example/p'),
318
- RDF::Statement(
319
- RDF::URI('http://example/s1'),
320
- RDF::URI('http://example/p1'),
321
- RDF::Node.new('o1'))),
322
- output: %({
323
- "@context": {"ex": "http://example/"},
324
- "@id": "ex:s",
325
- "ex:p": {
326
- "@id": {
327
- "@id": "ex:s1",
328
- "ex:p1": {"@id": "_:o1"}
329
- }
330
- }
331
- })
332
- },
333
- "object-iil": {
334
- input: RDF::Statement(
335
- RDF::URI('http://example/s'),
336
- RDF::URI('http://example/p'),
337
- RDF::Statement(
338
- RDF::URI('http://example/s1'),
339
- RDF::URI('http://example/p1'),
340
- RDF::Literal('o1'))),
341
- output: %({
342
- "@context": {"ex": "http://example/"},
343
- "@id": "ex:s",
344
- "ex:p": {
345
- "@id": {
346
- "@id": "ex:s1",
347
- "ex:p1": "o1"
348
- }
349
- }
350
- })
351
- },
352
- "recursive-subject": {
353
- input: RDF::Statement(
354
- RDF::Statement(
355
- RDF::Statement(
356
- RDF::URI('http://example/s2'),
357
- RDF::URI('http://example/p2'),
358
- RDF::URI('http://example/o2')),
359
- RDF::URI('http://example/p1'),
360
- RDF::URI('http://example/o1')),
361
- RDF::URI('http://example/p'),
362
- RDF::URI('http://example/o')),
363
- output: %({
364
- "@context": {"ex": "http://example/"},
365
- "@id": {
366
- "@id": {
367
- "@id": "ex:s2",
368
- "ex:p2": {"@id": "ex:o2"}
369
- },
370
- "ex:p1": {"@id": "ex:o1"}
371
- },
372
- "ex:p": {"@id": "ex:o"}
373
- })
374
- },
375
- }.each do |name, params|
376
- it name do
377
- graph = RDF::Graph.new {|g| g << params[:input]}
378
- expect(
379
- serialize(graph, rdfstar: true, prefixes: {ex: 'http://example/'})
380
- ).to produce_jsonld(JSON.parse(params[:output]), logger)
381
- end
382
- end
383
- end
384
-
385
- context "Writes fromRdf tests to isomorphic graph" do
386
- require 'suite_helper'
387
- m = Fixtures::SuiteTest::Manifest.open("#{Fixtures::SuiteTest::SUITE}fromRdf-manifest.jsonld")
388
- describe m.name do
389
- m.entries.each do |t|
390
- next unless t.positiveTest? && !t.property('input').include?('0016')
391
- specify "#{t.property('@id')}: #{t.name}" do
392
- logger.info "test: #{t.inspect}"
393
- logger.info "source: #{t.input}"
394
- t.logger = logger
395
- pending "Shared list BNode in different graphs" if t.property('input').include?("fromRdf-0021")
396
- repo = RDF::Repository.load(t.input_loc, format: :nquads)
397
- jsonld = JSON::LD::Writer.buffer(logger: t.logger, **t.options) do |writer|
398
- writer << repo
399
- end
400
-
401
- # And then, re-generate jsonld as RDF
402
-
403
- expect(parse(jsonld, format: :jsonld, **t.options)).to be_equivalent_graph(repo, t)
404
- end
405
- end
406
- end
407
- end unless ENV['CI']
408
-
409
- def parse(input, format: :trig, **options)
410
- reader = RDF::Reader.for(format)
411
- RDF::Repository.new << reader.new(input, **options)
412
- end
413
-
414
- # Serialize ntstr to a string and compare against regexps
415
- def serialize(ntstr, **options)
416
- g = ntstr.is_a?(String) ? parse(ntstr, **options) : ntstr
417
- #logger.info g.dump(:ttl)
418
- result = JSON::LD::Writer.buffer(logger: logger, **options) do |writer|
419
- writer << g
420
- end
421
- if $verbose
422
- #puts hash.to_json
423
- end
424
-
425
- JSON.parse(result)
426
- end
427
- end