Class: YAML_LD::API

Inherits:
JSON::LD::API show all
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb

Overview

A YAML-LD processor based on JSON-LD.

Constant Summary collapse

%w(rel http://www.w3.org/ns/yaml-ld#context).freeze
%w(rel alternate).freeze
%w(type application/ld+yaml).freeze

Constants inherited from JSON::LD::API

JSON::LD::API::JSON_LD_PROCESSING_MODES, JSON::LD::API::OPEN_OPTS, JSON::LD::API::SCRIPT_LOADERS

Constants included from RDF::Util::Logger

RDF::Util::Logger::IOWrapper

Constants included from JSON::LD::Compact

JSON::LD::Compact::CONTAINER_MAPPING_LANGUAGE_INDEX_ID_TYPE, JSON::LD::Compact::EXPANDED_PROPERTY_DIRECTION_INDEX_LANGUAGE_VALUE

Constants included from JSON::LD::Expand

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

Instance Attribute Summary

Attributes inherited from JSON::LD::API

#context, #input, #namer, #value

Class Method Summary collapse

Methods inherited from JSON::LD::API

add_script_loader, loadRemoteDocument, load_html, validate_input

Methods included from RDF::Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Methods included from JSON::LD::Frame

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

Methods included from JSON::LD::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 JSON::LD::FromRDF

#from_statements

Methods included from JSON::LD::Flatten

#create_annotations, #create_node_map, #rename_bnodes

Methods included from JSON::LD::ToRDF

#item_to_rdf, #node, #parse_list

Methods included from JSON::LD::Compact

#compact

Methods included from JSON::LD::Expand

#expand

Class Method Details

.compact(input, context, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld| ... } ⇒ String

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 YAML-LD object to copy and perform the expansion upon.

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

    The base context to use when compacting the input.

  • serializer (Proc) (defaults to: self.method(:serializer))

    (nil) A Serializer method used for generating the YAML serialization of the result. If absent, the internal Ruby objects are returned, which can be transformed to YAML externally via #to_yaml. See serializer.

  • 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[:base] 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.

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

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

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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. When given a YAML stream, extracts all documents in that stream into an array.

  • :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.

  • :lowercaseLanguage (Boolean)

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

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

  • :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).

  • :validate (Boolean)

    Validate input, if a string or readable object.

Yields:

  • YAML_LD

Yield Parameters:

  • yamlld (Array<Hash>)

    The expanded YAML-LD document

Yield Returns:

  • (String)

    returned YAML serialization

Returns:

  • (String)

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

Raises:

  • (JsonLdError)

See Also:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb', line 113

def self.compact(input, context, expanded: false,
                 documentLoader: self.method(:documentLoader),
                 serializer: self.method(:serializer),
                 **options,
                 &block)
  JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader))
  JSON::LD::API.compact(input, context, expanded: expanded,
                       allowed_content_types: %r(application/(.+\+)?yaml),
                       documentLoader: documentLoader, 
                       extendedRepresentation: options[:extendedYAML],
                       processingMode: 'json-ld-1.1',
                       serializer: serializer,
                       **options,
                       &block)
end

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

Default document loader for YAML_LD.

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. When given a YAML stream, extracts all documents in that stream into an array.

  • 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)


300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb', line 300

def self.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **options, &block)
  if url.respond_to?(:read)
    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 &&
      # Any JSON type other than ld+json
      (content_type == 'application/json' || content_type.match?(%r(application/(^ld)+json)))
      link = url.links.find_link(JSON::LD::API::LINK_REL_CONTEXT)
      link.href if link
    elsif  url.respond_to?(:links) && url.links &&
      # Any YAML type
      content_type.match?(%r(application/(\w+\+)*yaml))
      link = url.links.find_link(LINK_REL_CONTEXT)
      link.href if link
    end

    content = case content_type
    when nil, %r(application/(\w+\+)*yaml)
      # Parse YAML
      if extractAllScripts
        Representation.load_stream(url.read, filename: url.to_s, **options)
      else
        Representation.load(url.read, filename: url.to_s, **options)
      end
    else
      url.read
    end
    block.call(RemoteDocument.new(content,
      documentUrl: base_uri,
      contentType: content_type,
      contextUrl: context_url))
  elsif url.to_s.match?(/\.yaml\w*$/) || content_type.to_s.match?(%r(application/(\w+\+)*yaml))
    # Parse YAML
    doc = RDF::Util::File.open_file(url.to_s)
    base_uri ||= doc.base_uri
    content = if extractAllScripts
      Representation.load_stream(doc.read, filename: url.to_s, **options)
    else
      Representation.load(doc.read, filename: url.to_s, **options)
    end

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

.expand(input, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld, RDF::URI| ... } ⇒ String

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 YAML-LD object to copy and perform the expansion upon.

  • documentLoader (Proc) (defaults to: self.method(:documentLoader))

    The callback of the loader to be used to retrieve remote documents and contexts, and to parse IO objects. 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. The remote document returned must be parsed if it is YAML.

  • serializer (Proc) (defaults to: self.method(:serializer))

    (nil) A Serializer method used for generating the YAML serialization of the result. If absent, the internal Ruby objects are returned, which can be transformed to YAML externally via #to_yaml. See serializer.

  • 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[:base] 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.

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

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

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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. When given a YAML stream, extracts all documents in that stream into an array.

  • :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.

  • :lowercaseLanguage (Boolean)

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

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

  • :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).

  • :validate (Boolean)

    Validate input, if a string or readable object.

Yields:

  • YAML_LD, base_iri

Yield Parameters:

  • yamlld (Array<Hash>)

    The expanded YAML-LD document

  • RDF::URI (RDF::URI The document base as determined during expansion)

    The document base as determined during expansion

Yield Returns:

  • (String)

    returned YAML serialization

Returns:

  • (String)

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

Raises:

  • (JsonLdError)

See Also:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb', line 72

def self.expand(input,
                documentLoader: self.method(:documentLoader),
                serializer: self.method(:serializer),
                **options,
                &block)
  JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader))
  JSON::LD::API.expand(input,
                       allowed_content_types: %r(application/(.+\+)?yaml),
                       documentLoader: documentLoader, 
                       extendedRepresentation: options[:extendedYAML],
                       processingMode: 'json-ld-1.1',
                       serializer: serializer,
                       **options,
                       &block)
end

.flatten(input, context, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld| ... } ⇒ Object, Hash

This algorithm flattens an expanded YAML-LD document by collecting all properties of a node in a single 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 YAML-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 YAML-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

  • serializer (Proc) (defaults to: self.method(:serializer))

    (nil) A Serializer method used for generating the YAML serialization of the result. If absent, the internal Ruby objects are returned, which can be transformed to YAML externally via #to_yaml. See serializer.

  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :createAnnotations (Boolean)

    Unfold embedded nodes which can be represented using @annotation.

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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[:base] 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.

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

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

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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. When given a YAML stream, extracts all documents in that stream into an array.

  • :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.

  • :lowercaseLanguage (Boolean)

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

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

  • :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).

  • :validate (Boolean)

    Validate input, if a string or readable object.

Yields:

  • YAML_LD

Yield Parameters:

  • yamlld (Array<Hash>)

    The expanded YAML-LD document

Yield Returns:

  • (String)

    returned YAML serialization

Returns:

  • (Object, Hash)

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

See Also:



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb', line 155

def self.flatten(input, context, expanded: false,
                 documentLoader: self.method(:documentLoader),
                 serializer: self.method(:serializer),
                 **options,
                 &block)
  JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader))
  JSON::LD::API.flatten(input, context, expanded: expanded,
                        allowed_content_types: %r(application/(.+\+)?yaml),
                        documentLoader: documentLoader, 
                        extendedRepresentation: options[:extendedYAML],
                        processingMode: 'json-ld-1.1',
                        serializer: serializer,
                        **options,
                        &block)
end

.frame(input, frame, expanded: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|yamlld| ... } ⇒ 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 YAML-LD object or array of YAML-LD objects to flatten or an IRI referencing the JSON-LD document to flatten.

  • 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.

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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[:base] 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.

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

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

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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. When given a YAML stream, extracts all documents in that stream into an array.

  • :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.

  • :lowercaseLanguage (Boolean)

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

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

  • :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).

  • :validate (Boolean)

    Validate input, if a string or readable object.

Yields:

  • YAML_LD

Yield Parameters:

  • yamlld (Array<Hash>)

    The expanded YAML-LD document

Yield Returns:

  • (String)

    returned YAML serialization

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:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb', line 202

def self.frame(input, frame, expanded: false,
               documentLoader: self.method(:documentLoader),
               serializer: self.method(:serializer),
               **options,
               &block)
  JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader))
  JSON::LD::API.frame(input, frame, expanded: expanded,
                      allowed_content_types: %r(application/(.+\+)?yaml),
                      documentLoader: documentLoader, 
                      extendedRepresentation: options[:extendedYAML],
                      processingMode: 'json-ld-1.1',
                      serializer: serializer,
                      **options,
                      &block)
end

.fromRdf(input, useRdfType: false, useNativeTypes: false, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **options) {|jsonld| ... } ⇒ Object, Hash

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)
  • useRdfType (Boolean) (defaults to: false)

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

  • useNativeTypes (Boolean) (defaults to: false)

    (false) use native representations

  • serializer (Proc) (defaults to: self.method(:serializer))

    (nil) A Serializer method used for generating the YAML serialization of the result. If absent, the internal Ruby objects are returned, which can be transformed to YAML externally via #to_yaml. See serializer.

  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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[:base] 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.

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

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

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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. When given a YAML stream, extracts all documents in that stream into an array.

  • :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.

  • :lowercaseLanguage (Boolean)

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

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

  • :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).

  • :validate (Boolean)

    Validate input, if a string or readable object.

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



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb', line 269

def self.fromRdf(input, useRdfType: false, useNativeTypes: false,
                 documentLoader: self.method(:documentLoader),
                 serializer: self.method(:serializer),
                 **options,
                 &block)
  JSON::LD::API.fromRdf(input,
                        allowed_content_types: %r(application/(.+\+)?yaml),
                        documentLoader: documentLoader, 
                        extendedRepresentation: options[:extendedYAML],
                        processingMode: 'json-ld-1.1',
                        serializer: serializer,
                        useRdfType: useRdfType,
                        useNativeTypes: useNativeTypes,
                        **options,
                        &block)
end

.htmlLoader(content, url:, extractAllScripts: false, **options) ⇒ Object

Extracts a single YAML script, or a stream of YAML scripts from HTML script tags.



354
355
356
357
358
359
360
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb', line 354

def self.htmlLoader(content, url:, extractAllScripts: false, **options)
  if extractAllScripts
    Representation.load_stream(content.unindent, filename: url.to_s, **options)
  else
    Representation.load(content, filename: url.to_s, **options)
  end
end

.serializer(object, *args, **options) ⇒ Object

The default serializer for serialzing Ruby Objects to JSON.

Defaults to MultiJson.dump

Parameters:

  • object (Object)
  • args (Array<Object>)

    other arguments that may be passed for some specific implementation.

  • options (Hash<Symbol, Object>)

    options passed from the invoking context.



372
373
374
375
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb', line 372

def self.serializer(object, *args, **options)
  # de-alias any objects to avoid the use of aliases and anchors
  "%YAML 1.2\n" + Representation.dump(object, **options)
end

.toRdf(input, expanded: false, documentLoader: self.method(:documentLoader), **options) {|statement| ... } ⇒ RDF::Enumerable

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

Parameters:

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

    The YAML-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):

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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[:base] 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.

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

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

  • :extendedYAML (Boolean) — default: false

    Use the expanded internal representation.

  • :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. When given a YAML stream, extracts all documents in that stream into an array.

  • :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.

  • :lowercaseLanguage (Boolean)

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

  • :ordered (Boolean) — default: true

    Order traversal of dictionary members by key when performing algorithms.

  • :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).

  • :validate (Boolean)

    Validate input, if a string or readable object.

Yields:

  • statement

Yield Parameters:

Returns:

Raises:

  • (JsonLdError)


233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/yaml-ld-2d2ba8e32d66/lib/yaml_ld/api.rb', line 233

def self.toRdf(input, expanded: false,
               documentLoader: self.method(:documentLoader),
               **options,
               &block)
  JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader))
  JSON::LD::API.toRdf(input, expanded: expanded,
                      allowed_content_types: %r(application/(.+\+)?yaml),
                      documentLoader: documentLoader, 
                      extendedRepresentation: options[:extendedYAML],
                      processingMode: 'json-ld-1.1',
                      **options,
                      &block)
end