Class: RDF::JSON::Reader

Inherits:
Reader
  • Object
show all
Includes:
Util::Logger
Defined in:
lib/rdf/json/reader.rb

Overview

RDF/JSON parser.

Examples:

Loading RDF/JSON parsing support

require 'rdf/json'

Obtaining an RDF/JSON reader class

RDF::Reader.for(:rj)         #=> RDF::JSON::Reader
RDF::Reader.for("etc/doap.rj")
RDF::Reader.for(:file_name      => "etc/doap.rj")
RDF::Reader.for(:file_extension => "rj")
RDF::Reader.for(:content_type   => "application/rj")

Parsing RDF statements from an RDF/JSON file

RDF::JSON::Reader.open("etc/doap.rj") do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

Parsing RDF statements from an RDF/JSON string

data = StringIO.new(File.read("etc/doap.rj"))
RDF::JSON::Reader.new(data) do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

See Also:

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

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

Yields:

  • (reader)

    self

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored



50
51
52
53
54
55
56
57
58
59
# File 'lib/rdf/json/reader.rb', line 50

def initialize(input = $stdin, **options, &block)
  super do
    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:

  • (RDF::Graph)


39
40
41
# File 'lib/rdf/json/reader.rb', line 39

def graph
  @graph
end

Instance Method Details

#parse_node(string) ⇒ RDF::Node Also known as: parse_bnode

Parses an RDF/JSON blank node string into an RDF::Node instance.

Parameters:

  • string (String)

Returns:

  • (RDF::Node)

Since:

  • 0.3.0



118
119
120
121
122
# File 'lib/rdf/json/reader.rb', line 118

def parse_node(string)
  @nodes ||= {}
  id = string[2..-1] # strips off the initial '_:'
  @nodes[id.to_sym] ||= RDF::Node.new(id)
end

#parse_object(object) ⇒ RDF::Value

Parses an RDF/JSON object string into an RDF value.

Parameters:

  • object (Hash{String => Object})

Returns:

  • (RDF::Value)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rdf/json/reader.rb', line 88

def parse_object(object)
  log_error("missing 'type' key in #{object.inspect}", exception: RDF::ReaderError) unless object.has_key?('type')
  log_error("missing 'value' key in #{object.inspect}", exception: RDF::ReaderError) unless object.has_key?('value')

  case type = object['type']
    when 'bnode'
      parse_node(object['value'])
    when 'uri'
      parse_uri(object['value'])
    when 'literal'
      literal = RDF::Literal.new(object['value'],
        language: object['lang'],
        datatype: object['datatype'],
        )
      literal.validate!     if validate?
      literal.canonicalize! if canonicalize?
      literal
    else
      log_error("expected 'type' to be 'bnode', 'uri', or 'literal', but got #{type.inspect}", exception: RDF::ReaderError)
  end
rescue RDF::ReaderError
  nil
end

#parse_predicate(predicate) ⇒ RDF::URI

Parses an RDF/JSON predicate string into a URI reference.

Parameters:

  • predicate (String)

Returns:

  • (RDF::URI)


78
79
80
81
# File 'lib/rdf/json/reader.rb', line 78

def parse_predicate(predicate)
  # TODO: optional support for CURIE predicates? (issue #1 on GitHub).
  parse_uri(predicate, :intern => true)
end

#parse_subject(subject) ⇒ RDF::Resource

Parses an RDF/JSON subject string into a URI reference or blank node.

Parameters:

  • subject (String)

Returns:

  • (RDF::Resource)


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

def parse_subject(subject)
  case subject
    when /^_:/ then parse_node(subject)
    else parse_uri(subject)
  end
end

#parse_uri(string, **options) ⇒ RDF::URI

Parses an RDF/JSON URI string into an RDF::URI instance.

Parameters:

  • string (String)
  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :intern (Boolean) — default: false

Returns:

  • (RDF::URI)

Since:

  • 0.3.0



133
134
135
136
137
138
# File 'lib/rdf/json/reader.rb', line 133

def parse_uri(string, **options)
  uri = RDF::URI.send(intern = intern? && options[:intern] ? :intern : :new, string)
  uri.validate!     if validate?
  uri.canonicalize! if canonicalize? && !intern
  uri
end