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
@@ -1,344 +1,367 @@
1
- # -*- encoding: utf-8 -*-
2
1
  # frozen_string_literal: true
2
+
3
3
  require 'json/ld/streaming_writer'
4
4
  require 'link_header'
5
5
 
6
- module JSON::LD
7
- ##
8
- # A JSON-LD parser in Ruby.
9
- #
10
- # Note that the natural interface is to write a whole graph at a time.
11
- # Writing statements or Triples will create a graph to add them to
12
- # and then serialize the graph.
13
- #
14
- # @example Obtaining a JSON-LD writer class
15
- # RDF::Writer.for(:jsonld) #=> JSON::LD::Writer
16
- # RDF::Writer.for("etc/test.json")
17
- # RDF::Writer.for(:file_name => "etc/test.json")
18
- # RDF::Writer.for(file_extension: "json")
19
- # RDF::Writer.for(:content_type => "application/turtle")
20
- #
21
- # @example Serializing RDF graph into an JSON-LD file
22
- # JSON::LD::Writer.open("etc/test.json") do |writer|
23
- # writer << graph
24
- # end
25
- #
26
- # @example Serializing RDF statements into an JSON-LD file
27
- # JSON::LD::Writer.open("etc/test.json") do |writer|
28
- # graph.each_statement do |statement|
29
- # writer << statement
30
- # end
31
- # end
32
- #
33
- # @example Serializing RDF statements into an JSON-LD string
34
- # JSON::LD::Writer.buffer do |writer|
35
- # graph.each_statement do |statement|
36
- # writer << statement
37
- # end
38
- # end
39
- #
40
- # The writer will add prefix definitions, and use them for creating @context definitions, and minting CURIEs
41
- #
42
- # @example Creating @@context prefix definitions in output
43
- # JSON::LD::Writer.buffer(
44
- # prefixes: {
45
- # nil => "http://example.com/ns#",
46
- # foaf: "http://xmlns.com/foaf/0.1/"}
47
- # ) do |writer|
48
- # graph.each_statement do |statement|
49
- # writer << statement
50
- # end
51
- # end
52
- #
53
- # Select the :expand option to output JSON-LD in expanded form
54
- #
55
- # @see https://www.w3.org/TR/json-ld11-api/
56
- # @see https://www.w3.org/TR/json-ld11-api/#the-normalization-algorithm
57
- # @author [Gregg Kellogg](http://greggkellogg.net/)
58
- class Writer < RDF::Writer
59
- include StreamingWriter
60
- include Utils
61
- include RDF::Util::Logger
62
- format Format
63
-
64
- # @!attribute [r] graph
65
- # @return [RDF::Graph] Graph of statements serialized
66
- attr_reader :graph
67
-
68
- # @!attribute [r] context
69
- # @return [Context] context used to load and administer contexts
70
- attr_reader :context
71
-
6
+ module JSON
7
+ module LD
72
8
  ##
73
- # JSON-LD Writer options
74
- # @see https://ruby-rdf.github.io/rdf/RDF/Writer#options-class_method
75
- def self.options
76
- super + [
77
- RDF::CLI::Option.new(
78
- symbol: :compactArrays,
79
- datatype: TrueClass,
80
- default: true,
81
- control: :checkbox,
82
- on: ["--[no-]compact-arrays"],
83
- description: "Replaces arrays with just one element with that element during compaction. Default is `true` use --no-compact-arrays to disable.") {|arg| arg},
84
- RDF::CLI::Option.new(
85
- symbol: :compactToRelative,
86
- datatype: TrueClass,
87
- default: true,
88
- control: :checkbox,
89
- on: ["--[no-]compact-to-relative"],
90
- description: "Creates document relative IRIs when compacting, if `true`, otherwise leaves expanded. Default is `true` use --no-compact-to-relative to disable.") {|arg| arg},
91
- RDF::CLI::Option.new(
92
- symbol: :context,
93
- datatype: RDF::URI,
94
- control: :url2,
95
- on: ["--context CONTEXT"],
96
- description: "Context to use when compacting.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
97
- RDF::CLI::Option.new(
98
- symbol: :embed,
99
- datatype: %w(@always @once @never),
100
- default: '@once',
101
- control: :select,
102
- on: ["--embed EMBED"],
103
- description: "How to embed matched objects (@once).") {|arg| RDF::URI(arg)},
104
- RDF::CLI::Option.new(
105
- symbol: :explicit,
106
- datatype: TrueClass,
107
- control: :checkbox,
108
- on: ["--[no-]explicit"],
109
- description: "Only include explicitly declared properties in output (false)") {|arg| arg},
110
- RDF::CLI::Option.new(
111
- symbol: :frame,
112
- datatype: RDF::URI,
113
- control: :url2,
114
- use: :required,
115
- on: ["--frame FRAME"],
116
- description: "Frame to use when serializing.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
117
- RDF::CLI::Option.new(
118
- symbol: :lowercaseLanguage,
119
- datatype: TrueClass,
120
- control: :checkbox,
121
- on: ["--[no-]lowercase-language"],
122
- description: "By default, language tags are left as is. To normalize to lowercase, set this option to `true`."),
123
- RDF::CLI::Option.new(
124
- symbol: :omitDefault,
125
- datatype: TrueClass,
126
- control: :checkbox,
127
- on: ["--[no-]omitDefault"],
128
- description: "Omit missing properties from output (false)") {|arg| arg},
129
- RDF::CLI::Option.new(
130
- symbol: :ordered,
131
- datatype: TrueClass,
132
- control: :checkbox,
133
- on: ["--[no-]ordered"],
134
- description: "Order object member processing lexographically.") {|arg| arg},
135
- RDF::CLI::Option.new(
136
- symbol: :processingMode,
137
- datatype: %w(json-ld-1.0 json-ld-1.1),
138
- control: :radio,
139
- on: ["--processingMode MODE", %w(json-ld-1.0 json-ld-1.1)],
140
- description: "Set Processing Mode (json-ld-1.0 or json-ld-1.1)"),
141
- RDF::CLI::Option.new(
142
- symbol: :rdfDirection,
143
- datatype: %w(i18n-datatype compound-literal),
144
- default: 'null',
145
- control: :select,
146
- on: ["--rdf-direction DIR", %w(i18n-datatype compound-literal)],
147
- description: "How to serialize literal direction (i18n-datatype compound-literal)") {|arg| arg},
148
- RDF::CLI::Option.new(
149
- symbol: :requireAll,
150
- datatype: TrueClass,
151
- default: true,
152
- control: :checkbox,
153
- on: ["--[no-]require-all"],
154
- description: "Require all properties to match (true). Default is `true` use --no-require-all to disable.") {|arg| arg},
155
- RDF::CLI::Option.new(
156
- symbol: :stream,
157
- datatype: TrueClass,
158
- control: :checkbox,
159
- on: ["--[no-]stream"],
160
- description: "Do not attempt to optimize graph presentation, suitable for streaming large graphs.") {|arg| arg},
161
- RDF::CLI::Option.new(
162
- symbol: :useNativeTypes,
163
- datatype: TrueClass,
164
- control: :checkbox,
165
- on: ["--[no-]use-native-types"],
166
- description: "Use native JSON values in value objects.") {|arg| arg},
167
- RDF::CLI::Option.new(
168
- symbol: :useRdfType,
169
- datatype: TrueClass,
170
- control: :checkbox,
171
- on: ["--[no-]use-rdf-type"],
172
- description: "Treat `rdf:type` like a normal property instead of using `@type`.") {|arg| arg},
173
- ]
174
- end
9
+ # A JSON-LD parser in Ruby.
10
+ #
11
+ # Note that the natural interface is to write a whole graph at a time.
12
+ # Writing statements or Triples will create a graph to add them to
13
+ # and then serialize the graph.
14
+ #
15
+ # @example Obtaining a JSON-LD writer class
16
+ # RDF::Writer.for(:jsonld) #=> JSON::LD::Writer
17
+ # RDF::Writer.for("etc/test.json")
18
+ # RDF::Writer.for(:file_name => "etc/test.json")
19
+ # RDF::Writer.for(file_extension: "json")
20
+ # RDF::Writer.for(:content_type => "application/turtle")
21
+ #
22
+ # @example Serializing RDF graph into an JSON-LD file
23
+ # JSON::LD::Writer.open("etc/test.json") do |writer|
24
+ # writer << graph
25
+ # end
26
+ #
27
+ # @example Serializing RDF statements into an JSON-LD file
28
+ # JSON::LD::Writer.open("etc/test.json") do |writer|
29
+ # graph.each_statement do |statement|
30
+ # writer << statement
31
+ # end
32
+ # end
33
+ #
34
+ # @example Serializing RDF statements into an JSON-LD string
35
+ # JSON::LD::Writer.buffer do |writer|
36
+ # graph.each_statement do |statement|
37
+ # writer << statement
38
+ # end
39
+ # end
40
+ #
41
+ # The writer will add prefix definitions, and use them for creating @context definitions, and minting CURIEs
42
+ #
43
+ # @example Creating @@context prefix definitions in output
44
+ # JSON::LD::Writer.buffer(
45
+ # prefixes: {
46
+ # nil => "http://example.com/ns#",
47
+ # foaf: "http://xmlns.com/foaf/0.1/"}
48
+ # ) do |writer|
49
+ # graph.each_statement do |statement|
50
+ # writer << statement
51
+ # end
52
+ # end
53
+ #
54
+ # Select the :expand option to output JSON-LD in expanded form
55
+ #
56
+ # @see https://www.w3.org/TR/json-ld11-api/
57
+ # @see https://www.w3.org/TR/json-ld11-api/#the-normalization-algorithm
58
+ # @author [Gregg Kellogg](http://greggkellogg.net/)
59
+ class Writer < RDF::Writer
60
+ include StreamingWriter
61
+ include Utils
62
+ include RDF::Util::Logger
63
+ format Format
64
+
65
+ # @!attribute [r] graph
66
+ # @return [RDF::Graph] Graph of statements serialized
67
+ attr_reader :graph
175
68
 
176
- class << self
177
- attr_reader :white_list
178
- attr_reader :black_list
69
+ # @!attribute [r] context
70
+ # @return [Context] context used to load and administer contexts
71
+ attr_reader :context
179
72
 
180
73
  ##
181
- # Use parameters from accept-params to determine if the parameters are acceptable to invoke this writer. The `accept_params` will subsequently be provided to the writer instance.
182
- #
183
- # @param [Hash{Symbol => String}] accept_params
184
- # @yield [accept_params] if a block is given, returns the result of evaluating that block
185
- # @yieldparam [Hash{Symbol => String}] accept_params
186
- # @return [Boolean]
187
- # @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
188
- def accept?(accept_params)
189
- if block_given?
190
- yield(accept_params)
191
- else
192
- true
193
- end
74
+ # JSON-LD Writer options
75
+ # @see https://ruby-rdf.github.io/rdf/RDF/Writer#options-class_method
76
+ def self.options
77
+ super + [
78
+ RDF::CLI::Option.new(
79
+ symbol: :compactArrays,
80
+ datatype: TrueClass,
81
+ default: true,
82
+ control: :checkbox,
83
+ on: ["--[no-]compact-arrays"],
84
+ description: "Replaces arrays with just one element with that element during compaction. Default is `true` use --no-compact-arrays to disable."
85
+ ) { |arg| arg },
86
+ RDF::CLI::Option.new(
87
+ symbol: :compactToRelative,
88
+ datatype: TrueClass,
89
+ default: true,
90
+ control: :checkbox,
91
+ on: ["--[no-]compact-to-relative"],
92
+ description: "Creates document relative IRIs when compacting, if `true`, otherwise leaves expanded. Default is `true` use --no-compact-to-relative to disable."
93
+ ) { |arg| arg },
94
+ RDF::CLI::Option.new(
95
+ symbol: :context,
96
+ datatype: RDF::URI,
97
+ control: :url2,
98
+ on: ["--context CONTEXT"],
99
+ description: "Context to use when compacting."
100
+ ) { |arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg)) },
101
+ RDF::CLI::Option.new(
102
+ symbol: :embed,
103
+ datatype: %w[@always @once @never],
104
+ default: '@once',
105
+ control: :select,
106
+ on: ["--embed EMBED"],
107
+ description: "How to embed matched objects (@once)."
108
+ ) { |arg| RDF::URI(arg) },
109
+ RDF::CLI::Option.new(
110
+ symbol: :explicit,
111
+ datatype: TrueClass,
112
+ control: :checkbox,
113
+ on: ["--[no-]explicit"],
114
+ description: "Only include explicitly declared properties in output (false)"
115
+ ) { |arg| arg },
116
+ RDF::CLI::Option.new(
117
+ symbol: :frame,
118
+ datatype: RDF::URI,
119
+ control: :url2,
120
+ use: :required,
121
+ on: ["--frame FRAME"],
122
+ description: "Frame to use when serializing."
123
+ ) { |arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg)) },
124
+ RDF::CLI::Option.new(
125
+ symbol: :lowercaseLanguage,
126
+ datatype: TrueClass,
127
+ control: :checkbox,
128
+ on: ["--[no-]lowercase-language"],
129
+ description: "By default, language tags are left as is. To normalize to lowercase, set this option to `true`."
130
+ ),
131
+ RDF::CLI::Option.new(
132
+ symbol: :omitDefault,
133
+ datatype: TrueClass,
134
+ control: :checkbox,
135
+ on: ["--[no-]omitDefault"],
136
+ description: "Omit missing properties from output (false)"
137
+ ) { |arg| arg },
138
+ RDF::CLI::Option.new(
139
+ symbol: :ordered,
140
+ datatype: TrueClass,
141
+ control: :checkbox,
142
+ on: ["--[no-]ordered"],
143
+ description: "Order object member processing lexographically."
144
+ ) { |arg| arg },
145
+ RDF::CLI::Option.new(
146
+ symbol: :processingMode,
147
+ datatype: %w[json-ld-1.0 json-ld-1.1],
148
+ control: :radio,
149
+ on: ["--processingMode MODE", %w[json-ld-1.0 json-ld-1.1]],
150
+ description: "Set Processing Mode (json-ld-1.0 or json-ld-1.1)"
151
+ ),
152
+ RDF::CLI::Option.new(
153
+ symbol: :rdfDirection,
154
+ datatype: %w[i18n-datatype compound-literal],
155
+ default: 'null',
156
+ control: :select,
157
+ on: ["--rdf-direction DIR", %w[i18n-datatype compound-literal]],
158
+ description: "How to serialize literal direction (i18n-datatype compound-literal)"
159
+ ) { |arg| arg },
160
+ RDF::CLI::Option.new(
161
+ symbol: :requireAll,
162
+ datatype: TrueClass,
163
+ default: true,
164
+ control: :checkbox,
165
+ on: ["--[no-]require-all"],
166
+ description: "Require all properties to match (true). Default is `true` use --no-require-all to disable."
167
+ ) { |arg| arg },
168
+ RDF::CLI::Option.new(
169
+ symbol: :stream,
170
+ datatype: TrueClass,
171
+ control: :checkbox,
172
+ on: ["--[no-]stream"],
173
+ description: "Do not attempt to optimize graph presentation, suitable for streaming large graphs."
174
+ ) { |arg| arg },
175
+ RDF::CLI::Option.new(
176
+ symbol: :useNativeTypes,
177
+ datatype: TrueClass,
178
+ control: :checkbox,
179
+ on: ["--[no-]use-native-types"],
180
+ description: "Use native JSON values in value objects."
181
+ ) { |arg| arg },
182
+ RDF::CLI::Option.new(
183
+ symbol: :useRdfType,
184
+ datatype: TrueClass,
185
+ control: :checkbox,
186
+ on: ["--[no-]use-rdf-type"],
187
+ description: "Treat `rdf:type` like a normal property instead of using `@type`."
188
+ ) { |arg| arg }
189
+ ]
194
190
  end
195
191
 
196
- ##
197
- # Returns default context used for compacted profile without an explicit context URL
198
- # @return [String]
199
- def default_context; @default_context || JSON::LD::DEFAULT_CONTEXT; end
192
+ class << self
193
+ attr_reader :white_list, :black_list
200
194
 
201
- ##
202
- # Sets default context used for compacted profile without an explicit context URL
203
- # @param [String] url
204
- def default_context=(url); @default_context = url; end
205
- end
195
+ ##
196
+ # Use parameters from accept-params to determine if the parameters are acceptable to invoke this writer. The `accept_params` will subsequently be provided to the writer instance.
197
+ #
198
+ # @param [Hash{Symbol => String}] accept_params
199
+ # @yield [accept_params] if a block is given, returns the result of evaluating that block
200
+ # @yieldparam [Hash{Symbol => String}] accept_params
201
+ # @return [Boolean]
202
+ # @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
203
+ def accept?(accept_params)
204
+ if block_given?
205
+ yield(accept_params)
206
+ else
207
+ true
208
+ end
209
+ end
206
210
 
207
- ##
208
- # Initializes the JSON-LD writer instance.
209
- #
210
- # @param [IO, File] output
211
- # the output stream
212
- # @param [Hash{Symbol => Object}] options
213
- # any additional options
214
- # @option options [Encoding] :encoding (Encoding::UTF_8)
215
- # the encoding to use on the output stream (Ruby 1.9+)
216
- # @option options [Boolean] :canonicalize (false)
217
- # whether to canonicalize literals when serializing
218
- # @option options [Hash] :prefixes ({})
219
- # the prefix mappings to use (not supported by all writers)
220
- # @option options [Boolean] :standard_prefixes (false)
221
- # Add standard prefixes to @prefixes, if necessary.
222
- # @option options [IO, Array, Hash, String, Context] :context ({})
223
- # context to use when serializing. Constructed context for native serialization.
224
- # @option options [IO, Array, Hash, String, Context] :frame ({})
225
- # frame to use when serializing.
226
- # @option options [Boolean] :unique_bnodes (false)
227
- # Use unique bnode identifiers, defaults to using the identifier which the node was originall initialized with (if any).
228
- # @option options [Proc] serializer (JSON::LD::API.serializer)
229
- # A Serializer method used for generating the JSON serialization of the result.
230
- # @option options [Boolean] :stream (false)
231
- # Do not attempt to optimize graph presentation, suitable for streaming large graphs.
232
- # @yield [writer] `self`
233
- # @yieldparam [RDF::Writer] writer
234
- # @yieldreturn [void]
235
- # @yield [writer]
236
- # @yieldparam [RDF::Writer] writer
237
- def initialize(output = $stdout, **options, &block)
238
- options[:base_uri] ||= options[:base] if options.key?(:base)
239
- options[:base] ||= options[:base_uri] if options.key?(:base_uri)
240
- @serializer = options.fetch(:serializer, JSON::LD::API.method(:serializer))
241
- super do
242
- @repo = RDF::Repository.new
211
+ ##
212
+ # Returns default context used for compacted profile without an explicit context URL
213
+ # @return [String]
214
+ def default_context
215
+ @default_context || JSON::LD::DEFAULT_CONTEXT
216
+ end
217
+
218
+ ##
219
+ # Sets default context used for compacted profile without an explicit context URL
220
+ # @param [String] url
221
+ attr_writer :default_context
222
+ end
243
223
 
244
- if block_given?
245
- case block.arity
224
+ ##
225
+ # Initializes the JSON-LD writer instance.
226
+ #
227
+ # @param [IO, File] output
228
+ # the output stream
229
+ # @param [Hash{Symbol => Object}] options
230
+ # any additional options
231
+ # @option options [Encoding] :encoding (Encoding::UTF_8)
232
+ # the encoding to use on the output stream (Ruby 1.9+)
233
+ # @option options [Boolean] :canonicalize (false)
234
+ # whether to canonicalize literals when serializing
235
+ # @option options [Hash] :prefixes ({})
236
+ # the prefix mappings to use (not supported by all writers)
237
+ # @option options [Boolean] :standard_prefixes (false)
238
+ # Add standard prefixes to @prefixes, if necessary.
239
+ # @option options [IO, Array, Hash, String, Context] :context ({})
240
+ # context to use when serializing. Constructed context for native serialization.
241
+ # @option options [IO, Array, Hash, String, Context] :frame ({})
242
+ # frame to use when serializing.
243
+ # @option options [Boolean] :unique_bnodes (false)
244
+ # Use unique bnode identifiers, defaults to using the identifier which the node was originall initialized with (if any).
245
+ # @option options [Proc] serializer (JSON::LD::API.serializer)
246
+ # A Serializer method used for generating the JSON serialization of the result.
247
+ # @option options [Boolean] :stream (false)
248
+ # Do not attempt to optimize graph presentation, suitable for streaming large graphs.
249
+ # @yield [writer] `self`
250
+ # @yieldparam [RDF::Writer] writer
251
+ # @yieldreturn [void]
252
+ # @yield [writer]
253
+ # @yieldparam [RDF::Writer] writer
254
+ def initialize(output = $stdout, **options, &block)
255
+ options[:base_uri] ||= options[:base] if options.key?(:base)
256
+ options[:base] ||= options[:base_uri] if options.key?(:base_uri)
257
+ @serializer = options.fetch(:serializer, JSON::LD::API.method(:serializer))
258
+ super do
259
+ @repo = RDF::Repository.new
260
+
261
+ if block
262
+ case block.arity
246
263
  when 0 then instance_eval(&block)
247
- else block.call(self)
264
+ else yield(self)
265
+ end
248
266
  end
249
267
  end
250
268
  end
251
- end
252
269
 
253
- ##
254
- # Addes a triple to be serialized
255
- # @param [RDF::Resource] subject
256
- # @param [RDF::URI] predicate
257
- # @param [RDF::Value] object
258
- # @return [void]
259
- # @abstract
260
- def write_triple(subject, predicate, object)
261
- write_quad(subject, predicate, object, nil)
262
- end
270
+ ##
271
+ # Addes a triple to be serialized
272
+ # @param [RDF::Resource] subject
273
+ # @param [RDF::URI] predicate
274
+ # @param [RDF::Value] object
275
+ # @return [void]
276
+ # @abstract
277
+ def write_triple(subject, predicate, object)
278
+ write_quad(subject, predicate, object, nil)
279
+ end
263
280
 
264
- ##
265
- # Outputs the N-Quads representation of a statement.
266
- #
267
- # @param [RDF::Resource] subject
268
- # @param [RDF::URI] predicate
269
- # @param [RDF::Term] object
270
- # @return [void]
271
- def write_quad(subject, predicate, object, graph_name)
272
- statement = RDF::Statement.new(subject, predicate, object, graph_name: graph_name)
273
- if @options[:stream]
274
- stream_statement(statement)
275
- else
276
- @repo.insert(statement)
281
+ ##
282
+ # Outputs the N-Quads representation of a statement.
283
+ #
284
+ # @param [RDF::Resource] subject
285
+ # @param [RDF::URI] predicate
286
+ # @param [RDF::Term] object
287
+ # @return [void]
288
+ def write_quad(subject, predicate, object, graph_name)
289
+ statement = RDF::Statement.new(subject, predicate, object, graph_name: graph_name)
290
+ if @options[:stream]
291
+ stream_statement(statement)
292
+ else
293
+ @repo.insert(statement)
294
+ end
277
295
  end
278
- end
279
296
 
280
- ##
281
- # Necessary for streaming
282
- # @return [void] `self`
283
- def write_prologue
284
- stream_prologue if @options[:stream]
285
- super
286
- end
297
+ ##
298
+ # Necessary for streaming
299
+ # @return [void] `self`
300
+ def write_prologue
301
+ stream_prologue if @options[:stream]
302
+ super
303
+ end
287
304
 
288
- ##
289
- # Outputs the Serialized JSON-LD representation of all stored statements.
290
- #
291
- # If provided a context or prefixes, we'll create a context
292
- # and use it to compact the output. Otherwise, we return un-compacted JSON-LD
293
- #
294
- # @return [void]
295
- # @see #write_triple
296
- def write_epilogue
297
- if @options[:stream]
298
- stream_epilogue
299
- else
305
+ ##
306
+ # Outputs the Serialized JSON-LD representation of all stored statements.
307
+ #
308
+ # If provided a context or prefixes, we'll create a context
309
+ # and use it to compact the output. Otherwise, we return un-compacted JSON-LD
310
+ #
311
+ # @return [void]
312
+ # @see #write_triple
313
+ def write_epilogue
314
+ if @options[:stream]
315
+ stream_epilogue
316
+ else
300
317
 
301
- log_debug("writer") { "serialize #{@repo.count} statements, #{@options.inspect}"}
302
- result = API.fromRdf(@repo, **@options.merge(serializer: nil))
318
+ # log_debug("writer") { "serialize #{@repo.count} statements, #{@options.inspect}"}
319
+ result = API.fromRdf(@repo, **@options.merge(serializer: nil))
303
320
 
304
- # Some options may be indicated from accept parameters
305
- profile = @options.fetch(:accept_params, {}).fetch(:profile, "").split(' ')
306
- links = LinkHeader.parse(@options[:link])
307
- @options[:context] ||= links.find_link(['rel', JSON_LD_NS+"context"]).href rescue nil
308
- @options[:context] ||= Writer.default_context if profile.include?(JSON_LD_NS+"compacted")
309
- @options[:frame] ||= links.find_link(['rel', JSON_LD_NS+"frame"]).href rescue nil
321
+ # Some options may be indicated from accept parameters
322
+ profile = @options.fetch(:accept_params, {}).fetch(:profile, "").split
323
+ links = LinkHeader.parse(@options[:link])
324
+ @options[:context] ||= begin
325
+ links.find_link(['rel', JSON_LD_NS + "context"]).href
326
+ rescue StandardError
327
+ nil
328
+ end
329
+ @options[:context] ||= Writer.default_context if profile.include?(JSON_LD_NS + "compacted")
330
+ @options[:frame] ||= begin
331
+ links.find_link(['rel', JSON_LD_NS + "frame"]).href
332
+ rescue StandardError
333
+ nil
334
+ end
310
335
 
311
- # If we were provided a context, or prefixes, use them to compact the output
312
- context = @options[:context]
313
- context ||= if @options[:prefixes] || @options[:language] || @options[:standard_prefixes]
314
- ctx = Context.new(**@options)
315
- ctx.language = @options[:language] if @options[:language]
316
- @options[:prefixes].each do |prefix, iri|
317
- ctx.set_mapping(prefix, iri) if prefix && iri
318
- end if @options[:prefixes]
319
- ctx
320
- end
336
+ # If we were provided a context, or prefixes, use them to compact the output
337
+ context = @options[:context]
338
+ context ||= if @options[:prefixes] || @options[:language] || @options[:standard_prefixes]
339
+ ctx = Context.new(**@options)
340
+ ctx.language = @options[:language] if @options[:language]
341
+ @options[:prefixes]&.each do |prefix, iri|
342
+ ctx.set_mapping(prefix, iri) if prefix && iri
343
+ end
344
+ ctx
345
+ end
321
346
 
322
- # Rename BNodes to uniquify them, if necessary
323
- if options[:unique_bnodes]
324
- result = API.flatten(result, context, **@options.merge(serializer: nil))
325
- end
347
+ # Rename BNodes to uniquify them, if necessary
348
+ result = API.flatten(result, context, **@options.merge(serializer: nil)) if options[:unique_bnodes]
326
349
 
327
- if frame = @options[:frame]
328
- # Perform framing, if given a frame
329
- log_debug("writer") { "frame result"}
330
- result = API.frame(result, frame, **@options.merge(serializer: nil))
331
- elsif context
332
- # Perform compaction, if we have a context
333
- log_debug("writer") { "compact result"}
334
- result = API.compact(result, context, **@options.merge(serializer: nil))
350
+ if (frame = @options[:frame])
351
+ # Perform framing, if given a frame
352
+ # log_debug("writer") { "frame result"}
353
+ result = API.frame(result, frame, **@options.merge(serializer: nil))
354
+ elsif context
355
+ # Perform compaction, if we have a context
356
+ # log_debug("writer") { "compact result"}
357
+ result = API.compact(result, context, **@options.merge(serializer: nil))
358
+ end
359
+
360
+ @output.write(@serializer.call(result, **@options))
335
361
  end
336
362
 
337
- @output.write(@serializer.call(result, **@options))
363
+ super
338
364
  end
339
-
340
- super
341
365
  end
342
366
  end
343
367
  end
344
-