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

Constant Summary collapse

DEFAULT_CONTEXT =

Default context

{
  "rdf"           => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
  "rdfs"          => "http://www.w3.org/2000/01/rdf-schema#",
  "owl"           => "http://www.w3.org/2002/07/owl#",
  "xsd"           => "http://www.w3.org/2001/XMLSchema#",
  "dcterms"       => "http://purl.org/dc/terms/",
  "foaf"          => "http://xmlns.com/foaf/0.1/",
  "cal"           => "http://www.w3.org/2002/12/cal/ical#",
  "vcard"         => "http://www.w3.org/2006/vcard/ns# ",
  "geo"           => "http://www.w3.org/2003/01/geo/wgs84_pos#",
  "cc"            => "http://creativecommons.org/ns#",
  "sioc"          => "http://rdfs.org/sioc/ns#",
  "doap"          => "http://usefulinc.com/ns/doap#",
  "com"           => "http://purl.org/commerce#",
  "ps"            => "http://purl.org/payswarm#",
  "gr"            => "http://purl.org/goodrelations/v1#",
  "sig"           => "http://purl.org/signature#",
  "ccard"         => "http://purl.org/commerce/creditcard#",
  "@coerce"       => {
    # Note: rdf:type is not in the document, but necessary for this implementation
    "xsd:anyURI"  => ["rdf:type", "foaf:homepage", "foaf:member", "rdf:type"],
    "xsd:integer" => "foaf:age",
  }
}.freeze

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



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/json/ld/reader.rb', line 121

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:

  • (RDF::Graph)


41
42
43
# File 'lib/json/ld/reader.rb', line 41

def graph
  @graph
end

Instance Method Details

#each_statement(&block) ⇒ Object

See Also:

  • RDF::Reader#each_statement


138
139
140
141
142
143
144
145
146
147
148
# File 'lib/json/ld/reader.rb', line 138

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


153
154
155
156
157
# File 'lib/json/ld/reader.rb', line 153

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