Class: SwarmSDK::V3::Tools::DocumentConverters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/v3/tools/document_converters/base.rb

Overview

Abstract base class for document converters

Provides common interface and helpers for converting documents to text. Each converter checks gem availability and provides clear error messages.

Direct Known Subclasses

DocxConverter, PdfConverter, XlsxConverter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available?Boolean

Check if required gem is available

Returns:

  • (Boolean)

    true if gem is installed



40
41
42
43
44
45
# File 'lib/swarm_sdk/v3/tools/document_converters/base.rb', line 40

def available?
  Gem::Specification.find_by_name(gem_name)
  true
rescue Gem::MissingSpecError
  false
end

.extensionsArray<String>

File extensions this converter handles

Returns:

  • (Array<String>)

    array of extensions including dot (e.g., [“.pdf”])

Raises:

  • (NotImplementedError)

    if not implemented by subclass



33
34
35
# File 'lib/swarm_sdk/v3/tools/document_converters/base.rb', line 33

def extensions
  raise NotImplementedError, "#{name} must implement .extensions"
end

.format_nameString

Human-readable format name

Returns:

  • (String)

    the format name (e.g., “PDF”, “DOCX”)

Raises:

  • (NotImplementedError)

    if not implemented by subclass



25
26
27
# File 'lib/swarm_sdk/v3/tools/document_converters/base.rb', line 25

def format_name
  raise NotImplementedError, "#{name} must implement .format_name"
end

.gem_nameString

Gem name required for this converter

Returns:

  • (String)

    the gem name

Raises:

  • (NotImplementedError)

    if not implemented by subclass



17
18
19
# File 'lib/swarm_sdk/v3/tools/document_converters/base.rb', line 17

def gem_name
  raise NotImplementedError, "#{name} must implement .gem_name"
end

Instance Method Details

#convert(file_path) ⇒ String, RubyLLM::Content

Convert document to text (possibly with image attachments)

Parameters:

  • file_path (String)

    absolute path to document

Returns:

  • (String, RubyLLM::Content)

    text or text with image attachments

Raises:

  • (NotImplementedError)

    if not implemented by subclass



53
54
55
# File 'lib/swarm_sdk/v3/tools/document_converters/base.rb', line 53

def convert(file_path)
  raise NotImplementedError, "#{self.class.name} must implement #convert"
end