Sha256: 12a2980b6ecbec8c440883632f20082193104c09e48b61a0726833ce4ba86cbb

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

# -*- encoding: utf-8 -*-
require 'json/ld/reader'

module YAML_LD
  ##
  # A YAML-LD parser in Ruby.
  class Reader < JSON::LD::Reader
    format Format

    ##
    # Initializes the YAML-LD reader instance.
    #
    # @param  [IO, File, String]       input
    # @param [Proc] documentLoader
    #   The callback of the loader to be used to retrieve remote documents and contexts, and to parse IO objects.
    #   If specified, it must be used to retrieve remote documents and contexts; otherwise, if not specified, the processor's built-in loader must be used.
    #   The remote document returned must be parsed if it is YAML.
    # @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,
      documentLoader: YAML_LD::API.method(:documentLoader),
      **options, &block)
      input = StringIO.new(input).tap do |d|
        d.define_singleton_method(:content_type) {'application/ld+yaml'}
      end if input.is_a?(String)
      super(input, documentLoader: documentLoader, **options, &block)
    end

    ##
    # @private
    # @see   RDF::Reader#each_statement
    def each_statement(&block)
      API.toRdf(@doc, **@options, &block)
    rescue ::Psych::SyntaxError, ::JSON::LD::JsonLdError => e
      log_fatal("Failed to parse input document: #{e.message}", exception: RDF::ReaderError)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
yaml-ld-0.0.3 lib/yaml_ld/reader.rb
yaml-ld-0.0.2 lib/yaml_ld/reader.rb