Class: RDF::Microdata::Reader

Inherits:
Reader show all
Includes:
Expansion, Util::Logger
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/lib/rdf/microdata/reader.rb,
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/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

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

Instance Attribute Summary collapse

Attributes inherited from Reader

#options

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, #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, #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_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #supports?, #terms, #to_a, #to_h, #to_set, #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



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
130
131
132
133
134
135
136
137
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/lib/rdf/microdata/reader.rb', line 105

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/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/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/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/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/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/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/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/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:



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/lib/rdf/microdata/reader.rb', line 147

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:



169
170
171
172
173
174
175
176
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-microdata-14cc40c62067/lib/rdf/microdata/reader.rb', line 169

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