Class: RDF::JSON::Reader
- Includes:
- Util::Logger
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/reader.rb
Overview
RDF/JSON parser.
Constant Summary
Constants included from Util::Logger
Instance Attribute Summary collapse
-
#graph ⇒ RDF::Graph
readonly
The graph constructed when parsing.
Attributes inherited from Reader
Attributes included from Enumerable
Instance Method Summary collapse
-
#initialize(input = $stdin, **options) {|reader| ... } ⇒ Reader
constructor
Initializes the RDF/JSON reader instance.
-
#parse_node(string) ⇒ RDF::Node
(also: #parse_bnode)
Parses an RDF/JSON blank node string into an
RDF::Node
instance. -
#parse_object(object) ⇒ RDF::Value
Parses an RDF/JSON object string into an RDF value.
-
#parse_predicate(predicate) ⇒ RDF::URI
Parses an RDF/JSON predicate string into a URI reference.
-
#parse_subject(subject) ⇒ RDF::Resource
Parses an RDF/JSON subject string into a URI reference or blank node.
-
#parse_uri(string, **options) ⇒ RDF::URI
Parses an RDF/JSON URI string into an
RDF::URI
instance.
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 inherited from Reader
#base_uri, #canonicalize?, #close, each, #each_pg_statement, #encoding, #fail_object, #fail_predicate, #fail_subject, for, format, #intern?, #lineno, open, options, #prefix, #prefixes, #prefixes=, #read_statement, #read_triple, #rewind, to_sym, #to_sym, #valid?, #validate?
Methods included from Util::Aliasing::LateBound
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
Methods included from Readable
Constructor Details
#initialize(input = $stdin, **options) {|reader| ... } ⇒ Reader
Initializes the RDF/JSON reader instance.
50 51 52 53 54 55 56 57 58 59 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/reader.rb', line 50 def initialize(input = $stdin, **, &block) super do 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
#graph ⇒ RDF::Graph (readonly)
The graph constructed when parsing.
39 40 41 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/reader.rb', line 39 def graph @graph end |
Instance Method Details
#parse_node(string) ⇒ RDF::Node Also known as: parse_bnode
Parses an RDF/JSON blank node string into an RDF::Node
instance.
118 119 120 121 122 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/reader.rb', line 118 def parse_node(string) @nodes ||= {} id = string[2..-1] # strips off the initial '_:' @nodes[id.to_sym] ||= RDF::Node.new(id) end |
#parse_object(object) ⇒ RDF::Value
Parses an RDF/JSON object string into an RDF value.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/reader.rb', line 88 def parse_object(object) log_error("missing 'type' key in #{object.inspect}", exception: RDF::ReaderError) unless object.has_key?('type') log_error("missing 'value' key in #{object.inspect}", exception: RDF::ReaderError) unless object.has_key?('value') case type = object['type'] when 'bnode' parse_node(object['value']) when 'uri' parse_uri(object['value']) when 'literal' literal = RDF::Literal.new(object['value'], language: object['lang'], datatype: object['datatype'], ) literal.validate! if validate? literal.canonicalize! if canonicalize? literal else log_error("expected 'type' to be 'bnode', 'uri', or 'literal', but got #{type.inspect}", exception: RDF::ReaderError) end rescue RDF::ReaderError nil end |
#parse_predicate(predicate) ⇒ RDF::URI
Parses an RDF/JSON predicate string into a URI reference.
78 79 80 81 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/reader.rb', line 78 def parse_predicate(predicate) # TODO: optional support for CURIE predicates? (issue #1 on GitHub). parse_uri(predicate, :intern => true) end |
#parse_subject(subject) ⇒ RDF::Resource
Parses an RDF/JSON subject string into a URI reference or blank node.
66 67 68 69 70 71 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/reader.rb', line 66 def parse_subject(subject) case subject when /^_:/ then parse_node(subject) else parse_uri(subject) end end |
#parse_uri(string, **options) ⇒ RDF::URI
Parses an RDF/JSON URI string into an RDF::URI
instance.
133 134 135 136 137 138 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-json-7d579de01769/lib/rdf/json/reader.rb', line 133 def parse_uri(string, **) uri = RDF::URI.send(intern = intern? && [:intern] ? :intern : :new, string) uri.validate! if validate? uri.canonicalize! if canonicalize? && !intern uri end |