Skip to content

Instantly share code, notes, and snippets.

@VoronoyAlexandr
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save VoronoyAlexandr/b32ae3699a11ca6b72dc to your computer and use it in GitHub Desktop.

Select an option

Save VoronoyAlexandr/b32ae3699a11ca6b72dc to your computer and use it in GitHub Desktop.
require 'gmail'
require 'pry'
require 'zip'
require 'csv'
require 'active_support/all'
desc "Make coffee"
task :check_homework do
counter = 1
results = []
#Подключение к gmail
gmail = Gmail.connect('[email protected]', 'test')
while counter < gmail.inbox.count(:from => "[email protected]")
#Разбор писем (аргумент :unread для тестирования) с прикрепленными файлами
gmail.inbox.emails(:unread, :from => "[email protected]").each do |email|
begin
puts '='*80
student_grade = []
student_name = email.message.subject.split('.')[0] #=> ["Мананников Сергей", " HW1", " Tasks1"]
# Получаем номера тасков из темы письма
get_task = email.message.subject.split('Tasks ')
get_number_edited_task = get_task[1].split(',')
puts "checking #{student_name}, #{counter}"
student_grade << counter << student_name
FileUtils.mkdir "#{Dir.pwd}/tmp#{counter}"
FileUtils.mkdir "#{Dir.pwd}/tmp#{counter}/sandbox#{counter}"
email.message.attachments.each do |f|
File.write(File.join("#{Dir.pwd}/tmp#{counter}", 'ruby_bursa_task_1.zip'), f.body.decoded)
end
Zip::File.open("#{Dir.pwd}/tmp#{counter}/ruby_bursa_task_1.zip") do |zip_file|
zip_file.each { |f| zip_file.extract(f, File.join("#{Dir.pwd}/tmp#{counter}/sandbox#{counter}", f.name)) }
end
begin
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")
require "./tmp#{counter}/sandbox#{counter}/library_manager.rb" if File.exist?("./tmp#{counter}/sandbox#{counter}/library_manager.rb")
first = 0
if LibraryManager.new.methods.include? :penalty
first += 2
first += 4 if 0 == LibraryManager.new.penalty(1400, DateTime.now.new_offset(0))
first += 4 if (16..17).include? LibraryManager.new.penalty(1400, (DateTime.now.new_offset(0) - 12.hours))
end
second = 0
if LibraryManager.new.methods.include? :could_meet_each_other?
second += 2
second += 4 if !LibraryManager.new.could_meet_each_other?(1234, 1256, 1876, 1955)
second += 4 if LibraryManager.new.could_meet_each_other?(1905, 1967, 1900, 1980)
end
third = 0
if LibraryManager.new.methods.include? :days_to_buy
third += 2
third += 8 if (41..42).include? LibraryManager.new.days_to_buy(123)
end
if third == 0 && LibraryManager.new.methods.include?(:days_to_bye)
first += 2
third += 8 if (41..42).include? LibraryManager.new.days_to_bye(123)
end
fourth = 0
if LibraryManager.new.methods.include? :author_translit
fourth += 2
fourth += 4 if 'Ivan Franko' == LibraryManager.new.author_translit('Іван Франко')
fourth += 4 if 'Marko Vovchok' == LibraryManager.new.author_translit('Марко Вовчок')
end
fifth = 0
if LibraryManager.new.methods.include? :penalty_to_finish
fifth += 2
fifth += 4 if 8 == LibraryManager.new.penalty_to_finish(1400, DateTime.now.new_offset(0) - 1.hours, 100, 50, 10)
fifth += 4 if 0 == LibraryManager.new.penalty_to_finish(1400, DateTime.now.new_offset(0) + 5.hours, 100, 50, 10)
end
Object.send(:remove_const, :LibraryManager)
total = fifth + first + fourth + second + third
student_grade << total << first << second << third << fourth << fifth
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
puts student_grade.to_s
#Если число тасков меньше 5, значит письмо с исправлениями
if get_number_edited_task.count < 5
#Парсинг таблицы заданий
get_table = CSV.parse(File.read("hw_1_grades.csv"))
#счетчики
count = 0
found = 0
#Поиск студента в таблице
get_table.each do |arr|
if arr.index(student_name) != nil
found = count
end
count += 1
end
#Изменение оценок после проверки измененных заданий
get_number_edited_task.each do |digit|
if digit.to_i == 1
get_table[found][3] = first
puts "got task1"
elsif digit.to_i == 2
get_table[found][4] = second
puts "got task2"
elsif digit.to_i == 3
get_table[found][5] = third
puts "got task3"
elsif digit.to_i == 4
get_table[found][6] = fourth
puts "got task4"
elsif digit.to_i == 5
get_table[found][7] = fifth
puts "got task5"
end
#Изменение суммарной оценки за задания
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
end
#Сохранение измененной таблицы
results = get_table
else
#Сохранение нового студента
results << student_grade
end
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
counter += 1
end
CSV.open("hw_1_grades.csv", "w") do |csv|
results.each { |res| csv << res }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment