Class: RDF::JSON::Reader
- Inherits:
-
Reader
- Object
- Reader
- RDF::JSON::Reader
- Includes:
- Util::Logger
- Defined in:
- lib/rdf/json/reader.rb
Overview
RDF/JSON parser.
Instance Attribute Summary collapse
-
#graph ⇒ RDF::Graph
readonly
The graph constructed when parsing.
Instance Method Summary collapse
- #each_statement(&block) ⇒ Object
- #each_triple(&block) ⇒ Object
-
#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::Nodeinstance. -
#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::URIinstance.
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 '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 |
Instance Attribute Details
#graph ⇒ RDF::Graph (readonly)
The graph constructed when parsing.
39 40 41 |
# File 'lib/rdf/json/reader.rb', line 39 def graph @graph end |
Instance Method Details
#each_statement(&block) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rdf/json/reader.rb', line 143 def each_statement(&block) if block_given? @input.rewind rescue nil begin ::JSON.parse(@input.read).each do |subject, predicates| subject = parse_subject(subject) predicates.each do |predicate, objects| predicate = parse_predicate(predicate) objects.each do |object| object = parse_object(object) yield RDF::Statement(subject, predicate, object) if object end end end rescue ::JSON::ParserError => e log_error(e.) end if validate? && log_statistics[:error] raise RDF::ReaderError, "Errors found during processing" end end enum_for(:each_statement) end |
#each_triple(&block) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/rdf/json/reader.rb', line 170 def each_triple(&block) if block_given? each_statement do |statement| block.call(*statement.to_triple) end end enum_for(:each_triple) end |
#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 '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 '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 '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 '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 '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 |