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



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/json/ld/reader.rb', line 94

def initialize(input = $stdin, options = {}, &block)
  super do
    @base_uri = uri(options[:base_uri]) if options[:base_uri]
    @doc = ::JSON.load(input)

    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:



14
15
16
# File 'lib/json/ld/reader.rb', line 14

def graph
  @graph
end

Instance Method Details

#each_statement(&block) ⇒ Object

See Also:

  • RDF::Reader#each_statement


111
112
113
114
115
116
117
118
119
120
121
# File 'lib/json/ld/reader.rb', line 111

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


126
127
128
129
130
# File 'lib/json/ld/reader.rb', line 126

def each_triple(&block)
  each_statement do |statement|
    block.call(*statement.to_triple)
  end
end