Import CSV from url Errno::ENAMETOOLONG: file name too long

I’m trying to import a CSV file from a url but i’m getting Errno::ENAMETOOLONG: file name too long. I process the file as follow:

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

SmarterCSV.process(url_data, {
  ...
})

What am i missing ?

  • No route matches “/sessions/user”
  • Attributes not showing on users#show page
  • How do you keep secrets.yml secret in rails?
  • link_to Show page takes me to the index page Rails?
  • Rails 4.2 - dependent: :restrict_with_error - access errors
  • Rails 4 - update form without submit (gem)
  • Status of Rails' link_to_function deprecation?
  • Devise Views with Multiple Models
  • 3 Solutions collect form web for “Import CSV from url Errno::ENAMETOOLONG: file name too long”

    You have to pass a filename which should be on server. rightnow you are passing all 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',{ })
    

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

    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
    

    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

    Ruby is the best programming language in the world - Ruby on Rails.