Class: RDF::Microdata::Reader

Inherits:
Reader show all
Includes:
Expansion, Util::Logger
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/reader.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/reader/nokogiri.rb

Overview

An Microdata parser in Ruby

Based on processing rules, amended with the following:

Defined Under Namespace

Modules: Nokogiri

Constant Summary collapse

URL_PROPERTY_ELEMENTS =
%w(a area audio embed iframe img link object source track video)

Constants included from Util::Logger

Util::Logger::IOWrapper

Instance Attribute Summary collapse

Attributes inherited from Reader

#options

Attributes included from Enumerable

#existentials, #universals

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Logger

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

Methods included from Expansion

#expand, #rule

Methods inherited from Reader

#canonicalize?, #close, each, #each_pg_statement, #encoding, #fail_object, #fail_predicate, #fail_subject, for, format, #intern?, #lineno, open, #prefix, #prefixes, #prefixes=, #read_statement, #read_triple, #rewind, #to_sym, to_sym, #valid?, #validate?

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Enumerable

add_entailment, #canonicalize, #canonicalize!, #dump, #each_graph, #each_object, #each_predicate, #each_quad, #each_subject, #each_term, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph?, #graph_names, #invalid?, #method_missing, #object?, #objects, #predicate?, #predicates, #project_graph, #quad?, #quads, #respond_to_missing?, #statement?, #statements, #subject?, #subjects, #supports?, #term?, #terms, #to_a, #to_h, #to_set, #triple?, #triples, #valid?, #validate!

Methods included from Isomorphic

#bijection_to, #isomorphic_with?

Methods included from Countable

#count, #empty?

Methods included from Readable

#readable?

Constructor Details

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

Initializes the Microdata reader instance.

Parameters:

Options Hash (**options):

  • :encoding (Encoding) — default: Encoding::UTF_8

    the encoding of the input stream (Ruby 1.9+)

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

  • :canonicalize (Boolean) — default: false

    whether to canonicalize parsed literals

  • :intern (Boolean) — default: true

    whether to intern all parsed URIs

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs

  • :registry (#to_s)

Yields:

  • (reader)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Raises:

  • (Error)

    Raises RDF::ReaderError when validating



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/reader.rb', line 97

def initialize(input = $stdin, **options, &block)
  super do
    @library = :nokogiri

    require "rdf/microdata/reader/#{@library}"
    @implementation = Nokogiri
    self.extend(@implementation)

    input.rewind if input.respond_to?(:rewind)
    initialize_html(input, **options) rescue log_fatal($!.message, exception: RDF::ReaderError)

    log_error("Empty document") if root.nil?
    log_error(doc_errors.map(&:message).uniq.join("\n")) if !doc_errors.empty?

    log_debug('', "library = #{@library}")

    # 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
    
    if block_given?
      case block.arity
        when 0 then instance_eval(&block)
        else block.call(self)
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Instance Attribute Details

#implementationModule (readonly)

Returns the HTML implementation module for this reader instance.

Returns:

  • (Module)

    Returns the HTML implementation module for this reader instance.



23
24
25
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/reader.rb', line 23

def implementation
  @implementation
end

#memoryHash{Object => RDF::Resource} (readonly)

Returns maps RDF elements (items) to resources.

Returns:



26
27
28
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/reader.rb', line 26

def memory
  @memory
end

Class Method Details

.optionsObject

Reader options



43
44
45
46
47
48
49
50
51
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/reader.rb', line 43

def self.options
  super + [
    RDF::CLI::Option.new(
      symbol: :rdfa,
      datatype: TrueClass,
      on: ["--rdfa"],
      description: "Transform and parse as RDFa.") {true},
  ]
end

Instance Method Details

#base_uriHash{Symbol => RDF::URI}

Returns the base URI determined by this reader.

Examples:

reader.prefixes[:dc]  #=> RDF::URI('http://purl.org/dc/terms/')

Returns:

Since:

  • 0.3.0



36
37
38
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/reader.rb', line 36

def base_uri
  @options[:base_uri]
end

#each_statement {|statement| ... }

This method returns an undefined value.

Iterates the given block for each RDF statement in the input.

Reads to graph and performs expansion if required.

Yields:

  • (statement)

Yield Parameters:



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/reader.rb', line 139

def each_statement(&block)
  if block_given?
    @callback = block

    # parse
    parse_whole_document(@doc, base_uri)

    if validate? && log_statistics[:error]
      raise RDF::ReaderError, "Errors found during processing"
    end
  end
  enum_for(:each_statement)
end

#each_triple {|subject, predicate, object| ... }

This method returns an undefined value.

Iterates the given block for each RDF triple in the input.

Yields:

  • (subject, predicate, object)

Yield Parameters:



161
162
163
164
165
166
167
168
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-microdata-73e8bfd8e661/lib/rdf/microdata/reader.rb', line 161

def each_triple(&block)
  if block_given?
    each_statement do |statement|
      block.call(*statement.to_triple)
    end
  end
  enum_for(:each_triple)
end