Class: JSON::LD::Reader

Inherits:
RDF::Reader
  • Object
show all
Defined in:
lib/json/ld/reader.rb

Overview

A JSON-LD parser in Ruby.

See Also:

Author:

Defined Under Namespace

Classes: EvaluationContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes the RDF/JSON reader instance.

Parameters:

  • input (IO, File, String) (defaults to: $stdin)
  • options (Hash{Symbol => Object}) (defaults to: {})

    any additional options (see ‘RDF::Reader#initialize`)

Yields:

  • (reader)

    ‘self`

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored

Raises:

  • (RDF::ReaderError)

    if the JSON document cannot be loaded



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, options = {}, &block)
  super do
    @base_uri = uri(options[:base_uri]) if options[:base_uri]
    begin
      @doc = JSON.load(input)
    rescue JSON::ParserError => e
      raise RDF::ReaderError, "Failed to parse input document: #{e.message}"
    end

    if block_given?
      case block.arity
        when 0 then instance_eval(&block)
        else block.call(self)
      end
    end
  end
end

Instance Attribute Details

#graphRDF::Graph (readonly)

The graph constructed when parsing.

Returns:



16
17
18
# File 'lib/json/ld/reader.rb', line 16

def graph
  @graph
end

Instance Method Details

#each_statement(&block) ⇒ Object

See Also:

  • RDF::Reader#each_statement


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

See Also:

  • RDF::Reader#each_triple


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