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:

Class Method 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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/json/ld/reader.rb', line 28

def initialize(input = $stdin, options = {}, &block)
  options[:base_uri] ||= options[:base] if options.has_key?(:base)
  options[:base] ||= options[:base_uri] if options.has_key?(:base_uri)
  super do
    begin
      @doc = JSON.load(input)
    rescue JSON::ParserError => e
      raise RDF::ReaderError, "Failed to parse input document: #{e.message}" if validate?
      @doc = JSON.parse("{}")
    end

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

Class Method Details

.to_symObject

Override normal symbol generation



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

def self.to_sym
  :jsonld
end

Instance Method Details

#each_statement(&block) ⇒ Object

See Also:

  • RDF::Reader#each_statement


51
52
53
# File 'lib/json/ld/reader.rb', line 51

def each_statement(&block)
  JSON::LD::API.toRDF(@doc, @options[:context], nil, @options, &block)
end

#each_triple(&block) ⇒ Object

See Also:

  • RDF::Reader#each_triple


58
59
60
61
62
# File 'lib/json/ld/reader.rb', line 58

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