Class: RDF::Raptor::FFI::V1::Parser
- Inherits:
-
FFI::ManagedStruct
- Object
- FFI::ManagedStruct
- RDF::Raptor::FFI::V1::Parser
- Includes:
- RDF::Raptor::FFI
- Defined in:
- lib/rdf/raptor/ffi/v1/parser.rb
Overview
This class provides the functionality of turning syntaxes into RDF triples - RDF parsing.
Constant Summary collapse
- BASE_URI =
The default base URI
'file:///dev/stdin'
- BUFFER_SIZE =
The maximum chunk size for
#parse_stream
64 * 1024
Constants included from RDF::Raptor::FFI
Class Method Summary collapse
-
.release(ptr)
Releases
libraptor
memory associated with this structure.
Instance Method Summary collapse
- #error_handler=(handler)
-
#initialize(ptr_or_name) ⇒ Parser
constructor
A new instance of Parser.
- #parse(input, **options) {|parser, statement| ... }
- #parse_buffer(buffer, base_uri: nil, **options) {|parser, statement| ... }
- #parse_file(file, base_uri: nil, **options) {|parser, statement| ... }
- #parse_stream(stream, base_uri: nil, **options) {|parser, statement| ... }
- #parse_url(url, base_uri: nil, **options) {|parser, statement| ... } (also: #parse_uri)
- #statement_handler=(handler)
- #warning_handler=(handler)
Methods included from RDF::Raptor::FFI
Constructor Details
#initialize(ptr) ⇒ Parser #initialize(name) ⇒ Parser
Returns a new instance of Parser.
24 25 26 27 28 29 30 31 32 33 |
# 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 |
Class Method Details
.release(ptr)
This method returns an undefined value.
Releases libraptor
memory associated with this structure.
40 41 42 |
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 40 def self.release(ptr) V1.raptor_free_parser(ptr) end |
Instance Method Details
#error_handler=(handler)
This method returns an undefined value.
47 48 49 |
# 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) {|parser, statement| ... }
This method returns an undefined value.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 78 def parse(input, **, &block) case input when RDF::URI, %r(^(file|https|http|ftp)://) parse_url(input, **, &block) when File, Tempfile parse_file(input, **, &block) when IO, StringIO parse_stream(input, **, &block) when String parse_buffer(input, **, &block) else raise ArgumentError, "don't know how to parse #{input.inspect}" end end |
#parse_buffer(buffer, base_uri: nil, **options) {|parser, statement| ... }
This method returns an undefined value.
171 172 173 174 175 176 177 |
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 171 def parse_buffer(buffer, base_uri: nil, **, &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_file(file, base_uri: nil, **options) {|parser, statement| ... }
This method returns an undefined value.
126 127 128 129 130 131 132 133 134 |
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 126 def parse_file(file, base_uri: nil, **, &block) self.statement_handler = block if block_given? data_url = V1::URI.new("file://#{File.(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_stream(stream, base_uri: nil, **options) {|parser, statement| ... }
This method returns an undefined value.
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 147 def parse_stream(stream, base_uri: nil, **, &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_url(url, base_uri: nil, **options) {|parser, statement| ... } Also known as: parse_uri
This method returns an undefined value.
104 105 106 107 108 109 110 111 112 |
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 104 def parse_url(url, base_uri: nil, **, &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 |
#statement_handler=(handler)
This method returns an undefined value.
61 62 63 |
# 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)
This method returns an undefined value.
54 55 56 |
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 54 def warning_handler=(handler) V1.raptor_set_warning_handler(self, self, handler) end |