Need to import csv data inside a zip file to my product model using activerecord-import and rubyzip gem.
This code works (download the zip and display the csv name)
desc "Import products data from web"
task import_product: :environment do
url = "https://example.com"
dir = "db/example_zip.zip"
File.open(dir, "wb") do |f|
f.write HTTParty.get(url).body
end
Zip::File.open(dir) do |zip|
zip.each do |entry|
entry.name
end
end
end
In the "zip.each loop" I tried this :
items = []
CSV.foreach(entry, headers: true) do |row|
items << Item.new(row.to_h)
end
Item.import(items)
I have the following error TypeError: no implicit conversion of Zip::Entry into String
According this tutorial: https://mattboldt.com/importing-massive-data-into-rails/
What is the best way to refresh my product model data with this csv? Do I have to read the file into memory (entry.get_input_stream.read) or save the file then import it?
Thanks for your help