Class: JSON::LD::API

Inherits:
Object
  • Object
show all
Includes:
Compact, Expand, Flatten, Frame, FromRDF, ToRDF, RDF::Util::Logger
Defined in:
lib/json/ld/api.rb,
lib/json/ld/html/rexml.rb,
lib/json/ld/html/nokogiri.rb

Overview

A JSON-LD processor based on the JsonLdProcessor interface.

This API provides a clean mechanism that enables developers to convert JSON-LD data into a a variety of output formats that are easier to work with in various programming languages. If a JSON-LD API is provided in a programming environment, the entirety of the following API must be implemented.

Note that the API method signatures are somewhat different than what is specified, as the use of Futures and explicit callback parameters is not as relevant for Ruby-based interfaces.

Defined Under Namespace

Modules: Nokogiri, REXML Classes: RemoteDocument

Constant Summary collapse

OPEN_OPTS =

Options used for open_file

{
  headers: {"Accept" => "application/ld+json, text/html;q=0.8, application/xhtml+xml;q=0.8, application/json;q=0.5"}
}
%w(rel http://www.w3.org/ns/json-ld#context).freeze
%w(rel alternate).freeze
%w(type application/ld+json).freeze
JSON_LD_PROCESSING_MODES =
%w(json-ld-1.0 json-ld-1.1).freeze

Constants included from Compact

Compact::CONTAINER_MAPPING_LANGUAGE_INDEX_ID_TYPE, Compact::EXPANDED_PROPERTY_DIRECTION_INDEX_LANGUAGE_VALUE

Constants included from Expand

Expand::CONTAINER_INDEX_ID_TYPE, Expand::KEYS_INCLUDED_TYPE_REVERSE, Expand::KEYS_SET_LIST_INDEX, Expand::KEYS_VALUE_LANGUAGE_TYPE_INDEX_DIRECTION, Expand::KEY_ID

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from Frame

#cleanup_null, #cleanup_preserve, #count_blank_node_identifiers, #count_blank_node_identifiers_internal, #frame, #prune_bnodes, #remove_dependents

Methods included from Utils

#add_value, #as_array, #as_resource, #blank_node?, #compare_values, #graph?, #has_value?, #index?, #list?, #node?, #node_or_ref?, #node_reference?, #property?, #simple_graph?, #value?

Methods included from FromRDF

#from_statements

Methods included from Flatten

#create_annotations, #create_node_map, #rename_bnodes

Methods included from ToRDF

#item_to_rdf, #node, #parse_list

Methods included from Compact

#compact

Methods included from Expand

#expand

Instance Attribute Details

#contextJSON::LD::Context

Input evaluation context

Returns:



55
56
57
# File 'lib/json/ld/api.rb', line 55

def context
  @context
end

#inputString, ...

Current input

Returns:

  • (String, #read, Hash, Array)


50
# File 'lib/json/ld/api.rb', line 50

attr_accessor :value

#namerJSON::LD::BlankNodeNamer (readonly)

Current Blank Node Namer



60
61
62
# File 'lib/json/ld/api.rb', line 60

def namer
  @namer
end

#valueString, ...

Current input

Returns:

  • (String, #read, Hash, Array)


50
51
52
# File 'lib/json/ld/api.rb', line 50

def value
  @value
end

Class Method Details

.compact(input, context, expanded: false, **options) {|jsonld| ... } ⇒ Object, Hash

Compacts the given input according to the steps in the Compaction Algorithm. The input must be copied, compacted and returned if there are no errors. If the compaction fails, an appropirate exception must be thrown.

If no context is provided, the input document is compacted using the top-level context of the document

The resulting ‘Hash` is either returned or yielded, if a block is given.

Parameters:

  • input (String, #read, Hash, Array)

    The JSON-LD object to copy and perform the compaction upon.

  • context (String, #read, Hash, Array, JSON::LD::Context)

    The base context to use when compacting the input.

  • expanded (Boolean) (defaults to: false)

    (false) Input is already expanded

  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :base (RDF::URI, String, #to_s)

    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 will be modified with this value.

  • :compactArrays (Boolean) — default: true

    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.

  • :compactToRelative (Boolean) — default: true

    Creates document relative IRIs when compacting, if ‘true`, otherwise leaves expanded.

  • :documentLoader (Proc)

    The callback of the loader to be used to retrieve remote documents and contexts. If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor’s built-in loader must be used. See documentLoader for the method signature.

  • :lowercaseLanguage (Boolean)

    By default, language tags are left as is. To normalize to lowercase, set this option to ‘true`.

  • :expandContext (String, #read, Hash, Array, JSON::LD::Context)

    A context that is used to initialize the active context when expanding a document.

  • :extractAllScripts (Boolean)

    If set, when given an HTML input without a fragment identifier, extracts all ‘script` elements with type `application/ld+json` into an array during expansion.

  • :flatten (Boolean, String, RDF::URI)

    If set to a value that is not ‘false`, the JSON-LD processor must modify the output of the Compaction Algorithm or the Expansion Algorithm by coalescing all properties associated with each subject via the Flattening Algorithm. The value of `flatten must` be either an IRI value representing the name of the graph to flatten, or `true`. If the value is `true`, then the first graph encountered in the input document is selected and flattened.

  • :language (String)

    When set, this has the effect of inserting a context definition with ‘@language` set to the associated value, creating a default language for interpreting string values.

  • :library (Symbol)

    One of :nokogiri or :rexml. If nil/unspecified uses :nokogiri if available, :rexml otherwise.

  • :processingMode (String)

    Processing mode, json-ld-1.0 or json-ld-1.1. If ‘processingMode` is not specified, a mode of `json-ld-1.0` or `json-ld-1.1` is set, the context used for `expansion` or `compaction`.

  • rdfstar (Boolean) — default: false

    support parsing JSON-LD-star statement resources.

  • :rename_bnodes (Boolean) — default: true

    Rename bnodes as part of expansion, or keep them the same.

  • :unique_bnodes (Boolean) — default: false

    Use unique bnode identifiers, defaults to using the identifier which the node was originally initialized with (if any).

  • :adapter (Symbol)

    used with MultiJson

  • :validate (Boolean)

    Validate input, if a string or readable object.

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

Yields:

  • jsonld

Yield Parameters:

  • jsonld (Hash)

    The compacted JSON-LD document

Yield Returns:

  • (Object)

    returned object

Returns:

  • (Object, Hash)

    If a block is given, the result of evaluating the block is returned, otherwise, the compacted JSON-LD document

Raises:

See Also:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/json/ld/api.rb', line 218

def self.compact(input, context, expanded: false, **options)
  result = nil
  options = {compactToRelative:  true}.merge(options)

  # 1) Perform the Expansion Algorithm on the JSON-LD input.
  #    This removes any existing context to allow the given context to be cleanly applied.
  expanded_input = expanded ? input : API.expand(input, ordered: false, **options) do |res, base_iri|
    options[:base] ||= RDF::URI(base_iri) if base_iri && options[:compactToRelative]
    res
  end

  API.new(expanded_input, context, no_default_base: true, **options) do
    log_debug(".compact") {"expanded input: #{expanded_input.to_json(JSON_STATE) rescue 'malformed json'}"}
    result = compact(value)

    # xxx) Add the given context to the output
    ctx = self.context.serialize(provided_context: context)
    if result.is_a?(Array)
      kwgraph = self.context.compact_iri('@graph', vocab: true)
      result = result.empty? ? {} : {kwgraph => result}
    end
    result = ctx.merge(result) unless ctx.fetch('@context', {}).empty?
  end
  block_given? ? yield(result) : result
end

.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **options) {|remote_document| ... } ⇒ Object

Default document loader.

Parameters:

  • url (RDF::URI, String)
  • extractAllScripts (Boolean) (defaults to: false)

    If set to ‘true`, when extracting JSON-LD script elements from HTML, unless a specific fragment identifier is targeted, extracts all encountered JSON-LD script elements using an array form, if necessary.

  • profile (String) (defaults to: nil)

    When the resulting ‘contentType` is `text/html` or `application/xhtml+xml`, this option determines the profile to use for selecting a JSON-LD script elements.

  • requestProfile (String) (defaults to: nil)

    One or more IRIs to use in the request as a profile parameter.

  • options (Hash<Symbol => Object>)

Yields:

  • remote_document

Yield Parameters:

Raises:

  • (IOError)


678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'lib/json/ld/api.rb', line 678

def self.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **options, &block)
  case url
  when IO, StringIO
    base_uri = options[:base]
    base_uri ||= url.base_uri if url.respond_to?(:base_uri)
    content_type = options[:content_type]
    content_type ||= url.content_type if url.respond_to?(:content_type)
    context_url = if url.respond_to?(:links) && url.links
     (content_type == 'appliaction/json' || content_type.match?(%r(application/(^ld)+json)))
      link = url.links.find_link(LINK_REL_CONTEXT)
      link.href if link
    end

    block.call(RemoteDocument.new(url.read,
      documentUrl: base_uri,
      contentType: content_type,
      contextUrl: context_url))
  else
    RDF::Util::File.open_file(url, **options, &block)
  end
end

.expand(input, framing: false, **options) {|jsonld, base_iri| ... } ⇒ Object, Array<Hash>

Expands the given input according to the steps in the Expansion Algorithm. The input must be copied, expanded and returned if there are no errors. If the expansion fails, an appropriate exception must be thrown.

The resulting ‘Array` either returned or yielded

Parameters:

  • input (String, #read, Hash, Array)

    The JSON-LD object to copy and perform the expansion upon.

  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :base (RDF::URI, String, #to_s)

    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 will be modified with this value.

  • :compactArrays (Boolean) — default: true

    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.

  • :compactToRelative (Boolean) — default: true

    Creates document relative IRIs when compacting, if ‘true`, otherwise leaves expanded.

  • :documentLoader (Proc)

    The callback of the loader to be used to retrieve remote documents and contexts. If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor’s built-in loader must be used. See documentLoader for the method signature.

  • :lowercaseLanguage (Boolean)

    By default, language tags are left as is. To normalize to lowercase, set this option to ‘true`.

  • :expandContext (String, #read, Hash, Array, JSON::LD::Context)

    A context that is used to initialize the active context when expanding a document.

  • :extractAllScripts (Boolean)

    If set, when given an HTML input without a fragment identifier, extracts all ‘script` elements with type `application/ld+json` into an array during expansion.

  • :flatten (Boolean, String, RDF::URI)

    If set to a value that is not ‘false`, the JSON-LD processor must modify the output of the Compaction Algorithm or the Expansion Algorithm by coalescing all properties associated with each subject via the Flattening Algorithm. The value of `flatten must` be either an IRI value representing the name of the graph to flatten, or `true`. If the value is `true`, then the first graph encountered in the input document is selected and flattened.

  • :language (String)

    When set, this has the effect of inserting a context definition with ‘@language` set to the associated value, creating a default language for interpreting string values.

  • :library (Symbol)

    One of :nokogiri or :rexml. If nil/unspecified uses :nokogiri if available, :rexml otherwise.

  • :processingMode (String)

    Processing mode, json-ld-1.0 or json-ld-1.1. If ‘processingMode` is not specified, a mode of `json-ld-1.0` or `json-ld-1.1` is set, the context used for `expansion` or `compaction`.

  • rdfstar (Boolean) — default: false

    support parsing JSON-LD-star statement resources.

  • :rename_bnodes (Boolean) — default: true

    Rename bnodes as part of expansion, or keep them the same.

  • :unique_bnodes (Boolean) — default: false

    Use unique bnode identifiers, defaults to using the identifier which the node was originally initialized with (if any).

  • :adapter (Symbol)

    used with MultiJson

  • :validate (Boolean)

    Validate input, if a string or readable object.

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

Yields:

  • jsonld, base_iri

Yield Parameters:

  • jsonld (Array<Hash>)

    The expanded JSON-LD document

  • base_iri (RDF::URI)

    The document base as determined during expansion

Yield Returns:

  • (Object)

    returned object

Returns:

  • (Object, Array<Hash>)

    If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document

Raises:

See Also:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/json/ld/api.rb', line 170

def self.expand(input, framing: false, **options, &block)
  result = doc_base = nil
  API.new(input, options[:expandContext], **options) do
    result = self.expand(self.value, nil, self.context,
      framing: framing)
    doc_base = @options[:base]
  end

  # If, after the algorithm outlined above is run, the resulting element is an JSON object with just a @graph property, element is set to the value of @graph's value.
  result = result['@graph'] if result.is_a?(Hash) && result.length == 1 && result.key?('@graph')

  # Finally, if element is a JSON object, it is wrapped into an array.
  result = [result].compact unless result.is_a?(Array)

  if block_given?
    case block.arity
    when 1 then yield(result)
    when 2 then yield(result, doc_base)
    else
      raise "Unexpected number of yield parameters to expand"
    end
  else
    result
  end
end

.flatten(input, context, expanded: false, **options) {|jsonld| ... } ⇒ Object, Hash

This algorithm flattens an expanded JSON-LD document by collecting all properties of a node in a single JSON object and labeling all blank nodes with blank node identifiers. This resulting uniform shape of the document, may drastically simplify the code required to process JSON-LD data in certain applications.

The resulting ‘Array` is either returned, or yielded if a block is given.

Parameters:

  • input (String, #read, Hash, Array)

    The JSON-LD object or array of JSON-LD objects to flatten or an IRI referencing the JSON-LD document to flatten.

  • context (String, #read, Hash, Array, JSON::LD::EvaluationContext)

    An optional external context to use additionally to the context embedded in input when expanding the input.

  • expanded (Boolean) (defaults to: false)

    (false) Input is already expanded

  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :createAnnotations (Boolean)

    Unfold embedded nodes which can be represented using ‘@annotation`.

  • :base (RDF::URI, String, #to_s)

    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 will be modified with this value.

  • :compactArrays (Boolean) — default: true

    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.

  • :compactToRelative (Boolean) — default: true

    Creates document relative IRIs when compacting, if ‘true`, otherwise leaves expanded.

  • :documentLoader (Proc)

    The callback of the loader to be used to retrieve remote documents and contexts. If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor’s built-in loader must be used. See documentLoader for the method signature.

  • :lowercaseLanguage (Boolean)

    By default, language tags are left as is. To normalize to lowercase, set this option to ‘true`.

  • :expandContext (String, #read, Hash, Array, JSON::LD::Context)

    A context that is used to initialize the active context when expanding a document.

  • :extractAllScripts (Boolean)

    If set, when given an HTML input without a fragment identifier, extracts all ‘script` elements with type `application/ld+json` into an array during expansion.

  • :flatten (Boolean, String, RDF::URI)

    If set to a value that is not ‘false`, the JSON-LD processor must modify the output of the Compaction Algorithm or the Expansion Algorithm by coalescing all properties associated with each subject via the Flattening Algorithm. The value of `flatten must` be either an IRI value representing the name of the graph to flatten, or `true`. If the value is `true`, then the first graph encountered in the input document is selected and flattened.

  • :language (String)

    When set, this has the effect of inserting a context definition with ‘@language` set to the associated value, creating a default language for interpreting string values.

  • :library (Symbol)

    One of :nokogiri or :rexml. If nil/unspecified uses :nokogiri if available, :rexml otherwise.

  • :processingMode (String)

    Processing mode, json-ld-1.0 or json-ld-1.1. If ‘processingMode` is not specified, a mode of `json-ld-1.0` or `json-ld-1.1` is set, the context used for `expansion` or `compaction`.

  • rdfstar (Boolean) — default: false

    support parsing JSON-LD-star statement resources.

  • :rename_bnodes (Boolean) — default: true

    Rename bnodes as part of expansion, or keep them the same.

  • :unique_bnodes (Boolean) — default: false

    Use unique bnode identifiers, defaults to using the identifier which the node was originally initialized with (if any).

  • :adapter (Symbol)

    used with MultiJson

  • :validate (Boolean)

    Validate input, if a string or readable object.

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

Yields:

  • jsonld

Yield Parameters:

  • jsonld (Hash)

    The flattened JSON-LD document

Yield Returns:

  • (Object)

    returned object

Returns:

  • (Object, Hash)

    If a block is given, the result of evaluating the block is returned, otherwise, the flattened JSON-LD document

See Also:



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/json/ld/api.rb', line 265

def self.flatten(input, context, expanded: false, **options)
  flattened = []
  options = {
    compactToRelative:  true,
    extractAllScripts:  true,
  }.merge(options)

  # Expand input to simplify processing
  expanded_input = expanded ? input : API.expand(input, **options) do |result, base_iri|
    options[:base] ||= RDF::URI(base_iri) if base_iri && options[:compactToRelative]
    result
  end

  # Initialize input using
  API.new(expanded_input, context, no_default_base: true, **options) do
    log_debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE) rescue 'malformed json'}"}

    # Rename blank nodes recusively. Note that this does not create new blank node identifiers where none exist, which is performed in the node map generation algorithm.
    @value = rename_bnodes(@value) if @options[:rename_bnodes]

    # Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object.
    graph_maps = {'@default' => {}}
    create_node_map(value, graph_maps)

    # If create annotations flag is set, then update each node map in graph maps with the result of calling the create annotations algorithm.
    if options[:createAnnotations]
      graph_maps.values.each do |node_map|
        create_annotations(node_map)
      end
    end

    default_graph = graph_maps['@default']
    graph_maps.keys.opt_sort(ordered: @options[:ordered]).each do |graph_name|
      next if graph_name == '@default'

      graph = graph_maps[graph_name]
      entry = default_graph[graph_name] ||= {'@id' => graph_name}
      nodes = entry['@graph'] ||= []
      graph.keys.opt_sort(ordered: @options[:ordered]).each do |id|
        nodes << graph[id] unless node_reference?(graph[id])
      end
    end
    default_graph.keys.opt_sort(ordered: @options[:ordered]).each do |id|
      flattened << default_graph[id] unless node_reference?(default_graph[id])
    end

    if context && !flattened.empty?
      # 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.
      compacted = as_array(compact(flattened))
      kwgraph = self.context.compact_iri('@graph', vocab: true)
      flattened = self.context.
        serialize(provided_context: context).
        merge(kwgraph => compacted)
    end
  end

  block_given? ? yield(flattened) : flattened
end

.frame(input, frame, expanded: false, **options) {|jsonld| ... } ⇒ Object, Hash

Frames the given input using the frame according to the steps in the Framing Algorithm. The input is used to build the framed output and is returned if there are no errors. If there are no matches for the frame, null must be returned. Exceptions must be thrown if there are errors.

The resulting ‘Array` is either returned, or yielded if a block is given.

Parameters:

  • input (String, #read, Hash, Array)

    The JSON-LD object to copy and perform the framing on.

  • frame (String, #read, Hash, Array)

    The frame to use when re-arranging the data.

  • expanded (Boolean) (defaults to: false)

    (false) Input is already expanded

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :embed ('@always', '@link', '@once', '@never') — default: '@once'

    a flag specifying that objects should be directly embedded in the output, instead of being referred to by their IRI.

  • :explicit (Boolean) — default: false

    a flag specifying that for properties to be included in the output, they must be explicitly declared in the framing context.

  • :requireAll (Boolean) — default: false

    A flag specifying that all properties present in the input frame must either have a default value or be present in the JSON-LD input for the frame to match.

  • :omitDefault (Boolean) — default: false

    a flag specifying that properties that are missing from the JSON-LD input should be omitted from the output.

  • :pruneBlankNodeIdentifiers (Boolean) — default: true

    removes blank node identifiers that are only used once.

  • :omitGraph (Boolean)

    does not use ‘@graph` at top level unless necessary to describe multiple objects, defaults to `true` if processingMode is 1.1, otherwise `false`.

  • :base (RDF::URI, String, #to_s)

    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 will be modified with this value.

  • :compactArrays (Boolean) — default: true

    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.

  • :compactToRelative (Boolean) — default: true

    Creates document relative IRIs when compacting, if ‘true`, otherwise leaves expanded.

  • :documentLoader (Proc)

    The callback of the loader to be used to retrieve remote documents and contexts. If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor’s built-in loader must be used. See documentLoader for the method signature.

  • :lowercaseLanguage (Boolean)

    By default, language tags are left as is. To normalize to lowercase, set this option to ‘true`.

  • :expandContext (String, #read, Hash, Array, JSON::LD::Context)

    A context that is used to initialize the active context when expanding a document.

  • :extractAllScripts (Boolean)

    If set, when given an HTML input without a fragment identifier, extracts all ‘script` elements with type `application/ld+json` into an array during expansion.

  • :flatten (Boolean, String, RDF::URI)

    If set to a value that is not ‘false`, the JSON-LD processor must modify the output of the Compaction Algorithm or the Expansion Algorithm by coalescing all properties associated with each subject via the Flattening Algorithm. The value of `flatten must` be either an IRI value representing the name of the graph to flatten, or `true`. If the value is `true`, then the first graph encountered in the input document is selected and flattened.

  • :language (String)

    When set, this has the effect of inserting a context definition with ‘@language` set to the associated value, creating a default language for interpreting string values.

  • :library (Symbol)

    One of :nokogiri or :rexml. If nil/unspecified uses :nokogiri if available, :rexml otherwise.

  • :processingMode (String)

    Processing mode, json-ld-1.0 or json-ld-1.1. If ‘processingMode` is not specified, a mode of `json-ld-1.0` or `json-ld-1.1` is set, the context used for `expansion` or `compaction`.

  • rdfstar (Boolean) — default: false

    support parsing JSON-LD-star statement resources.

  • :rename_bnodes (Boolean) — default: true

    Rename bnodes as part of expansion, or keep them the same.

  • :unique_bnodes (Boolean) — default: false

    Use unique bnode identifiers, defaults to using the identifier which the node was originally initialized with (if any).

  • :adapter (Symbol)

    used with MultiJson

  • :validate (Boolean)

    Validate input, if a string or readable object.

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

Yields:

  • jsonld

Yield Parameters:

  • jsonld (Hash)

    The framed JSON-LD document

Yield Returns:

  • (Object)

    returned object

Returns:

  • (Object, Hash)

    If a block is given, the result of evaluating the block is returned, otherwise, the framed JSON-LD document

Raises:

  • (InvalidFrame)

See Also:



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/json/ld/api.rb', line 353

def self.frame(input, frame, expanded: false, **options)
  result = nil
  options = {
    base:                       (RDF::URI(input) if input.is_a?(String)),
    compactArrays:              true,
    compactToRelative:          true,
    embed:                      '@once',
    explicit:                   false,
    requireAll:                 false,
    omitDefault:                false,
  }.merge(options)

  framing_state = {
    graphMap:     {},
    graphStack:   [],
    subjectStack: [],
    link:         {},
    embedded:     false # False at the top-level
  }

  # de-reference frame to create the framing object
  frame = case frame
  when Hash then frame.dup
  when IO, StringIO, String
    remote_doc = loadRemoteDocument(frame,
                                    profile: 'http://www.w3.org/ns/json-ld#frame',
                                    requestProfile: 'http://www.w3.org/ns/json-ld#frame',
                                    **options)
    if remote_doc.document.is_a?(String)
      MultiJson.load(remote_doc.document)
    else
      remote_doc.document
    end
  end

  # Expand input to simplify processing
  expanded_input = expanded ? input : API.expand(input, ordered: false, **options) do |res, base_iri|
    options[:base] ||= RDF::URI(base_iri) if base_iri && options[:compactToRelative]
    res
  end

  # Expand frame to simplify processing
  expanded_frame = API.expand(frame, framing: true, ordered: false, **options)

  # Initialize input using frame as context
  API.new(expanded_input, frame['@context'], no_default_base: true, **options) do
    log_debug(".frame") {"expanded input: #{expanded_input.to_json(JSON_STATE) rescue 'malformed json'}"}
    log_debug(".frame") {"expanded frame: #{expanded_frame.to_json(JSON_STATE) rescue 'malformed json'}"}

    if %w(@first @last).include?(options[:embed]) && context.processingMode('json-ld-1.1')
      raise JSON::LD::JsonLdError::InvalidEmbedValue, "#{options[:embed]} is not a valid value of @embed in 1.1 mode" if @options[:validate]
      warn "[DEPRECATION] #{options[:embed]}  is not a valid value of @embed in 1.1 mode.\n"
    end

    # Set omitGraph option, if not present, based on processingMode
    unless options.key?(:omitGraph)
      options[:omitGraph] = context.processingMode('json-ld-1.1')
    end

    # Rename blank nodes recusively. Note that this does not create new blank node identifiers where none exist, which is performed in the node map generation algorithm.
    @value = rename_bnodes(@value)

    # Get framing nodes from expanded input, replacing Blank Node identifiers as necessary
    create_node_map(value, framing_state[:graphMap], active_graph: '@default')

    frame_keys = frame.keys.map {|k| context.expand_iri(k, vocab: true)}
    if frame_keys.include?('@graph')
      # If frame contains @graph, it matches the default graph.
      framing_state[:graph] = '@default'
    else
      # If frame does not contain @graph used the merged graph.
      framing_state[:graph] = '@merged'
      framing_state[:link]['@merged'] = {}
      framing_state[:graphMap]['@merged'] = merge_node_map_graphs(framing_state[:graphMap])
    end

    framing_state[:subjects] = framing_state[:graphMap][framing_state[:graph]]

    result = []
    frame(framing_state, framing_state[:subjects].keys.opt_sort(ordered: @options[:ordered]), (expanded_frame.first || {}), parent: result, **options)

    # Default to based on processinMode
    if !options.key?(:pruneBlankNodeIdentifiers)
      options[:pruneBlankNodeIdentifiers] = context.processingMode('json-ld-1.1')
    end

    # Count blank node identifiers used in the document, if pruning
    if options[:pruneBlankNodeIdentifiers]
      bnodes_to_clear = count_blank_node_identifiers(result).collect {|k, v| k if v == 1}.compact
      result = prune_bnodes(result, bnodes_to_clear)
    end

    # Replace values with `@preserve` with the content of its entry.
    result = cleanup_preserve(result)
    log_debug(".frame") {"expanded result: #{result.to_json(JSON_STATE) rescue 'malformed json'}"}

    # Compact result
    compacted = compact(result)

    # @replace `@null` with nil, compacting arrays
    compacted = cleanup_null(compacted)
    compacted = [compacted] unless options[:omitGraph] || compacted.is_a?(Array)

    # Add the given context to the output
    result = if !compacted.is_a?(Array)
      compacted
    else
      kwgraph = context.compact_iri('@graph', vocab: true)
      {kwgraph => compacted}
    end
    # Only add context if one was provided
    result = context.serialize(provided_context: frame).merge(result) if frame['@context']
    
    log_debug(".frame") {"after compact: #{result.to_json(JSON_STATE) rescue 'malformed json'}"}
    result
  end

  block_given? ? yield(result) : result
end

.fromRdf(input, useRdfType: false, useNativeTypes: false, **options) {|jsonld| ... } ⇒ Object, Hash Also known as: fromRDF

Take an ordered list of RDF::Statements and turn them into a JSON-LD document.

The resulting ‘Array` is either returned or yielded, if a block is given.

Parameters:

  • input (RDF::Enumerable)
  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :useRdfType (Boolean) — default: false

    If set to ‘true`, the JSON-LD processor will treat `rdf:type` like a normal property instead of using `@type`.

  • :useNativeTypes (Boolean) — default: false

    use native representations

  • :base (RDF::URI, String, #to_s)

    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 will be modified with this value.

  • :compactArrays (Boolean) — default: true

    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.

  • :compactToRelative (Boolean) — default: true

    Creates document relative IRIs when compacting, if ‘true`, otherwise leaves expanded.

  • :documentLoader (Proc)

    The callback of the loader to be used to retrieve remote documents and contexts. If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor’s built-in loader must be used. See documentLoader for the method signature.

  • :lowercaseLanguage (Boolean)

    By default, language tags are left as is. To normalize to lowercase, set this option to ‘true`.

  • :expandContext (String, #read, Hash, Array, JSON::LD::Context)

    A context that is used to initialize the active context when expanding a document.

  • :extractAllScripts (Boolean)

    If set, when given an HTML input without a fragment identifier, extracts all ‘script` elements with type `application/ld+json` into an array during expansion.

  • :flatten (Boolean, String, RDF::URI)

    If set to a value that is not ‘false`, the JSON-LD processor must modify the output of the Compaction Algorithm or the Expansion Algorithm by coalescing all properties associated with each subject via the Flattening Algorithm. The value of `flatten must` be either an IRI value representing the name of the graph to flatten, or `true`. If the value is `true`, then the first graph encountered in the input document is selected and flattened.

  • :language (String)

    When set, this has the effect of inserting a context definition with ‘@language` set to the associated value, creating a default language for interpreting string values.

  • :library (Symbol)

    One of :nokogiri or :rexml. If nil/unspecified uses :nokogiri if available, :rexml otherwise.

  • :processingMode (String)

    Processing mode, json-ld-1.0 or json-ld-1.1. If ‘processingMode` is not specified, a mode of `json-ld-1.0` or `json-ld-1.1` is set, the context used for `expansion` or `compaction`.

  • rdfstar (Boolean) — default: false

    support parsing JSON-LD-star statement resources.

  • :rename_bnodes (Boolean) — default: true

    Rename bnodes as part of expansion, or keep them the same.

  • :unique_bnodes (Boolean) — default: false

    Use unique bnode identifiers, defaults to using the identifier which the node was originally initialized with (if any).

  • :adapter (Symbol)

    used with MultiJson

  • :validate (Boolean)

    Validate input, if a string or readable object.

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

Yields:

  • jsonld

Yield Parameters:

  • jsonld (Hash)

    The JSON-LD document in expanded form

Yield Returns:

  • (Object)

    returned object

Returns:

  • (Object, Hash)

    If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document



542
543
544
545
546
547
548
549
550
551
552
# File 'lib/json/ld/api.rb', line 542

def self.fromRdf(input, useRdfType: false, useNativeTypes: false, **options, &block)
  result = nil

  API.new(nil, nil, **options) do
    result = from_statements(input,
      useRdfType: useRdfType,
      useNativeTypes: useNativeTypes)
  end

  block_given? ? yield(result) : result
end

.load_html(input, url:, library: nil, extractAllScripts: false, profile: nil, **options) ⇒ Object

Load one or more script tags from an HTML source. Unescapes and uncomments input, returns the internal representation Yields document base

Parameters:

  • input (String)
  • url (String)

    Original URL

  • library (:nokogiri, :rexml) (defaults to: nil)

    (nil)

  • extractAllScripts (Boolean) (defaults to: false)

    (false)

  • profile (Boolean) (defaults to: nil)

    (nil) Optional priortized profile when loading a single script by type.

  • options (Hash{Symbol => Object})


716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/json/ld/api.rb', line 716

def self.load_html(input, url:,
                     library: nil,
                     extractAllScripts: false,
                     profile: nil,
                     **options)

  if input.is_a?(String)
    library ||= begin
      require 'nokogiri'
      :nokogiri
    rescue LoadError
      :rexml
    end
    require "json/ld/html/#{library}"

    # Parse HTML using the appropriate library
    implementation = case library
    when :nokogiri then Nokogiri
    when :rexml then REXML
    end
    self.extend(implementation)

    input = begin
      self.send("initialize_html_#{library}".to_sym, input, **options)
    rescue
      raise JSON::LD::JsonLdError::LoadingDocumentFailed, "Malformed HTML document: #{$!.message}"
    end

    # Potentially update options[:base]
    if html_base = input.at_xpath("/html/head/base/@href")
      base = RDF::URI(url) if url
      html_base = RDF::URI(html_base)
      html_base = base.join(html_base) if base
      yield html_base
    end
  end

  url = RDF::URI.parse(url)
  if url.fragment
    id = CGI.unescape(url.fragment)
    # Find script with an ID based on that fragment.
    element = input.at_xpath("//script[@id='#{id}']")
    raise JSON::LD::JsonLdError::LoadingDocumentFailed, "No script tag found with id=#{id}" unless element
    raise JSON::LD::JsonLdError::LoadingDocumentFailed, "Script tag has type=#{element.attributes['type']}" unless element.attributes['type'].to_s.start_with?('application/ld+json')
    content = element.inner_html
    validate_input(content, url: url) if options[:validate]
    MultiJson.load(content, **options)
  elsif extractAllScripts
    res = []
    elements = if profile
      es = input.xpath("//script[starts-with(@type, 'application/ld+json;profile=#{profile}')]")
      # If no profile script, just take a single script without profile
      es = [input.at_xpath("//script[starts-with(@type, 'application/ld+json')]")].compact if es.empty?
      es
    else
      input.xpath("//script[starts-with(@type, 'application/ld+json')]")
    end
    elements.each do |element|
      content = element.inner_html
      validate_input(content, url: url) if options[:validate]
      r = MultiJson.load(content, **options)
      if r.is_a?(Hash)
        res << r
      elsif r.is_a?(Array)
        res = res.concat(r)
      end
    end
    res
  else
    # Find the first script with type application/ld+json.
    element = input.at_xpath("//script[starts-with(@type, 'application/ld+json;profile=#{profile}')]") if profile
    element ||= input.at_xpath("//script[starts-with(@type, 'application/ld+json')]")
    raise JSON::LD::JsonLdError::LoadingDocumentFailed, "No script tag found" unless element
    content = element.inner_html
    validate_input(content, url: url) if options[:validate]
    MultiJson.load(content, **options)
  end
rescue MultiJson::ParseError => e
  raise JSON::LD::JsonLdError::InvalidScriptElement, e.message
end

.loadRemoteDocument(url, base: nil, documentLoader: nil, extractAllScripts: false, profile: nil, requestProfile: nil, validate: false, **options) {|remote_document| ... } ⇒ Object, RemoteDocument

Uses built-in or provided documentLoader to retrieve a parsed document.

Parameters:

  • url (RDF::URI, String)
  • base (String, RDF::URI) (defaults to: nil)

    Location to use as documentUrl instead of ‘url`.

  • extractAllScripts (Boolean) (defaults to: false)

    If set to ‘true`, when extracting JSON-LD script elements from HTML, unless a specific fragment identifier is targeted, extracts all encountered JSON-LD script elements using an array form, if necessary.

  • profile (String) (defaults to: nil)

    When the resulting ‘contentType` is `text/html` or `application/xhtml+xml`, this option determines the profile to use for selecting a JSON-LD script elements.

  • requestProfile (String) (defaults to: nil)

    One or more IRIs to use in the request as a profile parameter.

  • validate (Boolean) (defaults to: false)

    (false) Allow only appropriate content types

  • options (Hash<Symbol => Object>)

Options Hash (**options):

  • :documentLoader (Proc)

    The callback of the loader to be used to retrieve remote documents and contexts.

Yields:

  • remote_document

Yield Parameters:

  • remote_document (RemoteDocumentRemoteDocument, RDF::Util::File::RemoteDocument)

Yield Returns:

  • (Object)

    returned object

Returns:

  • (Object, RemoteDocument)

    If a block is given, the result of evaluating the block is returned, otherwise, the retrieved remote document and context information unless block given

Raises:



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/json/ld/api.rb', line 577

def self.loadRemoteDocument(url,
                            base: nil,
                            documentLoader: nil,
                            extractAllScripts: false,
                            profile: nil,
                            requestProfile: nil,
                            validate: false,
                            **options)
  documentLoader ||= self.method(:documentLoader)
  options = OPEN_OPTS.merge(options)
  if requestProfile
    # Add any request profile
    options[:headers]['Accept'] = options[:headers]['Accept'].sub('application/ld+json,', "application/ld+json;profile=#{requestProfile}, application/ld+json;q=0.9,")
  end
  documentLoader.call(url, **options) do |remote_doc|
    case remote_doc
    when RDF::Util::File::RemoteDocument
      # Convert to RemoteDocument
      context_url = if remote_doc.content_type != 'application/ld+json' &&
                       (remote_doc.content_type == 'application/json' ||
                        remote_doc.content_type.to_s.match?(%r(application/\w+\+json)))
        # Get context link(s)
        # Note, we can't simply use #find_link, as we need to detect multiple
        links = remote_doc.links.links.select do |link|
          link.attr_pairs.include?(LINK_REL_CONTEXT)
        end
        raise JSON::LD::JsonLdError::MultipleContextLinkHeaders,
          "expected at most 1 Link header with rel=jsonld:context, got #{links.length}" if links.length > 1
        Array(links.first).first
      end

      # If content-type is not application/ld+json, nor any other +json and a link with rel=alternate and type='application/ld+json' is found, use that instead
      alternate = !remote_doc.content_type.match?(%r(application/(\w*\+)?json)) && remote_doc.links.links.detect do |link|
        link.attr_pairs.include?(LINK_REL_ALTERNATE) &&
        link.attr_pairs.include?(LINK_TYPE_JSONLD)
      end

      remote_doc = if alternate
        # Load alternate relative to URL
        loadRemoteDocument(RDF::URI(url).join(alternate.href),
            extractAllScripts: extractAllScripts,
            profile: profile,
            requestProfile: requestProfile,
            validate: validate,
            base: base,
            **options)
      else
        RemoteDocument.new(remote_doc.read,
          documentUrl: remote_doc.base_uri,
          contentType: remote_doc.content_type,
          contextUrl: context_url)
      end
    when RemoteDocument
      # Pass through
    else
      raise JSON::LD::JsonLdError::LoadingDocumentFailed, "unknown result from documentLoader: #{remote_doc.class}"
    end

    # Use specified document location
    remote_doc.documentUrl = base if base

    # Parse any HTML
    if remote_doc.document.is_a?(String)
      remote_doc.document = case remote_doc.contentType
      when 'text/html', 'application/xhtml+xml'
        load_html(remote_doc.document,
                  url: remote_doc.documentUrl,
                  extractAllScripts: extractAllScripts,
                  profile: profile,
                  **options) do |base|
          remote_doc.documentUrl = base
        end
      else
        validate_input(remote_doc.document, url: remote_doc.documentUrl) if validate
        MultiJson.load(remote_doc.document, **options)
      end
    end

    if remote_doc.contentType && validate
      raise IOError, "url: #{url}, contentType: #{remote_doc.contentType}" unless
        remote_doc.contentType.match?(/application\/(.+\+)?json|text\/html|application\/xhtml\+xml/)
    end
    block_given? ? yield(remote_doc) : remote_doc
  end
rescue IOError, MultiJson::ParseError => e
  raise JSON::LD::JsonLdError::LoadingDocumentFailed, e.message
end

.toRdf(input, expanded: false, **options) {|statement| ... } ⇒ RDF::Enumerable Also known as: toRDF

Processes the input according to the RDF Conversion Algorithm, calling the provided callback for each triple generated.

Parameters:

  • input (String, #read, Hash, Array)

    The JSON-LD object to process when outputting statements.

  • expanded (Boolean) (defaults to: false)

    (false) Input is already expanded

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :produceGeneralizedRdf (Boolean) — default: false

    If true, output will include statements having blank node predicates, otherwise they are dropped.

  • :base (RDF::URI, String, #to_s)

    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 will be modified with this value.

  • :compactArrays (Boolean) — default: true

    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.

  • :compactToRelative (Boolean) — default: true

    Creates document relative IRIs when compacting, if ‘true`, otherwise leaves expanded.

  • :documentLoader (Proc)

    The callback of the loader to be used to retrieve remote documents and contexts. If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor’s built-in loader must be used. See documentLoader for the method signature.

  • :lowercaseLanguage (Boolean)

    By default, language tags are left as is. To normalize to lowercase, set this option to ‘true`.

  • :expandContext (String, #read, Hash, Array, JSON::LD::Context)

    A context that is used to initialize the active context when expanding a document.

  • :extractAllScripts (Boolean)

    If set, when given an HTML input without a fragment identifier, extracts all ‘script` elements with type `application/ld+json` into an array during expansion.

  • :flatten (Boolean, String, RDF::URI)

    If set to a value that is not ‘false`, the JSON-LD processor must modify the output of the Compaction Algorithm or the Expansion Algorithm by coalescing all properties associated with each subject via the Flattening Algorithm. The value of `flatten must` be either an IRI value representing the name of the graph to flatten, or `true`. If the value is `true`, then the first graph encountered in the input document is selected and flattened.

  • :language (String)

    When set, this has the effect of inserting a context definition with ‘@language` set to the associated value, creating a default language for interpreting string values.

  • :library (Symbol)

    One of :nokogiri or :rexml. If nil/unspecified uses :nokogiri if available, :rexml otherwise.

  • :processingMode (String)

    Processing mode, json-ld-1.0 or json-ld-1.1. If ‘processingMode` is not specified, a mode of `json-ld-1.0` or `json-ld-1.1` is set, the context used for `expansion` or `compaction`.

  • rdfstar (Boolean) — default: false

    support parsing JSON-LD-star statement resources.

  • :rename_bnodes (Boolean) — default: true

    Rename bnodes as part of expansion, or keep them the same.

  • :unique_bnodes (Boolean) — default: false

    Use unique bnode identifiers, defaults to using the identifier which the node was originally initialized with (if any).

  • :adapter (Symbol)

    used with MultiJson

  • :validate (Boolean)

    Validate input, if a string or readable object.

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

Yields:

  • statement

Yield Parameters:

Returns:

  • (RDF::Enumerable)

    set of statements, unless a block is given.

Raises:



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/json/ld/api.rb', line 486

def self.toRdf(input, expanded: false, **options, &block)
  unless block_given?
    results = []
    results.extend(RDF::Enumerable)
    self.toRdf(input, expanded: expanded, **options) do |stmt|
      results << stmt
    end
    return results
  end

  options = {
    extractAllScripts:  true,
  }.merge(options)

  # Flatten input to simplify processing
  flattened_input = API.flatten(input, nil, expanded: expanded, ordered: false, **options)

  API.new(flattened_input, nil, **options) do
    # 1) Perform the Expansion Algorithm on the JSON-LD input.
    #    This removes any existing context to allow the given context to be cleanly applied.
    log_debug(".toRdf") {"flattened input: #{flattened_input.to_json(JSON_STATE) rescue 'malformed json'}"}

    # Recurse through input
    flattened_input.each do |node|
      item_to_rdf(node) do |statement|
        next if statement.predicate.node? && !options[:produceGeneralizedRdf]

        # Drop invalid statements (other than IRIs)
        unless statement.valid_extended?
          log_debug(".toRdf") {"drop invalid statement: #{statement.to_nquads}"}
          next
        end

        yield statement
      end
    end
  end
end