Class: Ragdoll::Api::V1::DocumentsController
- Inherits:
-
BaseController
- Object
- ActionController::API
- BaseController
- Ragdoll::Api::V1::DocumentsController
- Defined in:
- app/controllers/ragdoll/api/v1/documents_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #index ⇒ Object
- #reprocess ⇒ Object
- #show ⇒ Object
- #update ⇒ Object
Instance Method Details
#create ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/controllers/ragdoll/api/v1/documents_controller.rb', line 28 def create begin if params[:file].present? # Handle file upload temp_path = Rails.root.join('tmp', 'uploads', params[:file].original_filename) FileUtils.mkdir_p(File.dirname(temp_path)) File.binwrite(temp_path, params[:file].read) result = ::Ragdoll.add_document(path: temp_path.to_s) if result[:success] && result[:document_id] document = ::Ragdoll::Document.find(result[:document_id]) File.delete(temp_path) if File.exist?(temp_path) render json: { document: document_json(document) }, status: :created else File.delete(temp_path) if File.exist?(temp_path) return render_error(result[:error] || "Failed to create document") end elsif params[:content].present? # Handle text content temp_path = Rails.root.join('tmp', 'uploads', "#{SecureRandom.hex(8)}.txt") FileUtils.mkdir_p(File.dirname(temp_path)) File.write(temp_path, params[:content]) result = ::Ragdoll.add_document(path: temp_path.to_s) if result[:success] && result[:document_id] document = ::Ragdoll::Document.find(result[:document_id]) File.delete(temp_path) if File.exist?(temp_path) render json: { document: document_json(document) }, status: :created else File.delete(temp_path) if File.exist?(temp_path) return render_error(result[:error] || "Failed to create document") end else return render_error("Either file or content must be provided") end rescue => e render_error(e.) end end |
#destroy ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'app/controllers/ragdoll/api/v1/documents_controller.rb', line 83 def destroy begin @document.destroy render_success({}, "Document deleted successfully") rescue => e render_error(e.) end end |
#index ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/controllers/ragdoll/api/v1/documents_controller.rb', line 9 def index documents = ::Ragdoll::Document.all documents = documents.where(status: params[:status]) if params[:status].present? documents = documents.where(document_type: params[:document_type]) if params[:document_type].present? documents = documents.order(created_at: :desc) render json: { documents: documents.map(&method(:document_json)), total: documents.count } end |
#reprocess ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/controllers/ragdoll/api/v1/documents_controller.rb', line 92 def reprocess begin @document..destroy_all @document.update(status: 'pending') ::Ragdoll::GenerateEmbeddingsJob.perform_later(@document.id) render_success({}, "Document reprocessing initiated") rescue => e render_error(e.) end end |
#show ⇒ Object
21 22 23 24 25 26 |
# File 'app/controllers/ragdoll/api/v1/documents_controller.rb', line 21 def show render json: { document: document_json(@document), embeddings: @document..map(&method(:embedding_json)) } end |
#update ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/controllers/ragdoll/api/v1/documents_controller.rb', line 71 def update begin if @document.update(document_params) render json: { document: document_json(@document) } else render_error(@document.errors..join(', ')) end rescue => e render_error(e.) end end |