Class: JSON::LD::Reader
- Inherits:
-
RDF::Reader
- Object
- RDF::Reader
- JSON::LD::Reader
- Defined in:
- lib/json/ld/reader.rb
Overview
A JSON-LD parser in Ruby.
Defined Under Namespace
Classes: EvaluationContext
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.
Constructor Details
#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ Reader
Initializes the RDF/JSON reader instance.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/json/ld/reader.rb', line 97 def initialize(input = $stdin, = {}, &block) super do @base_uri = uri([:base_uri]) if [:base_uri] begin @doc = JSON.load(input) rescue JSON::ParserError => e raise RDF::ReaderError, "Failed to parse input document: #{e.}" end 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.
16 17 18 |
# File 'lib/json/ld/reader.rb', line 16 def graph @graph end |
Instance Method Details
#each_statement(&block) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/json/ld/reader.rb', line 118 def each_statement(&block) @callback = block # initialize the evaluation context with the appropriate base ec = EvaluationContext.new do |ec| ec.base = @base_uri if @base_uri parse_context(ec, DEFAULT_CONTEXT) end traverse("", @doc, nil, nil, ec) end |
#each_triple(&block) ⇒ Object
133 134 135 136 137 |
# File 'lib/json/ld/reader.rb', line 133 def each_triple(&block) each_statement do |statement| block.call(*statement.to_triple) end end |