Advertisement
Guest User

Untitled

a guest
Jul 20th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. require 'gmail'
  2. require 'pry'
  3. require 'zip'
  4. require 'csv'
  5. require 'active_support/all'
  6.  
  7. desc "Make coffee"
  8. task :check_homework do
  9. counter = 1
  10. results = []
  11. #Подключение к gmail
  12. gmail = Gmail.connect('test@gmail.com', 'test')
  13.  
  14.  
  15. while counter < gmail.inbox.count(:from => "test@gmail.com")
  16.  
  17. #Разбор писем (аргумент :unread для тестирования) с прикрепленными файлами
  18. gmail.inbox.emails(:unread, :from => "test@gmail.com").each do |email|
  19. begin
  20. puts '='*80
  21. student_grade = []
  22. student_name = email.message.subject.split('.')[0] #=> ["Мананников Сергей", " HW1", " Tasks1"]
  23.  
  24. # Получаем номера тасков из темы письма
  25. get_task = email.message.subject.split('Tasks ')
  26. get_number_edited_task = get_task[1].split(',')
  27.  
  28.  
  29. puts "checking #{student_name}, #{counter}"
  30. student_grade << counter << student_name
  31. FileUtils.mkdir "#{Dir.pwd}/tmp#{counter}"
  32. FileUtils.mkdir "#{Dir.pwd}/tmp#{counter}/sandbox#{counter}"
  33. email.message.attachments.each do |f|
  34. File.write(File.join("#{Dir.pwd}/tmp#{counter}", 'ruby_bursa_task_1.zip'), f.body.decoded)
  35. end
  36. Zip::File.open("#{Dir.pwd}/tmp#{counter}/ruby_bursa_task_1.zip") do |zip_file|
  37. zip_file.each { |f| zip_file.extract(f, File.join("#{Dir.pwd}/tmp#{counter}/sandbox#{counter}", f.name)) }
  38. end
  39. begin
  40. require "./tmp#{counter}/sandbox#{counter}/ruby_bursa_task_1/library_manager.rb" if File.exist?("./tmp#{counter}/sandbox#{counter}/ruby_bursa_task_1/library_manager.rb")
  41. require "./tmp#{counter}/sandbox#{counter}/library_manager.rb" if File.exist?("./tmp#{counter}/sandbox#{counter}/library_manager.rb")
  42.  
  43.  
  44. first = 0
  45. if LibraryManager.new.methods.include? :penalty
  46. first += 2
  47. first += 4 if 0 == LibraryManager.new.penalty(1400, DateTime.now.new_offset(0))
  48. first += 4 if (16..17).include? LibraryManager.new.penalty(1400, (DateTime.now.new_offset(0) - 12.hours))
  49. end
  50. second = 0
  51. if LibraryManager.new.methods.include? :could_meet_each_other?
  52. second += 2
  53. second += 4 if !LibraryManager.new.could_meet_each_other?(1234, 1256, 1876, 1955)
  54. second += 4 if LibraryManager.new.could_meet_each_other?(1905, 1967, 1900, 1980)
  55. end
  56. third = 0
  57. if LibraryManager.new.methods.include? :days_to_buy
  58. third += 2
  59. third += 8 if (41..42).include? LibraryManager.new.days_to_buy(123)
  60. end
  61. if third == 0 && LibraryManager.new.methods.include?(:days_to_bye)
  62. first += 2
  63. third += 8 if (41..42).include? LibraryManager.new.days_to_bye(123)
  64. end
  65. fourth = 0
  66. if LibraryManager.new.methods.include? :author_translit
  67. fourth += 2
  68. fourth += 4 if 'Ivan Franko' == LibraryManager.new.author_translit('Іван Франко')
  69. fourth += 4 if 'Marko Vovchok' == LibraryManager.new.author_translit('Марко Вовчок')
  70. end
  71. fifth = 0
  72. if LibraryManager.new.methods.include? :penalty_to_finish
  73. fifth += 2
  74. fifth += 4 if 8 == LibraryManager.new.penalty_to_finish(1400, DateTime.now.new_offset(0) - 1.hours, 100, 50, 10)
  75. fifth += 4 if 0 == LibraryManager.new.penalty_to_finish(1400, DateTime.now.new_offset(0) + 5.hours, 100, 50, 10)
  76. end
  77. Object.send(:remove_const, :LibraryManager)
  78.  
  79.  
  80. total = fifth + first + fourth + second + third
  81. student_grade << total << first << second << third << fourth << fifth
  82.  
  83.  
  84. rescue Exception => e
  85. puts e.message
  86. puts e.backtrace.inspect
  87. end
  88. puts student_grade.to_s
  89.  
  90. #Если число тасков меньше 5, значит письмо с исправлениями
  91. if get_number_edited_task.count < 5
  92. #Парсинг таблицы заданий
  93. get_table = CSV.parse(File.read("hw_1_grades.csv"))
  94.  
  95. #счетчики
  96. count = 0
  97. found = 0
  98.  
  99. #Поиск студента в таблице
  100. get_table.each do |arr|
  101.  
  102. if arr.index(student_name) != nil
  103. found = count
  104. end
  105. count += 1
  106. end
  107.  
  108. #Изменение оценок после проверки измененных заданий
  109. get_number_edited_task.each do |digit|
  110.  
  111. if digit.to_i == 1
  112. get_table[found][3] = first
  113. puts "got task1"
  114. elsif digit.to_i == 2
  115. get_table[found][4] = second
  116. puts "got task2"
  117. elsif digit.to_i == 3
  118. get_table[found][5] = third
  119. puts "got task3"
  120. elsif digit.to_i == 4
  121. get_table[found][6] = fourth
  122. puts "got task4"
  123. elsif digit.to_i == 5
  124. get_table[found][7] = fifth
  125. puts "got task5"
  126. end
  127.  
  128. #Изменение суммарной оценки за задания
  129. get_table[found][2] = get_table[found][3].to_i + get_table[found][4].to_i + get_table[found][5].to_i + get_table[found][6].to_i + get_table[found][7].to_i
  130.  
  131. end
  132. #Сохранение измененной таблицы
  133. results = get_table
  134.  
  135. else
  136. #Сохранение нового студента
  137. results << student_grade
  138.  
  139. end
  140.  
  141.  
  142. rescue Exception => e
  143. puts e.message
  144. puts e.backtrace.inspect
  145. end
  146. counter += 1
  147. end
  148.  
  149.  
  150. CSV.open("hw_1_grades.csv", "w") do |csv|
  151. results.each { |res| csv << res }
  152. end
  153. end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement