Class: Pageflow::FileImportJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/pageflow/file_import_job.rb

Overview

Import the list of file(s), It includes fetching of the file data from its source and uploading the file to s3

Instance Method Summary collapse

Instance Method Details

#perform(import_model_id, credentials) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'app/jobs/pageflow/file_import_job.rb', line 10

def perform(import_model_id, credentials)
  import_model = FileImport.find(import_model_id)
  file_record = import_model.file
  download_options = JSON.parse import_model.download_options
  file_source = import_model.file_importer.download_file credentials, download_options
  temp_file = save_to_tempfile(file_source)
  upload_attachment file_record, temp_file
  file_record.publish!
ensure
  temp_file.delete
end

#save_to_tempfile(data) ⇒ Object



29
30
31
32
33
34
35
# File 'app/jobs/pageflow/file_import_job.rb', line 29

def save_to_tempfile(data)
  file = Tempfile.new('foo')
  file.binmode
  file.write(data)
  file.flush
  file
end

#upload_attachment(file_record, temp_file) ⇒ Object



22
23
24
25
26
27
# File 'app/jobs/pageflow/file_import_job.rb', line 22

def upload_attachment(file_record, temp_file)
  attachment = file_record.attachment
  attachment.assign(temp_file)
  file_record.restore_attributes
  attachment.flush_writes
end