require "rufus-scheduler" DEFAULT_LESSON_PLAN = [ [15, "login and catch up on the week"], [45, "review chapter vocab and grammar"], [15, "answer Seth Clydesdale Genki review questions"], [15, "ask closing questions and have Japanese conversation"], ] class Lesson attr_reader :lesson_plan, :channel, :thread, :running, :scheduler def initialize(lesson_plan=DEFAULT_LESSON_PLAN, scheduler: Rufus::Scheduler.new) @lesson_plan = lesson_plan @running = false @scheduler = scheduler end def stop! @scheduler.shutdown end def start!(channel) lesson_plan.each_with_index do |(time, message), index| offset = lesson_plan.map(&:first)[0..index - 1].sum(0) offset = 0 if index == 0 scheduler.in "#{offset}m" do channel.send_message "#{time} minutes to #{message}" end end end_offset = lesson_plan.map(&:first).sum(0) scheduler.in "#{end_offset}m" do channel.send_message "Lesson over!" @running = false end @running = true end def running? running end end