diff options
Diffstat (limited to 'lib/authors_importer.rb')
| -rw-r--r-- | lib/authors_importer.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/authors_importer.rb b/lib/authors_importer.rb new file mode 100644 index 0000000..b0d9741 --- /dev/null +++ b/lib/authors_importer.rb | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | require 'csv' | ||
| 2 | |||
| 3 | |||
| 4 | class AuthorsImporter | ||
| 5 | |||
| 6 | def initialize path | ||
| 7 | if File.exists? path | ||
| 8 | @parsed_file = CSV::Reader.parse(File.open(path, "r")) | ||
| 9 | else | ||
| 10 | raise "File does not exist" | ||
| 11 | end | ||
| 12 | end | ||
| 13 | |||
| 14 | def import_authors | ||
| 15 | @parsed_file.each do |row| | ||
| 16 | password = generate_password | ||
| 17 | |||
| 18 | options = { | ||
| 19 | :login => row[0], | ||
| 20 | :full_name => row[1], | ||
| 21 | :email => row[2], | ||
| 22 | :password => password, | ||
| 23 | :password_confirmation => password | ||
| 24 | } | ||
| 25 | |||
| 26 | unless user = User.find_by_email(options[:email]) | ||
| 27 | User.create options | ||
| 28 | end | ||
| 29 | end | ||
| 30 | end | ||
| 31 | |||
| 32 | def generate_password | ||
| 33 | Digest::SHA1.hexdigest("#{Time.now+rand(10000).days}") | ||
| 34 | end | ||
| 35 | end \ No newline at end of file | ||
