This commit is contained in:
26
docs/snippets/ruby/cli/basic_cli.rb
Normal file
26
docs/snippets/ruby/cli/basic_cli.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
```ruby title="basic_cli.rb"
|
||||
require 'json'
|
||||
require 'open3'
|
||||
|
||||
def extract_with_cli(file_path, output_format = 'text')
|
||||
stdout, stderr, status = Open3.capture3(
|
||||
'kreuzberg', 'extract', file_path, '--format', output_format
|
||||
)
|
||||
|
||||
unless status.success?
|
||||
warn "Error: #{stderr}"
|
||||
exit 1
|
||||
end
|
||||
|
||||
return JSON.parse(stdout) if output_format == 'json'
|
||||
stdout
|
||||
end
|
||||
|
||||
document = 'document.pdf'
|
||||
|
||||
text_output = extract_with_cli(document, 'text')
|
||||
puts "Extracted: #{text_output.length} characters"
|
||||
|
||||
json_output = extract_with_cli(document, 'json')
|
||||
puts "Format: #{json_output['format']}"
|
||||
```
|
||||
27
docs/snippets/ruby/cli/cli_with_config.rb
Normal file
27
docs/snippets/ruby/cli/cli_with_config.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
```ruby title="cli_with_config.rb"
|
||||
require 'json'
|
||||
require 'open3'
|
||||
|
||||
def extract_with_config(file_path, config_path)
|
||||
stdout, stderr, status = Open3.capture3(
|
||||
'kreuzberg', 'extract', file_path, '--config', config_path, '--format', 'json'
|
||||
)
|
||||
|
||||
unless status.success?
|
||||
warn "Error: #{stderr}"
|
||||
exit 1
|
||||
end
|
||||
|
||||
JSON.parse(stdout)
|
||||
end
|
||||
|
||||
config_file = 'kreuzberg.toml'
|
||||
document = 'document.pdf'
|
||||
|
||||
puts "Extracting #{document} with config #{config_file}"
|
||||
result = extract_with_config(document, config_file)
|
||||
|
||||
puts "Content length: #{result['content'].length}"
|
||||
puts "Format: #{result['format']}"
|
||||
puts "Languages: #{result['languages'].join(', ')}"
|
||||
```
|
||||
Reference in New Issue
Block a user