Sha256: 53742e41aedacf65911314b1688628ac5b334e0829853d5def37525a45bc0e31
Contents?: true
Size: 1.8 KB
Versions: 2
Compression:
Stored size: 1.8 KB
Contents
require 'open-uri' module JSON::LD ## # A JSON-LD parser in Ruby. # # @see http://json-ld.org/spec/ED/20110507/ # @author [Gregg Kellogg](http://greggkellogg.net/) class Reader < RDF::Reader format Format ## # Override normal symbol generation def self.to_sym :jsonld end ## # Initializes the RDF/JSON reader instance. # # @param [IO, File, String] input # @param [Hash{Symbol => Object}] options # any additional options (see `RDF::Reader#initialize` and {JSON::LD::API.initialize}) # @yield [reader] `self` # @yieldparam [RDF::Reader] reader # @yieldreturn [void] ignored # @raise [RDF::ReaderError] if the JSON document cannot be loaded 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. input = input.read if input.respond_to?(:read) input = input.to_s.sub(%r(\A[^{\[]*)m, '').sub(%r([^}\]]*\Z)m, '') @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 ## # @private # @see RDF::Reader#each_statement def each_statement(&block) JSON::LD::API.toRdf(@doc, @options, &block) end ## # @private # @see RDF::Reader#each_triple def each_triple JSON::LD::API.toRdf(@doc, @options) do |statement| yield *statement.to_triple end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
json-ld-1.1.3 | lib/json/ld/reader.rb |
json-ld-1.1.2 | lib/json/ld/reader.rb |