Class: RDF::Raptor::CLI::Writer

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

Overview

CLI writer implementation.

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout, base_uri: nil, **options) {|writer| ... } ⇒ Writer

Initializes the CLI writer instance.

Parameters:

  • output (IO, File) (defaults to: $stdout)
  • options (Hash{Symbol => Object})

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

Yields:

  • (writer)

    self

Yield Parameters:

  • writer (RDF::Writer)

Yield Returns:

  • (void)

Raises:

  • (RDF::WriterError)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rdf/raptor/cli.rb', line 138

def initialize(output = $stdout, base_uri: nil, **options, &block)
  raise RDF::WriterError, "`rapper` binary not found" unless RDF::Raptor.available?

  format = self.class.format.rapper_format
  case output
    when File, IO, StringIO, Tempfile
      @command = "#{RAPPER} -q -i turtle -o #{format} file:///dev/stdin"
      @command << " '#{base_uri}'" if options.has_key?(:base_uri)
      @rapper  = IO.popen(@command, 'rb+')
    else
      raise ArgumentError, "unsupported output type: #{output.inspect}"
  end
  @writer = RDF::NTriples::Writer.new(@rapper, **options)
  super(output, base_uri: base_uri, **options, &block)
end

Instance Method Details

#output_transit(may_block) (protected)

This method returns an undefined value.

Feeds any available rapper output to the destination.

Parameters:

  • may_block (Boolean)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rdf/raptor/cli.rb', line 188

def output_transit(may_block)
  unless @rapper.closed?
    chunk_size = @options[:chunk_size] || 4096 # bytes
    begin
      loop do
        @output.write(may_block ? @rapper.sysread(chunk_size) : @rapper.read_nonblock(chunk_size))
      end
    rescue EOFError => e
      @rapper.close
    rescue Errno::EAGAIN, Errno::EINTR
      # eat
    end
  end
end

#write_epilogue (protected)

This method returns an undefined value.

See Also:

  • Writer#write_epilogue


178
179
180
181
# File 'lib/rdf/raptor/cli.rb', line 178

def write_epilogue
  @rapper.close_write unless @rapper.closed?
  output_transit(true)
end

#write_prologue (protected)

This method returns an undefined value.

See Also:

  • Writer#write_prologue


159
160
161
# File 'lib/rdf/raptor/cli.rb', line 159

def write_prologue
  super
end

#write_triple(subject, predicate, object) (protected)

This method returns an undefined value.

Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Term)

See Also:

  • Writer#write_triple


169
170
171
172
173
# File 'lib/rdf/raptor/cli.rb', line 169

def write_triple(subject, predicate, object)
  output_transit(false)
  @writer.write_triple(subject, predicate, object)
  output_transit(false)
end