Class: RDF::Microdata::JsonLdReader

Inherits:
JSON::LD::Reader
  • Object
show all
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/lib/rdf/microdata/jsonld_reader.rb

Overview

Update DOM to turn Microdata into JSON-LD and parse using the JSON-LD Reader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ reader

Initializes the JsonLdReader instance.

Parameters:

  • input (IO, File, String) (defaults to: $stdin)

    the input stream to read

  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options (see RDF::Reader#initialize)

Yields:

  • (reader)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/lib/rdf/microdata/jsonld_reader.rb', line 32

def initialize(input = $stdin, options = {}, &block)
  @options = options
  log_debug('', "using JSON-LD transformation reader")

  input = case input
  when ::Nokogiri::XML::Document, ::Nokogiri::HTML::Document then input
  else
    # Try to detect charset from input
    options[:encoding] ||= input.charset if input.respond_to?(:charset)
    
    # Otherwise, default is utf-8
    options[:encoding] ||= 'utf-8'
    options[:encoding] = options[:encoding].to_s if options[:encoding]
    input = input.read if input.respond_to?(:read)
    ::Nokogiri::HTML5(input.force_encoding(options[:encoding]))
  end

  # Load registry
  begin
    registry_uri = options[:registry] || RDF::Microdata::DEFAULT_REGISTRY
    log_debug('', "registry = #{registry_uri.inspect}")
    Registry.load_registry(registry_uri)
  rescue JSON::ParserError => e
    log_fatal("Failed to parse registry: #{e.message}", exception: RDF::ReaderError) if (root.nil? && validate?)
  end

  @jsonld = {'@graph' => []}

  # Start with all top-level items
  input.css("[itemscope]").each do |item|
    next if item['itemprop']  # Only top-level items
    jsonld['@graph'] << get_object(item)
  end

  log_debug('', "Transformed document: #{jsonld.to_json(JSON::LD::JSON_STATE)}")

  # Rely on RDFa reader
  super(jsonld.to_json, options, &block)
end

Instance Attribute Details

#jsonldHash (readonly)

The resulting JSON-LD

Returns:



10
11
12
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/lib/rdf/microdata/jsonld_reader.rb', line 10

def jsonld
  @jsonld
end

Class Method Details

.format(klass = nil) ⇒ Object



12
13
14
15
16
17
18
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/lib/rdf/microdata/jsonld_reader.rb', line 12

def self.format(klass = nil)
  if klass.nil?
    RDF::Microdata::Format
  else
    super
  end
end