Class: JSON::LD::API
- Inherits:
-
Object
- Object
- JSON::LD::API
- 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" } }
- LINK_REL_CONTEXT =
The following constants are used to reduce object allocations
%w[rel http://www.w3.org/ns/json-ld#context].freeze
- LINK_REL_ALTERNATE =
%w[rel alternate].freeze
- LINK_TYPE_JSONLD =
%w[type application/ld+json].freeze
- JSON_LD_PROCESSING_MODES =
%w[json-ld-1.0 json-ld-1.1].freeze
- SCRIPT_LOADERS =
Hash of recognized script types and the loaders that decode them into a hash or array of hashes.
{ 'application/ld+json' => ->(content, url:, **) do validate_input(content, url: url) if [:validate] mj_opts = .keep_if { |k, v| k != :adapter || MUTLI_JSON_ADAPTERS.include?(v) } MultiJson.load(content, **mj_opts) end }
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
-
#context ⇒ JSON::LD::Context
Input evaluation context.
-
#input ⇒ String, ...
Current input.
-
#namer ⇒ JSON::LD::BlankNodeNamer
readonly
Current Blank Node Namer.
-
#value ⇒ String, ...
Current input.
Class Method Summary collapse
-
.add_script_loader(type, loader) ⇒ Object
Adds a loader for some specific content type.
-
.compact(input, context, expanded: false, serializer: nil, **options) {|jsonld| ... } ⇒ Object, Hash
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.
-
.expand(input, framing: false, serializer: nil, **options) {|jsonld, base_iri| ... } ⇒ Object, Array<Hash>
Expands the given input according to the steps in the Expansion Algorithm.
-
.flatten(input, context, expanded: false, serializer: nil, **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.
-
.frame(input, frame, expanded: false, serializer: nil, **options) {|jsonld| ... } ⇒ Object, Hash
Frames the given input using the frame according to the steps in the Framing Algorithm.
-
.fromRdf(input, useRdfType: false, useNativeTypes: false, serializer: nil, **options) {|jsonld| ... } ⇒ Object, Hash
(also: fromRDF)
Take an ordered list of RDF::Statements and turn them into a JSON-LD document.
-
.load_html(input, url:, library: nil, extractAllScripts: false, profile: nil, **options) ⇒ Object
Load one or more script tags from an HTML source.
-
.loadRemoteDocument(url, allowed_content_types: nil, 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.
-
.serializer(object, *_args, **options) ⇒ Object
The default serializer for serialzing Ruby Objects to JSON.
-
.toRdf(input, expanded: false, **options) {|statement| ... } ⇒ RDF::Enumerable
(also: toRDF)
Processes the input according to the RDF Conversion Algorithm, calling the provided callback for each triple generated.
-
.validate_input(input, url:) ⇒ Object
Validate JSON using JsonLint, if loaded.
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
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
Methods included from Expand
Instance Attribute Details
#context ⇒ JSON::LD::Context
Input evaluation context
58 59 60 |
# File 'lib/json/ld/api.rb', line 58 def context @context end |
#input ⇒ String, ...
Current input
53 |
# File 'lib/json/ld/api.rb', line 53 attr_accessor :value |
#namer ⇒ JSON::LD::BlankNodeNamer (readonly)
Current Blank Node Namer
63 64 65 |
# File 'lib/json/ld/api.rb', line 63 def namer @namer end |
#value ⇒ String, ...
Current input
53 54 55 |
# File 'lib/json/ld/api.rb', line 53 def value @value end |
Class Method Details
.add_script_loader(type, loader) ⇒ Object
Adds a loader for some specific content type
780 781 782 |
# File 'lib/json/ld/api.rb', line 780 def self.add_script_loader(type, loader) SCRIPT_LOADERS[type] = loader end |
.compact(input, context, expanded: false, serializer: nil, **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.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/json/ld/api.rb', line 235 def self.compact(input, context, expanded: false, serializer: nil, **) result = nil = { compactToRelative: true }.merge() # 1) Perform the Expansion Algorithm on the JSON-LD input. # This removes any existing context to allow the given context to be cleanly applied. = if input else API.(input, ordered: false, **) do |res, base_iri| [:base] ||= RDF::URI(base_iri) if base_iri && [:compactToRelative] res end end API.new(, context, no_default_base: true, **) 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 result = serializer.call(result, **) if serializer block_given? ? yield(result) : result end |
.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **options) {|remote_document| ... } ⇒ Object
Default document loader.
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 |
# File 'lib/json/ld/api.rb', line 734 def self.documentLoader(url, extractAllScripts: false, profile: nil, requestProfile: nil, **, &block) case url when IO, StringIO 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 && (content_type == 'application/json' || content_type.match?(%r{application/(^ld)+json})) link = url.links.find_link(LINK_REL_CONTEXT) link&.href end yield(RemoteDocument.new(url.read, documentUrl: base_uri, contentType: content_type, contextUrl: context_url)) else RDF::Util::File.open_file(url, **, &block) end end |
.expand(input, framing: false, serializer: nil, **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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/json/ld/api.rb', line 183 def self.(input, framing: false, serializer: nil, **, &block) result = doc_base = nil API.new(input, [:expandContext], **) do result = (value, nil, 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) result = serializer.call(result, **) if serializer if block 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, serializer: nil, **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.
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 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 351 |
# File 'lib/json/ld/api.rb', line 290 def self.flatten(input, context, expanded: false, serializer: nil, **) flattened = [] = { compactToRelative: true }.merge() # Expand input to simplify processing = if input else API.(input, **) do |result, base_iri| [:base] ||= RDF::URI(base_iri) if base_iri && [:compactToRelative] result end end # Initialize input using API.new(, context, no_default_base: true, **) 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 [:createAnnotations] graph_maps.each_value 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 flattened = serializer.call(flattened, **) if serializer block_given? ? yield(flattened) : flattened end |
.frame(input, frame, expanded: false, serializer: nil, **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.
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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/json/ld/api.rb', line 382 def self.frame(input, frame, expanded: false, serializer: nil, **) result = nil = { base: (RDF::URI(input) if input.is_a?(String)), compactArrays: true, compactToRelative: true, embed: '@once', explicit: false, requireAll: false, omitDefault: false }.merge() 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', **) if remote_doc.document.is_a?(String) mj_opts = .keep_if { |k, v| k != :adapter || MUTLI_JSON_ADAPTERS.include?(v) } MultiJson.load(remote_doc.document, **mj_opts) else remote_doc.document end end # Expand input to simplify processing = if input else API.(input, ordered: false, **) do |res, base_iri| [:base] ||= RDF::URI(base_iri) if base_iri && [:compactToRelative] res end end # Expand frame to simplify processing = API.(frame, framing: true, ordered: false, **) # Initialize input using frame as context API.new(, frame['@context'], no_default_base: true, **) 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?([:embed]) && context.processingMode('json-ld-1.1') if @options[:validate] raise JSON::LD::JsonLdError::InvalidEmbedValue, "#{[:embed]} is not a valid value of @embed in 1.1 mode" end warn "[DEPRECATION] #{[:embed]} is not a valid value of @embed in 1.1 mode.\n" end # Set omitGraph option, if not present, based on processingMode [:omitGraph] = context.processingMode('json-ld-1.1') unless .key?(:omitGraph) # 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.(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]), (.first || {}), parent: result, **) # Default to based on processinMode unless .key?(:pruneBlankNodeIdentifiers) [:pruneBlankNodeIdentifiers] = context.processingMode('json-ld-1.1') end # Count blank node identifiers used in the document, if pruning if [: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 [:omitGraph] || compacted.is_a?(Array) # Add the given context to the output result = if compacted.is_a?(Array) kwgraph = context.compact_iri('@graph', vocab: true) { kwgraph => compacted } else 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 result = serializer.call(result, **) if serializer block_given? ? yield(result) : result end |
.fromRdf(input, useRdfType: false, useNativeTypes: false, serializer: nil, **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.
585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/json/ld/api.rb', line 585 def self.fromRdf(input, useRdfType: false, useNativeTypes: false, serializer: nil, **) result = nil API.new(nil, nil, **) do result = from_statements(input, extendedRepresentation: [:extendedRepresentation], useRdfType: useRdfType, useNativeTypes: useNativeTypes) end result = serializer.call(result, **) if serializer 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
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
# File 'lib/json/ld/api.rb', line 794 def self.load_html(input, url:, library: nil, extractAllScripts: false, profile: nil, **) 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 extend(implementation) input = begin send("initialize_html_#{library}".to_sym, input, **) rescue StandardError raise JSON::LD::JsonLdError::LoadingDocumentFailed, "Malformed HTML document: #{$ERROR_INFO.}" 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 script_type = SCRIPT_LOADERS.keys.detect {|type| element.attributes['type'].to_s.start_with?(type)} unless script_type raise JSON::LD::JsonLdError::LoadingDocumentFailed, "Script tag has type=#{element.attributes['type']}" end loader = SCRIPT_LOADERS[script_type] loader.call(element.inner_html, url: url, **) elsif extractAllScripts res = [] SCRIPT_LOADERS.each do |type, loader| elements = if profile es = input.xpath("//script[starts-with(@type, '#{type};profile=#{profile}')]") # If no profile script, just take a single script without profile es = [input.at_xpath("//script[starts-with(@type, '#{type}')]")].compact if es.empty? es else input.xpath("//script[starts-with(@type, '#{type}')]") end elements.each do |element| content = element.inner_html r = loader.call(content, url: url, extractAllScripts: true, **) if r.is_a?(Hash) res << r elsif r.is_a?(Array) res.concat(r) end end end res else # Find the first script with a known type script_type, element = nil, nil SCRIPT_LOADERS.keys.each do |type| next if script_type # already found the type element = input.at_xpath("//script[starts-with(@type, '#{type};profile=#{profile}')]") if profile element ||= input.at_xpath("//script[starts-with(@type, '#{type}')]") script_type = type if element end unless script_type raise JSON::LD::JsonLdError::LoadingDocumentFailed, "No script tag found" unless element end content = element.inner_html SCRIPT_LOADERS[script_type].call(content, url: url, **) end rescue MultiJson::ParseError => e raise JSON::LD::JsonLdError::InvalidScriptElement, e. end |
.loadRemoteDocument(url, allowed_content_types: nil, 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.
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 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 |
# File 'lib/json/ld/api.rb', line 625 def self.loadRemoteDocument(url, allowed_content_types: nil, base: nil, documentLoader: nil, extractAllScripts: false, profile: nil, requestProfile: nil, validate: false, **) documentLoader ||= method(:documentLoader) = OPEN_OPTS.merge() if requestProfile # Add any request profile [:headers]['Accept'] = [:headers]['Accept'].sub('application/ld+json,', "application/ld+json;profile=#{requestProfile}, application/ld+json;q=0.9,") end documentLoader.call(url, extractAllScripts: extractAllScripts, **) 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 if links.length > 1 raise JSON::LD::JsonLdError::MultipleContextLinkHeaders, "expected at most 1 Link header with rel=jsonld:context, got #{links.length}" end 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, **) 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, **) do |base| remote_doc.documentUrl = base end else validate_input(remote_doc.document, url: remote_doc.documentUrl) if validate mj_opts = .keep_if { |k, v| k != :adapter || MUTLI_JSON_ADAPTERS.include?(v) } MultiJson.load(remote_doc.document, **mj_opts) end end if remote_doc.contentType && validate && !(remote_doc.contentType.match?(%r{application/(.+\+)?json|text/html|application/xhtml\+xml}) || (allowed_content_types && remote_doc.contentType.match?(allowed_content_types))) raise IOError, "url: #{url}, contentType: #{remote_doc.contentType}" end block_given? ? yield(remote_doc) : remote_doc end rescue IOError, MultiJson::ParseError => e raise JSON::LD::JsonLdError::LoadingDocumentFailed, e. end |
.serializer(object, *_args, **options) ⇒ Object
The default serializer for serialzing Ruby Objects to JSON.
Defaults to MultiJson.dump
900 901 902 903 |
# File 'lib/json/ld/api.rb', line 900 def self.serializer(object, *_args, **) serializer_opts = .fetch(:serializer_opts, JSON_STATE) MultiJson.dump(object, serializer_opts) 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.
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/json/ld/api.rb', line 526 def self.toRdf(input, expanded: false, **) unless block_given? results = [] results.extend(RDF::Enumerable) toRdf(input, expanded: , **) do |stmt| results << stmt end return results end = { extractAllScripts: true }.merge() # Flatten input to simplify processing flattened_input = API.flatten(input, nil, expanded: , ordered: false, **) API.new(flattened_input, nil, **) 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? && ![: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 |
.validate_input(input, url:) ⇒ Object
Validate JSON using JsonLint, if loaded
908 909 910 911 912 913 914 915 916 917 918 |
# File 'lib/json/ld/api.rb', line 908 def self.validate_input(input, url:) return unless defined?(JsonLint) jsonlint = JsonLint::Linter.new input = StringIO.new(input) unless input.respond_to?(:read) unless jsonlint.check_stream(input) raise JsonLdError::LoadingDocumentFailed, "url: #{url}\n" + jsonlint.errors[''].join("\n") end input.rewind end |