Sha256: 09b7643b8b3f2f0412305c674dd988815829873cae21aa2407217422d27795b4

Contents?: true

Size: 1.66 KB

Versions: 14

Compression:

Stored size: 1.66 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`)
    # @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] if options.has_key?(:base)
      options[:base] ||= options[:base_uri] if options.has_key?(:base_uri)
      super do
        begin
          @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[:context], nil, @options, &block)
    end

    ##
    # @private
    # @see   RDF::Reader#each_triple
    def each_triple(&block)
      each_statement do |statement|
        block.call(*statement.to_triple)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
json-ld-0.9.0 lib/json/ld/reader.rb
json-ld-0.3.2 lib/json/ld/reader.rb
json-ld-0.3.1 lib/json/ld/reader.rb
json-ld-0.3.0 lib/json/ld/reader.rb
json-ld-0.1.6.1 lib/json/ld/reader.rb
json-ld-0.1.6 lib/json/ld/reader.rb
json-ld-0.1.5.2 lib/json/ld/reader.rb
json-ld-0.1.5.1 lib/json/ld/reader.rb
json-ld-0.1.5 lib/json/ld/reader.rb
json-ld-0.1.4.2 lib/json/ld/reader.rb
json-ld-0.1.4.1 lib/json/ld/reader.rb
json-ld-0.1.4 lib/json/ld/reader.rb
json-ld-0.1.3 lib/json/ld/reader.rb
json-ld-0.1.2 lib/json/ld/reader.rb