Import CSV from urr Errno :: ENAMETOOLONG: file name is too long

I am trying to import a CSV file from a URL, but I get Errno::ENAMETOOLONG: file name too long . I process the file as follows:

 require 'open-uri' url = "http://de65.grepolis.com/data/csv.txt" url_data = open(url).read() SmarterCSV.process(url_data, { ... }) 

What am I missing?

+4
source share
3 answers

You need to pass the file name, which should be on the server. right now you are transferring all the data. Do something like this

 require 'open-uri' url = "http://de65.grepolis.com/data/csv.txt" url_data = open(url).read() File.open('/tmp/file_name', 'w') { |file| file.write(url_data) } SmarterCSV.process('/tmp/file_name',{ }) 
+6
source

I had the same problem using the standard CSV library to pull out a CSV file via http url. I was able to solve the problem without having to write to a temporary server file with this code:

 require 'open-uri' require 'csv' url = "http://de65.grepolis.com/data/csv.txt" url_data = open(url).read() CSV.parse(url_data, headers: true).each do |row| # per row processing code ... end 
+4
source

Hope this helps you.

 # models/concerns/import.rb require 'open-uri' require 'import_error' module Import extend ActiveSupport::Concern class_methods do def import_remote(url) csv = CSV.parse(open(url), headers: true) begin ActiveRecord::Base.transaction do counter = 0 csv.each do |row| row_hash = row.to_hash begin instance = self.name.constantize.create!(row_hash) rescue => e raise ImportError.new("#{e.message}. at row: #{row_hash}") end counter += 1 if instance.persisted? end end rescue => e return puts e.message end puts "Imported #{counter} records" end end end # lib/tasks/import.rake namespace :remote_import do desc "Import companies from CSV" task :your_model, [:url] do |t, args| YourModel.import_remote(args.url) end end # lib/import_error.rb class ImportError < StandardError end # models/your_model.rb class YourModel < ActiveRecord::Base include Import end 

Gist: https://gist.github.com/victorhazbun87/9ac786961bbf7c235f76

0
source

All Articles