summaryrefslogtreecommitdiff
path: root/lib/authors_importer.rb
blob: 61951118400ea3e46e4c08030707acd73e2bf3dc (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'csv'


class AuthorsImporter
  
  def initialize path
    @parsed_file = CSV::Reader.parse(File.open(path, "r")) if File.exists? path
  end
  
  def import_authors
    parse_csv || find_or_create_user
  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
      
      return true
    end
  end
  
  def find_or_create_user options = {}
    password = generate_password
    
    # default_options = {
    #   :login => "webmaster",
    #   :full_name => "Webmaster",
    #   :email => "webmaster@ccc.de",
    #   :password => password,
    #   :password_confirmation => password
    # }
    # default_options = {}
    # 
    # options = default_options.merge(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