Module: JSON::LD::StreamingReader

Includes:
ToRDF, Utils
Included in:
Reader
Defined in:
lib/json/ld/streaming_reader.rb

Overview

A streaming JSON-LD parser in Ruby.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ToRDF

#item_to_rdf, #node

Methods included from Utils

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

Instance Attribute Details

#baseRDF::URI (readonly)

The base URI to use when resolving relative URIs

Returns:


18
19
20
# File 'lib/json/ld/streaming_reader.rb', line 18

def base
  @base
end

#namerObject (readonly)

Returns the value of attribute namer


19
20
21
# File 'lib/json/ld/streaming_reader.rb', line 19

def namer
  @namer
end

Class Method Details

.formatObject


21
# File 'lib/json/ld/streaming_reader.rb', line 21

def self.format; JSON::LD::Format; end

Instance Method Details

#stream_statement(&block) ⇒ Object

See Also:

  • RDF::Reader#each_statement

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/json/ld/streaming_reader.rb', line 25

def stream_statement(&block)
  unique_bnodes, rename_bnodes = @options[:unique_bnodes], @options.fetch(:rename_bnodes, true)
  # FIXME: document loader doesn't stream
  @base = RDF::URI(@options[:base] || base_uri)
  value = MultiJson.load(@doc, **@options)
  context_ref = @options[:expandContext]
  #context_ref = @options.fetch(:expandContext, remote_doc.contextUrl)
  context = Context.parse(context_ref, **@options)

  @namer = unique_bnodes ? BlankNodeUniqer.new : (rename_bnodes ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
  # Namer for naming provisional nodes, which may be determined later to be actual
  @provisional_namer = BlankNodeNamer.new("p")

  parse_object(value, nil, context, graph_is_named: false) do |st|
    # Only output reasonably valid triples
    if st.to_a.all? {|r| r.is_a?(RDF::Term) && (r.uri? ? r.valid? : true)}
      block.call(st)
    end
  end
rescue ::JSON::ParserError, ::JSON::LD::JsonLdError => e
  log_fatal("Failed to parse input document: #{e.message}", exception: RDF::ReaderError)
end