Reputation: 77
I'm building a CRON JOB in order to create a CSV file once a month and send it to an API
My method bellow generates a csv file in /tmp folder
def save_csv_to_tmp
f = Tempfile.create(["nb_administrateurs_par_mois_#{date_last_month}", '.csv'], 'tmp')
f << generate_csv
f.close
end
Now, in the perform method, I have to retreive this csv file but I do not know how to do it :
def perform(*args)
# creates the csv file in tmp folder
save_csv_to_tmp
# TODO : retreive this csv file and send it to the API
end
Upvotes: 0
Views: 341
Reputation: 291
You can do something like this (Using the block version of Tempfile#create
, it will automatically close and unlink the temporary file):
def save_csv_to_tmp
Tempfile.create(["nb_administrateurs_par_mois_#{date_last_month}", '.csv'], 'tmp'] do |f|
f << generate_csv
# You might need to call `f.rewind` here
# so that your service object can use the file object right away
yield f if block_given?
end
end
And in perform
def perform(*args)
save_csv_to_tmp do |file|
# Send file to API
SendFileService.new(file: file).call # Or something similar
end
end
Upvotes: 1