Class: ExportJsonService

Inherits:
Object
  • Object
show all
Defined in:
app/services/export_json_service.rb

Overview

ExportJsonService

Class Method Summary collapse

Class Method Details

.call(document_ids) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/export_json_service.rb', line 7

def self.call(document_ids)
  ActionCable.server.broadcast("export_channel", {progress: 0})

  document_ids = document_ids.flatten
  total = document_ids.size
  count = 0
  slice_count = 100
  documents = []

  Rails.logger.debug { "\n\nExportJsonService: #{document_ids.inspect}\n\n" }

  document_ids.each_slice(slice_count) do |slice|
    # Broadcast progress percentage
    count += slice_count
    progress = ((count.to_f / total) * 100).round
    progress = 100 if progress > 100

    ActionCable.server.broadcast("export_channel", {progress: progress})
    slice.each do |doc_id|
      doc = Document.find_by(friendlier_id: doc_id)
      documents << doc
    rescue NoMethodError
      Rails.logger.debug { "\n\nExport Failed: #{doc_id.inspect}\n\n" }
    end
  end

  documents
end