Class: Ragdoll::DocumentConverter
- Inherits:
-
Object
- Object
- Ragdoll::DocumentConverter
- Defined in:
- app/services/ragdoll/document_converter.rb
Defined Under Namespace
Classes: ConversionError
Class Method Summary collapse
Instance Method Summary collapse
- #convert_to_text(file_path, document_type = nil) ⇒ Object
- #determine_document_type(file_path) ⇒ Object
-
#initialize(**options) ⇒ DocumentConverter
constructor
A new instance of DocumentConverter.
- #supported_formats ⇒ Object
Constructor Details
#initialize(**options) ⇒ DocumentConverter
Returns a new instance of DocumentConverter.
11 12 13 |
# File 'app/services/ragdoll/document_converter.rb', line 11 def initialize(**) @options = end |
Class Method Details
.convert_to_text(file_path, document_type = nil, **options) ⇒ Object
7 8 9 |
# File 'app/services/ragdoll/document_converter.rb', line 7 def self.convert_to_text(file_path, document_type = nil, **) new(**).convert_to_text(file_path, document_type) end |
Instance Method Details
#convert_to_text(file_path, document_type = nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/services/ragdoll/document_converter.rb', line 15 def convert_to_text(file_path, document_type = nil) return "" unless File.exist?(file_path) document_type ||= determine_document_type(file_path) begin case document_type when "text", "markdown", "html", "pdf", "docx", "csv", "json", "xml", "yaml" convert_text_based_document(file_path, document_type) when "image" convert_image_to_text(file_path) when "audio" convert_audio_to_text(file_path) when "video" convert_video_to_text(file_path) else convert_unknown_document(file_path) end rescue StandardError => e puts "Warning: Document conversion failed for #{file_path}: #{e.}" generate_fallback_text(file_path, document_type) end end |
#determine_document_type(file_path) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/services/ragdoll/document_converter.rb', line 39 def determine_document_type(file_path) extension = File.extname(file_path).downcase case extension when ".pdf" then "pdf" when ".docx" then "docx" when ".txt" then "text" when ".md", ".markdown" then "markdown" when ".html", ".htm" then "html" when ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg", ".ico", ".tiff", ".tif" "image" when ".mp3", ".wav", ".m4a", ".flac", ".ogg", ".aac", ".wma" "audio" when ".mp4", ".mov", ".avi", ".webm", ".mkv" "video" when ".csv" then "csv" when ".json" then "json" when ".xml" then "xml" when ".yml", ".yaml" then "yaml" else "text" # Default to text for unknown extensions end end |
#supported_formats ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'app/services/ragdoll/document_converter.rb', line 63 def supported_formats { text: %w[.txt .md .markdown .html .htm .csv .json .xml .yml .yaml], documents: %w[.pdf .docx], images: %w[.jpg .jpeg .png .gif .bmp .webp .svg .ico .tiff .tif], audio: %w[.mp3 .wav .m4a .flac .ogg .aac .wma], video: %w[.mp4 .mov .avi .webm .mkv] } end |