Home > Enterprise >  Rails 6 : How to retreive a csv file in /tmp in order to send via API
Rails 6 : How to retreive a csv file in /tmp in order to send via API

Time:06-28

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

CodePudding user response:

You can do something like this:

  def save_csv_to_tmp
    f = Tempfile.create(["nb_administrateurs_par_mois_#{date_last_month}", '.csv'], 'tmp']
    f << generate_csv
    yield f if block_given?
    f.close
  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

  • Related