Skip to content

Commit 30fb242

Browse files
committed
Don't pass base as parameter, take from @options[:base].
1 parent cfb418d commit 30fb242

File tree

4 files changed

+45
-69
lines changed

4 files changed

+45
-69
lines changed

lib/json/ld/api.rb

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class API
6666
# @param [String, #read, Hash, Array, JSON::LD::Context] context
6767
# An external context to use additionally to the context embedded in input when expanding the input.
6868
# @param [Hash{Symbol => Object}] options
69-
# @option options [String, #to_s] :base
69+
# @option options [RDF::URI, String, #to_s] :base
7070
# The Base IRI to use when expanding the document. This overrides the value of `input` if it is a _IRI_. If not specified and `input` is not an _IRI_, the base IRI defaults to the current document IRI if in a browser context, or the empty string if there is no document context. If not specified, and a base IRI is found from `input`, options[:base] will be modified with this value.
7171
# @option options [Boolean] :compactArrays (true)
7272
# If set to `true`, the JSON-LD processor replaces arrays with just one element with that element during compaction. If set to `false`, all arrays will remain arrays even if they have just one element.
@@ -108,6 +108,7 @@ def initialize(input, context, rename_bnodes: true, unique_bnodes: false, **opti
108108
}.merge(options)
109109
@namer = unique_bnodes ? BlankNodeUniqer.new : (rename_bnodes ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
110110

111+
@options[:base] = RDF::URI(@options[:base]) if @options[:base] && !@options[:base].is_a?(RDF::URI)
111112
# For context via Link header
112113
_, context_ref = nil, nil
113114

@@ -117,7 +118,7 @@ def initialize(input, context, rename_bnodes: true, unique_bnodes: false, **opti
117118
remote_doc = self.class.loadRemoteDocument(input, **@options)
118119

119120
context_ref = remote_doc.contextUrl
120-
@options[:base] = remote_doc.documentUrl if remote_doc.documentUrl && !@options[:no_default_base]
121+
@options[:base] = RDF::URI(remote_doc.documentUrl) if remote_doc.documentUrl && !@options[:no_default_base]
121122

122123
case remote_doc.document
123124
when String
@@ -166,7 +167,6 @@ def self.expand(input, framing: false, **options, &block)
166167
result = doc_base = nil
167168
API.new(input, options[:expandContext], **options) do
168169
result = self.expand(self.value, nil, self.context,
169-
base: (RDF::URI(@options[:base]) if @options[:base]),
170170
framing: framing)
171171
doc_base = @options[:base]
172172
end
@@ -218,14 +218,13 @@ def self.compact(input, context, expanded: false, **options)
218218
# 1) Perform the Expansion Algorithm on the JSON-LD input.
219219
# This removes any existing context to allow the given context to be cleanly applied.
220220
expanded_input = expanded ? input : API.expand(input, ordered: false, **options) do |res, base_iri|
221-
options[:base] ||= base_iri if options[:compactToRelative]
221+
options[:base] ||= RDF::URI(base_iri) if base_iri && options[:compactToRelative]
222222
res
223223
end
224224

225225
API.new(expanded_input, context, no_default_base: true, **options) do
226226
log_debug(".compact") {"expanded input: #{expanded_input.to_json(JSON_STATE) rescue 'malformed json'}"}
227-
result = compact(value,
228-
base: (RDF::URI(@options[:base]) if @options[:base]))
227+
result = compact(value)
229228

230229
# xxx) Add the given context to the output
231230
ctx = self.context.serialize(provided_context: context)
@@ -266,7 +265,7 @@ def self.flatten(input, context, expanded: false, **options)
266265

267266
# Expand input to simplify processing
268267
expanded_input = expanded ? input : API.expand(input, **options) do |result, base_iri|
269-
options[:base] ||= base_iri if options[:compactToRelative]
268+
options[:base] ||= RDF::URI(base_iri) if base_iri && options[:compactToRelative]
270269
result
271270
end
272271

@@ -295,9 +294,7 @@ def self.flatten(input, context, expanded: false, **options)
295294

296295
if context && !flattened.empty?
297296
# Otherwise, return the result of compacting flattened according the Compaction algorithm passing context ensuring that the compaction result uses the @graph keyword (or its alias) at the top-level, even if the context is empty or if there is only one element to put in the @graph array. This ensures that the returned document has a deterministic structure.
298-
compacted = as_array(
299-
compact(flattened,
300-
base: (RDF::URI(options[:base]) if options[:base])))
297+
compacted = as_array(compact(flattened))
301298
kwgraph = self.context.compact_iri('@graph')
302299
flattened = self.context.
303300
serialize(provided_context: context).
@@ -374,7 +371,7 @@ def self.frame(input, frame, expanded: false, **options)
374371

375372
# Expand input to simplify processing
376373
expanded_input = expanded ? input : API.expand(input, ordered: false, **options) do |res, base_iri|
377-
options[:base] ||= base_iri if options[:compactToRelative]
374+
options[:base] ||= RDF::URI(base_iri) if base_iri && options[:compactToRelative]
378375
res
379376
end
380377

@@ -431,8 +428,7 @@ def self.frame(input, frame, expanded: false, **options)
431428
log_debug(".frame") {"expanded result: #{result.to_json(JSON_STATE) rescue 'malformed json'}"}
432429

433430
# Compact result
434-
compacted = compact(result,
435-
base: (RDF::URI(options[:base]) if options[:base]))
431+
compacted = compact(result)
436432

437433
# @replace `@null` with nil, compacting arrays
438434
compacted = cleanup_null(compacted)
@@ -529,8 +525,6 @@ def self.fromRdf(input, useRdfType: false, useNativeTypes: false, **options, &bl
529525

530526
API.new(nil, nil, **options) do
531527
result = from_statements(input,
532-
base: (RDF::URI(options[:base]) if options[:base]),
533-
ordered: @options[:ordered],
534528
useRdfType: useRdfType,
535529
useNativeTypes: useNativeTypes)
536530
end

lib/json/ld/compact.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def compact(element,
6666
end
6767

6868
if element.key?('@id') || element.key?('@value')
69-
result = context.compact_value(property, element, base: base)
69+
result = context.compact_value(property, element, base: @options[:base])
7070
if !result.is_a?(Hash) || context.coerce(property) == '@json'
7171
#log_debug("") {"=> scalar result: #{result.inspect}"}
7272
return result
@@ -98,7 +98,7 @@ def compact(element,
9898

9999
if expanded_property == '@id'
100100
compacted_value = Array(expanded_value).map do |expanded_id|
101-
context.compact_iri(expanded_id, base: base)
101+
context.compact_iri(expanded_id, base: @options[:base])
102102
end
103103

104104
kw_alias = context.compact_iri('@id', vocab: true)
@@ -239,7 +239,7 @@ def compact(element,
239239
map_object = nest_result[item_active_property] ||= {}
240240
# If there is no @id, create a blank node identifier to use as an index
241241
map_key = if container.include?('@id') && expanded_item['@id']
242-
context.compact_iri(expanded_item['@id'], base: base)
242+
context.compact_iri(expanded_item['@id'], base: @options[:base])
243243
elsif container.include?('@index') && expanded_item['@index']
244244
context.compact_iri(expanded_item['@index'], vocab: true)
245245
else

0 commit comments

Comments
 (0)