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` and API.initialize)

Yields:

  • (reader)

    ‘self`

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored

Raises:

  • (RDF::ReaderError)

    if the JSON document cannot be loaded



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/json/ld/reader.rb', line 42

def initialize(input = $stdin, options = {}, &block)
  options[:base_uri] ||= options[:base]
  super do
    @options[:base] ||= base_uri.to_s if base_uri
    begin
      # Trim non-JSON stuff in script.
      @doc = if input.respond_to?(:read)
        input
      else
        StringIO.new(input.to_s.sub(%r(\A[^{\[]*)m, '').sub(%r([^}\]]*\Z)m, ''))
      end
    end

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

Class Method Details

.optionsObject

JSON-LD Reader options



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/json/ld/reader.rb', line 15

def self.options
  super + [
    RDF::CLI::Option.new(
      symbol: :expandContext,
      control: :url2,
      datatype: RDF::URI,
      on: ["--expand-context CONTEXT"],
      description: "Context to use when expanding.") {|arg| RDF::URI(arg)},
    RDF::CLI::Option.new(
      symbol: :processing_mode,
      datatype: %w(json-ld-1.0 json-ld-1.1),
      control: :radio,
      on: ["--processingMode MODE", %w(json-ld-1.0 json-ld-1.1)],
      description: "Set Processing Mode (json-ld-1.0 or json-ld-1.1)"),
  ]
end

Instance Method Details

#each_statement(&block) ⇒ Object

See Also:

  • RDF::Reader#each_statement


67
68
69
70
71
# File 'lib/json/ld/reader.rb', line 67

def each_statement(&block)
  JSON::LD::API.toRdf(@doc, @options, &block)
rescue ::JSON::ParserError, ::JSON::LD::JsonLdError => e
  log_fatal("Failed to parse input document: #{e.message}", exception: RDF::ReaderError)
end

#each_triple(&block) ⇒ Object

See Also:

  • RDF::Reader#each_triple


76
77
78
79
80
81
82
83
# File 'lib/json/ld/reader.rb', line 76

def each_triple(&block)
  if block_given?
    JSON::LD::API.toRdf(@doc, @options) do |statement|
      yield *statement.to_triple
    end
  end
  enum_for(:each_triple)
end