@@ -86,16 +86,15 @@ class API
86
86
# @yield [api]
87
87
# @yieldparam [API]
88
88
# @raise [JsonLdError]
89
- def initialize ( input , context , options = { } , &block )
89
+ def initialize ( input , context , rename_bnodes : true , unique_bnodes : false , ** options , &block )
90
90
@options = {
91
91
compactArrays : true ,
92
- rename_bnodes : true ,
93
92
documentLoader : self . class . method ( :documentLoader )
94
93
} . merge ( options )
95
- @namer = options [ : unique_bnodes] ? BlankNodeUniqer . new : ( @options [ : rename_bnodes] ? BlankNodeNamer . new ( "b" ) : BlankNodeMapper . new )
94
+ @namer = unique_bnodes ? BlankNodeUniqer . new : ( rename_bnodes ? BlankNodeNamer . new ( "b" ) : BlankNodeMapper . new )
96
95
97
96
# For context via Link header
98
- remote_base , context_ref = nil , nil
97
+ _ , context_ref = nil , nil
99
98
100
99
@value = case input
101
100
when Array , Hash then input . dup
@@ -115,7 +114,6 @@ def initialize(input, context, options = {}, &block)
115
114
when String
116
115
remote_doc = @options [ :documentLoader ] . call ( input , @options )
117
116
118
- remote_base = remote_doc . documentUrl
119
117
context_ref = remote_doc . contextUrl
120
118
@options = { base : remote_doc . documentUrl } . merge ( @options ) unless @options [ :no_default_base ]
121
119
@@ -166,12 +164,12 @@ def initialize(input, context, options = {}, &block)
166
164
# @return [Object, Array<Hash>]
167
165
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
168
166
# @see http://json-ld.org/spec/latest/json-ld-api/#expansion-algorithm
169
- def self . expand ( input , options = { } , &block )
167
+ def self . expand ( input , ordered : true , framing : false , ** options , &block )
170
168
result , doc_base = nil
171
169
API . new ( input , options [ :expandContext ] , options ) do
172
170
result = self . expand ( self . value , nil , self . context ,
173
- ordered : options . fetch ( : ordered, true ) ,
174
- framing : @options [ :processingMode ] . to_s . include? ( 'expand-frame' ) )
171
+ ordered : ordered ,
172
+ framing : framing )
175
173
doc_base = @options [ :base ]
176
174
end
177
175
@@ -215,18 +213,18 @@ def self.expand(input, options = {}, &block)
215
213
# If a block is given, the result of evaluating the block is returned, otherwise, the compacted JSON-LD document
216
214
# @raise [JsonLdError]
217
215
# @see http://json-ld.org/spec/latest/json-ld-api/#compaction-algorithm
218
- def self . compact ( input , context , options = { } )
216
+ def self . compact ( input , context , expanded : false , ** options )
219
217
result = nil
220
218
options = { compactToRelative : true } . merge ( options )
221
219
222
220
# 1) Perform the Expansion Algorithm on the JSON-LD input.
223
221
# This removes any existing context to allow the given context to be cleanly applied.
224
- expanded_input = options [ : expanded] ? input : API . expand ( input , options ) do |result , base_iri |
222
+ expanded_input = expanded ? input : API . expand ( input , options ) do |res , base_iri |
225
223
options [ :base ] ||= base_iri if options [ :compactToRelative ]
226
- result
224
+ res
227
225
end
228
226
229
- API . new ( expanded_input , context , options . merge ( no_default_base : true ) ) do
227
+ API . new ( expanded_input , context , no_default_base : true , ** options ) do
230
228
log_debug ( ".compact" ) { "expanded input: #{ expanded_input . to_json ( JSON_STATE ) rescue 'malformed json' } " }
231
229
result = compact ( value )
232
230
@@ -260,18 +258,18 @@ def self.compact(input, context, options = {})
260
258
# @return [Object, Hash]
261
259
# If a block is given, the result of evaluating the block is returned, otherwise, the flattened JSON-LD document
262
260
# @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm
263
- def self . flatten ( input , context , options = { } )
261
+ def self . flatten ( input , context , expanded : false , ** options )
264
262
flattened = [ ]
265
263
options = { compactToRelative : true } . merge ( options )
266
264
267
265
# Expand input to simplify processing
268
- expanded_input = options [ : expanded] ? input : API . expand ( input , options ) do |result , base_iri |
266
+ expanded_input = expanded ? input : API . expand ( input , options ) do |result , base_iri |
269
267
options [ :base ] ||= base_iri if options [ :compactToRelative ]
270
268
result
271
269
end
272
270
273
271
# Initialize input using
274
- API . new ( expanded_input , context , options . merge ( no_default_base : true ) ) do
272
+ API . new ( expanded_input , context , no_default_base : true , ** options ) do
275
273
log_debug ( ".flatten" ) { "expanded input: #{ value . to_json ( JSON_STATE ) rescue 'malformed json' } " }
276
274
277
275
# Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object.
@@ -333,7 +331,7 @@ def self.flatten(input, context, options = {})
333
331
# If a block is given, the result of evaluating the block is returned, otherwise, the framed JSON-LD document
334
332
# @raise [InvalidFrame]
335
333
# @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm
336
- def self . frame ( input , frame , options = { } )
334
+ def self . frame ( input , frame , expanded : false , ** options )
337
335
result = nil
338
336
options = {
339
337
base : ( input if input . is_a? ( String ) ) ,
@@ -367,16 +365,16 @@ def self.frame(input, frame, options = {})
367
365
end
368
366
369
367
# Expand input to simplify processing
370
- expanded_input = options [ : expanded] ? input : API . expand ( input , options ) do |result , base_iri |
368
+ expanded_input = expanded ? input : API . expand ( input , options ) do |res , base_iri |
371
369
options [ :base ] ||= base_iri if options [ :compactToRelative ]
372
- result
370
+ res
373
371
end
374
372
375
373
# Expand frame to simplify processing
376
- expanded_frame = API . expand ( frame , options . merge ( processingMode : "json-ld-1.1-expand-frame" ) )
374
+ expanded_frame = API . expand ( frame , framing : true , ** options )
377
375
378
376
# Initialize input using frame as context
379
- API . new ( expanded_input , nil , options . merge ( no_default_base : true ) ) do
377
+ API . new ( expanded_input , nil , no_default_base : true , ** options ) do
380
378
log_debug ( ".frame" ) { "expanded input: #{ expanded_input . to_json ( JSON_STATE ) rescue 'malformed json' } " }
381
379
log_debug ( ".frame" ) { "expanded frame: #{ expanded_frame . to_json ( JSON_STATE ) rescue 'malformed json' } " }
382
380
@@ -397,7 +395,7 @@ def self.frame(input, frame, options = {})
397
395
framing_state [ :subjects ] = framing_state [ :graphMap ] [ framing_state [ :graph ] ]
398
396
399
397
result = [ ]
400
- frame ( framing_state , framing_state [ :subjects ] . keys . sort , ( expanded_frame . first || { } ) , options . merge ( parent : result ) )
398
+ frame ( framing_state , framing_state [ :subjects ] . keys . sort , ( expanded_frame . first || { } ) , parent : result , ** options )
401
399
402
400
# Count blank node identifiers used in the document, if pruning
403
401
bnodes_to_clear = if options [ :pruneBlankNodeIdentifiers ]
@@ -433,7 +431,7 @@ def self.frame(input, frame, options = {})
433
431
# @yield statement
434
432
# @yieldparam [RDF::Statement] statement
435
433
# @return [RDF::Enumerable] set of statements, unless a block is given.
436
- def self . toRdf ( input , options = { } , &block )
434
+ def self . toRdf ( input , expanded : false , ** options , &block )
437
435
unless block_given?
438
436
results = [ ]
439
437
results . extend ( RDF ::Enumerable )
@@ -444,7 +442,7 @@ def self.toRdf(input, options = {}, &block)
444
442
end
445
443
446
444
# Expand input to simplify processing
447
- expanded_input = options [ : expanded] ? input : API . expand ( input , options . merge ( ordered : false ) )
445
+ expanded_input = expanded ? input : API . expand ( input , ordered : false , ** options )
448
446
449
447
API . new ( expanded_input , nil , options ) do
450
448
# 1) Perform the Expansion Algorithm on the JSON-LD input.
@@ -495,9 +493,7 @@ def self.toRdf(input, options = {}, &block)
495
493
# @yieldreturn [Object] returned object
496
494
# @return [Object, Hash]
497
495
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
498
- def self . fromRdf ( input , options = { } , &block )
499
- useRdfType = options . fetch ( :useRdfType , false )
500
- useNativeTypes = options . fetch ( :useNativeTypes , false )
496
+ def self . fromRdf ( input , useRdfType : false , useNativeTypes : false , **options , &block )
501
497
result = nil
502
498
503
499
API . new ( nil , nil , options ) do |api |
@@ -519,12 +515,12 @@ def self.fromRdf(input, options = {}, &block)
519
515
# @return [Object, RemoteDocument]
520
516
# If a block is given, the result of evaluating the block is returned, otherwise, the retrieved remote document and context information unless block given
521
517
# @raise [JsonLdError]
522
- def self . documentLoader ( url , options = { } )
518
+ def self . documentLoader ( url , validate : false , ** options )
523
519
options = OPEN_OPTS . merge ( options )
524
520
RDF ::Util ::File . open_file ( url , options ) do |remote_doc |
525
521
content_type = remote_doc . content_type if remote_doc . respond_to? ( :content_type )
526
522
# If the passed input is a DOMString representing the IRI of a remote document, dereference it. If the retrieved document's content type is neither application/json, nor application/ld+json, nor any other media type using a +json suffix as defined in [RFC6839], reject the promise passing an loading document failed error.
527
- if content_type && options [ : validate]
523
+ if content_type && validate
528
524
main , sub = content_type . split ( "/" )
529
525
raise JSON ::LD ::JsonLdError ::LoadingDocumentFailed , "url: #{ url } , content_type: #{ content_type } " if
530
526
main != 'application' ||
0 commit comments