summaryrefslogtreecommitdiff
path: root/lib/authors_importer.rb
blob: e0b16bdca3a3e915df58c1eb5b156ec35a68bda6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require 'csv'
require 'digest/sha1'

class AuthorsImporter
  
  def initialize path
    @parsed_file = CSV::Reader.parse(File.open(path, "r")) if File.exist? path
  end
  
  def import_authors
    parse_csv
  end
  
  def parse_csv
    if @parsed_file
      @parsed_file.each do |row|
        password = generate_password
                
        options = {
          :login                  => row[0],
          :full_name              => row[1],
          :email                  => row[2],
          :password               => password,
          :password_confirmation  => password
        }
        
        find_or_create_user options
      end
    end
  end
  
  def find_or_create_user options = {}
    puts options[:login]
    
    unless User.find_by_email(options[:email])
      
      User.create! options
    end
  end
  
  def generate_password
    Digest::SHA1.hexdigest("#{Time.now+rand(10000).days}")
  end
end