class RDF::Raptor::FFI::V1::Parser

This class provides the functionality of turning syntaxes into RDF triples - RDF parsing.

@see librdf.org/raptor/api-1.4/raptor-section-parser.html

Constants

BASE_URI

The default base URI

BUFFER_SIZE

The maximum chunk size for ‘#parse_stream`

Public Class Methods

new(ptr_or_name) click to toggle source

@overload initialize(ptr)

@param  [FFI::Pointer] ptr

@overload initialize(name)

@param  [Symbol, String] name
Calls superclass method
# File lib/rdf/raptor/ffi/v1/parser.rb, line 24
def initialize(ptr_or_name)
  ptr = case ptr_or_name
    when FFI::Pointer then ptr_or_name
    when Symbol       then V1.raptor_new_parser(ptr_or_name.to_s)
    when String       then V1.raptor_new_parser(ptr_or_name)
    else nil
  end
  raise ArgumentError, "invalid argument: #{ptr_or_name.inspect}" if ptr.nil? || ptr.null?
  super(ptr)
end
release(ptr) click to toggle source

Releases ‘libraptor` memory associated with this structure.

@param [FFI::Pointer] ptr @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 40
def self.release(ptr)
  V1.raptor_free_parser(ptr)
end

Public Instance Methods

error_handler=(handler) click to toggle source

@param [Proc] handler @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 47
def error_handler=(handler)
  V1.raptor_set_error_handler(self, self, handler)
end
parse(input, **options, &block) click to toggle source

@param [Object] input

the input to parse

@param [Hash{Symbol => Object}] options

any additional options for parsing

@option options [String, to_s] :base_uri (nil)

the base URI to use when resolving relative URIs

@yield [parser, statement]

each statement in the input

@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 78
def parse(input, **options, &block)
  case input
    when RDF::URI, %r(^(file|https|http|ftp)://)
      parse_url(input, **options, &block)
    when File, Tempfile
      parse_file(input, **options, &block)
    when IO, StringIO
      parse_stream(input, **options, &block)
    when String
      parse_buffer(input, **options, &block)
    else
      raise ArgumentError, "don't know how to parse #{input.inspect}"
  end
end
parse_buffer(buffer, base_uri: nil, **options, &block) click to toggle source

@param [String, to_str] buffer

the input buffer to parse

@param [Hash{Symbol => Object}] options

any additional options for parsing (see {#parse})

@yield [parser, statement]

each statement in the input

@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 171
def parse_buffer(buffer, base_uri: nil, **options, &block)
  self.statement_handler = block if block_given?

  parse_start!((base_uri || BASE_URI).to_s)
  parse_chunk(buffer.to_str)
  parse_end!
end
parse_chunk(buffer) click to toggle source

@private @param [String] buffer

the input chunk to parse

@return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 193
def parse_chunk(buffer)
  result = V1.raptor_parse_chunk(self, buffer, buffer.bytesize, 0)
  # TODO: error handling if result.nonzero?
end
parse_end!() click to toggle source

@private @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 201
def parse_end!
  result = V1.raptor_parse_chunk(self, nil, 0, 1) # EOF
end
parse_file(file, base_uri: nil, **options, &block) click to toggle source

@param [File, Tempfile, path] file

the input file to parse

@param [Hash{Symbol => Object}] options

any additional options for parsing (see {#parse})

@yield [parser, statement]

each statement in the input

@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 126
def parse_file(file, base_uri: nil, **options, &block)
  self.statement_handler = block if block_given?

  data_url = V1::URI.new("file://#{File.expand_path(file.path)}")
  base_uri = base_uri.to_s.empty? ? nil : V1::URI.new(base_uri.to_s)

  result = V1.raptor_parse_file(self, data_url, base_uri)
  # TODO: error handling if result.nonzero?
end
parse_start!(base_uri = BASE_URI) click to toggle source

@private @param [String] base_uri @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 183
def parse_start!(base_uri = BASE_URI)
  result = V1.raptor_start_parse(self, base_uri)
  # TODO: error handling if result.nonzero?
end
parse_stream(stream, base_uri: nil, **options, &block) click to toggle source

@param [IO, StringIO, readpartial] stream

the input stream to parse

@param [Hash{Symbol => Object}] options

any additional options for parsing (see {#parse})

@yield [parser, statement]

each statement in the input

@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 147
def parse_stream(stream, base_uri: nil, **options, &block)
  self.statement_handler = block if block_given?

  begin
    parse_start!((base_uri || BASE_URI).to_s)
    loop do
      parse_chunk(stream.sysread(BUFFER_SIZE))
    end
  rescue EOFError => e
    parse_end!
  end
end
parse_uri(url, base_uri: nil, **options, &block)
Alias for: parse_url
parse_url(url, base_uri: nil, **options, &block) click to toggle source

@param [RDF::URI, String, to_s] url

the input URL to parse

@param [Hash{Symbol => Object}] options

any additional options for parsing (see {#parse})

@yield [parser, statement]

each statement in the input

@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 104
def parse_url(url, base_uri: nil, **options, &block)
  self.statement_handler = block if block_given?

  data_url = V1::URI.new((url.respond_to?(:to_uri) ? url.to_uri : url).to_s)
  base_uri = base_uri.to_s.empty? ? nil : V1::URI.new(base_uri.to_s)

  result = V1.raptor_parse_uri(self, data_url, base_uri)
  # TODO: error handling if result.nonzero?
end
Also aliased as: parse_uri
statement_handler=(handler) click to toggle source

@param [Proc] handler @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 61
def statement_handler=(handler)
  V1.raptor_parser_set_statement_handler(self, self, handler)
end
warning_handler=(handler) click to toggle source

@param [Proc] handler @return [void]

# File lib/rdf/raptor/ffi/v1/parser.rb, line 54
def warning_handler=(handler)
  V1.raptor_set_warning_handler(self, self, handler)
end