class RDF::Raptor::CLI::Reader

CLI reader implementation.

Public Class Methods

new(input = $stdin, base_uri: nil, **options, &block) click to toggle source

Initializes the CLI reader instance.

@param [IO, File, RDF::URI, String] input @param [String, to_s] base_uri (“file:///dev/stdin”) @param [Hash{Symbol => Object}] options

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

@yield [reader] ‘self` @yieldparam [RDF::Reader] reader @yieldreturn [void] ignored

# File lib/rdf/raptor/cli.rb, line 36
def initialize(input = $stdin, base_uri: nil, **options, &block)
  raise RDF::ReaderError, "`rapper` binary not found" unless RDF::Raptor.available?

  format = self.class.format.rapper_format
  case input
    when RDF::URI, %r(^(file|http|https|ftp)://)
      @command = "#{RAPPER} -q -i #{format} -o ntriples '#{input}'"
      @command << " '#{base_uri}'" if options.has_key?(:base_uri)
      @rapper  = IO.popen(@command, 'rb')

    when File, Tempfile
      @command = "#{RAPPER} -q -i #{format} -o ntriples '#{File.expand_path(input.path)}'"
      @command << " '#{base_uri}'" if options.has_key?(:base_uri)
      @rapper  = IO.popen(@command, 'rb')

    else # IO, String
      @command = "#{RAPPER} -q -i #{format} -o ntriples file:///dev/stdin"
      @command << " '#{base_uri}'" if options.has_key?(:base_uri)
      @rapper  = IO.popen(@command, 'rb+')
      pid = fork do
        # process to feed `rapper`
        begin
          @rapper.close_read
          if input.respond_to?(:read)
            buf = String.new
            while input.read(8192, buf)
              @rapper.write(buf)
            end
          else
            @rapper.write(input.to_s)
          end
          @rapper.close_write
        ensure
          Process.exit
        end
      end
      Process.detach(pid)
      @rapper.close_write
  end

  @options = options
  @reader = RDF::NTriples::Reader.new(@rapper, @options).extend(Extensions)

  if block_given?
    case block.arity
      when 0 then instance_eval(&block)
      else block.call(self)
    end
  end
end

Protected Instance Methods

read_triple() click to toggle source

@return [Array(RDF::Resource, RDF::URI, RDF::Term)] @see RDF::Reader#read_triple

# File lib/rdf/raptor/cli.rb, line 92
def read_triple
  raise EOFError if @rapper.closed?
  begin
    triple = @reader.read_triple
  rescue EOFError
    @rapper.close
    raise
  end
  triple
end