Class: YAML_LD::API
- Inherits:
-
JSON::LD::API
- Object
- JSON::LD::API
- YAML_LD::API
- 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
- LINK_REL_CONTEXT =
The following constants are used to reduce object allocations
%w(rel http://www.w3.org/ns/yaml-ld#context).freeze
- LINK_REL_ALTERNATE =
%w(rel alternate).freeze
- LINK_TYPE_JSONLD =
%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
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
-
.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.
-
.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **options) {|remote_document| ... } ⇒ Object
Default document loader for YAML_LD.
-
.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.
-
.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.
-
.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.
-
.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.
-
.htmlLoader(content, url:, extractAllScripts: false, **options) ⇒ Object
Extracts a single YAML script, or a stream of YAML scripts from HTML script tags.
-
.serializer(object, *args, **options) ⇒ Object
The default serializer for serialzing Ruby Objects to JSON.
-
.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.
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
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
Methods included from JSON::LD::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.
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), **, &block) JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader)) JSON::LD::API.compact(input, context, expanded: , allowed_content_types: %r(application/(.+\+)?yaml), documentLoader: documentLoader, extendedRepresentation: [:extendedYAML], processingMode: 'json-ld-1.1', serializer: serializer, **, &block) end |
.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **options) {|remote_document| ... } ⇒ Object
Default document loader for YAML_LD.
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, **, &block) if url.respond_to?(:read) base_uri = [:base] base_uri ||= url.base_uri if url.respond_to?(:base_uri) content_type = [: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, **) else Representation.load(url.read, filename: url.to_s, **) 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, **) else Representation.load(doc.read, filename: url.to_s, **) end block.call(RemoteDocument.new(content, documentUrl: base_uri, contentType: content_type, contextUrl: context_url)) else RDF::Util::File.open_file(url, **, &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
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.(input, documentLoader: self.method(:documentLoader), serializer: self.method(:serializer), **, &block) JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader)) JSON::LD::API.(input, allowed_content_types: %r(application/(.+\+)?yaml), documentLoader: documentLoader, extendedRepresentation: [:extendedYAML], processingMode: 'json-ld-1.1', serializer: serializer, **, &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.
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), **, &block) JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader)) JSON::LD::API.flatten(input, context, expanded: , allowed_content_types: %r(application/(.+\+)?yaml), documentLoader: documentLoader, extendedRepresentation: [:extendedYAML], processingMode: 'json-ld-1.1', serializer: serializer, **, &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.
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), **, &block) JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader)) JSON::LD::API.frame(input, frame, expanded: , allowed_content_types: %r(application/(.+\+)?yaml), documentLoader: documentLoader, extendedRepresentation: [:extendedYAML], processingMode: 'json-ld-1.1', serializer: serializer, **, &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.
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), **, &block) JSON::LD::API.fromRdf(input, allowed_content_types: %r(application/(.+\+)?yaml), documentLoader: documentLoader, extendedRepresentation: [:extendedYAML], processingMode: 'json-ld-1.1', serializer: serializer, useRdfType: useRdfType, useNativeTypes: useNativeTypes, **, &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, **) if extractAllScripts Representation.load_stream(content.unindent, filename: url.to_s, **) else Representation.load(content, filename: url.to_s, **) end end |
.serializer(object, *args, **options) ⇒ Object
The default serializer for serialzing Ruby Objects to JSON.
Defaults to MultiJson.dump
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, **) # de-alias any objects to avoid the use of aliases and anchors "%YAML 1.2\n" + Representation.dump(object, **) 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.
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), **, &block) JSON::LD::API.add_script_loader('application/ld+yaml', self.method(:htmlLoader)) JSON::LD::API.toRdf(input, expanded: , allowed_content_types: %r(application/(.+\+)?yaml), documentLoader: documentLoader, extendedRepresentation: [:extendedYAML], processingMode: 'json-ld-1.1', **, &block) end |