Class: RDF::Raptor::CLI::Reader

Inherits:
RDF::Reader
  • Object
show all
Defined in:
lib/rdf/raptor/cli.rb

Overview

CLI reader implementation.

Defined Under Namespace

Modules: Extensions

Instance Method Summary collapse

Constructor Details

#initialize(input = $stdin, base_uri: nil, **options) {|reader| ... } ⇒ Reader

Initializes the CLI reader instance.

Parameters:

  • input (IO, File, RDF::URI, String) (defaults to: $stdin)
  • base_uri (String, #to_s) (defaults to: nil)

    (“file:///dev/stdin”)

  • options (Hash{Symbol => Object})

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

Yields:

  • (reader)

    self

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored

Raises:

  • (RDF::ReaderError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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

Instance Method Details

#read_tripleArray(RDF::Resource, RDF::URI, RDF::Term) (protected)

Returns:

  • (Array(RDF::Resource, RDF::URI, RDF::Term))

Raises:

  • (EOFError)

See Also:

  • RDF::Reader#read_triple


92
93
94
95
96
97
98
99
100
101
# 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