title: importing data from csv into rails application
body: <blockquote>
    <p>I am trying to import data from CSV into the database using Classes so that I can easily write Test Case for the csv import rake task I created</p>
  </blockquote>
  
  <p>However, <strong>my solution does not work</strong>. </p>
  
  <p>And I also feel:</p>
  
  <ol>
  <li>It doesn't make sense </li>
  <li>Aside feeling its not a good solution that connotes Ruby mastery, it doesn't work.</li>
  </ol>
  
  <p>Here is what I came up with in my <strong>engines/csv_importer/lib/tasks/csv_import.rake</strong></p>
  
  <pre><code>require 'open-uri'
  require 'csv'
  
  namespace :csv_import do
    desc 'Import users from csv'
  
    task users: :environment do
      WebImport.new(url: 'http://blablabla.com/details/people.csv').call.answers
    end
  end
  
  class WebImport
    def initialize(url)
      @csv_string = url
    end
  
    def call
      CSV.parse(@csv_string, headers: true, header_converters: :symbol) do |row|
        next unless row[:name].present? &amp;&amp; row[:email_address].present?
      end
      CsvImporter::User.create row.to_h
    end
  
    def self.answers
      user = []
      counter = 0
      duplicate_counter = 0
      user.persisted? ? counter + 1 : duplicate_counter + 1
      p "Email duplicate record: #{user.email_address} - #{user.errors.full_messages.join(',')}" if user.errors.any?
      p "Imported #{counter} users, #{duplicate_counter} duplicate rows ain't added in total"
    end
  
  end
  </code></pre>
  
  <p><strong>Error when I run rake csv_import:users</strong></p>
  
  <pre><code>$ rake csv_import:users
  rake aborted!
  NoMethodError: private method `gets' called for {:url=&gt;"http://blablabla.com/details/people.csv"}:Hash
  </code></pre>
  
  <p>How do I <strong>make this work</strong> <em>and</em> <strong>write TEST for this</strong> at the long run?</p>