Class: JSON::LD::Reader

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

Overview

A JSON-LD parser in Ruby.

See Also:

Author:

Instance Attribute Summary

Attributes included from StreamingReader

#base, #namer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StreamingReader

format, #stream_statement

Methods included from ToRDF

#item_to_rdf, #node, #parse_list

Methods included from Utils

#add_value, #as_array, #as_resource, #blank_node?, #compare_values, #graph?, #has_property, #has_value, #index?, #list?, #node?, #node_or_ref?, #node_reference?, #simple_graph?, #value?

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})

    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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/json/ld/reader.rb', line 69

def initialize(input = $stdin, **options, &block)
  options[:base_uri] ||= options[:base]
  super do
    @options[:base] ||= base_uri.to_s if base_uri
    # 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

    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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/json/ld/reader.rb', line 16

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).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
    RDF::CLI::Option.new(
      symbol: :extractAllScripts,
      datatype: TrueClass,
      default: false,
      control: :checkbox,
      on: ["--[no-]extract-all-scripts"],
      description: "If set to true, when extracting JSON-LD script elements from HTML, unless a specific fragment identifier is targeted, extracts all encountered JSON-LD script elements using an array form, if necessary.") {|arg| RDF::URI(arg)},
    RDF::CLI::Option.new(
      symbol: :lowercaseLanguage,
      datatype: TrueClass,
      control: :checkbox,
      on: ["--[no-]lowercase-language"],
      description: "By default, language tags are left as is. To normalize to lowercase, set this option to `true`."),
    RDF::CLI::Option.new(
      symbol: :processingMode,
      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)"),
    RDF::CLI::Option.new(
      symbol: :rdfDirection,
      datatype: %w(i18n-datatype compound-literal),
      default: 'null',
      control: :select,
      on: ["--rdf-direction DIR", %w(i18n-datatype compound-literal)],
      description: "How to serialize literal direction (i18n-datatype compound-literal)") {|arg| RDF::URI(arg)},
    RDF::CLI::Option.new(
      symbol: :stream,
      datatype: TrueClass,
      control: :checkbox,
      on: ["--[no-]stream"],
      description: "Optimize for streaming JSON-LD to RDF.") {|arg| arg},
  ]
end

Instance Method Details

#each_statement(&block) ⇒ Object

See Also:

  • RDF::Reader#each_statement


92
93
94
95
96
97
98
99
100
# File 'lib/json/ld/reader.rb', line 92

def each_statement(&block)
  if @options[:stream]
    stream_statement(&block)
  else
    API.toRdf(@doc, **@options, &block)
  end
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


105
106
107
108
109
110
111
112
# File 'lib/json/ld/reader.rb', line 105

def each_triple(&block)
  if block_given?
    each_statement do |statement|
      yield(*statement.to_triple)
    end
  end
  enum_for(:each_triple)
end