class Ruboty::Handlers::Reminder

Constants

DAY_RANGE
HOUR_RANGE
MIN_RANGE
MONTH_RANGE
YEAR_RANGE

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/ruboty/handlers/reminder.rb, line 14
def initialize(*args)
  super(*args)
  restart
end

Public Instance Methods

add(message) click to toggle source
# File lib/ruboty/handlers/reminder.rb, line 19
def add(message)
  year, month, day =
    # [year, month, day] can be omitted.
    if message[:year] && message[:month] && message[:day]
      [message[:year].to_i, message[:month].to_i, message[:day].to_i]
    else
      now_time = Time.now
      [now_time.year, now_time.month, now_time.day]
    end
  hour = message[:hour].to_i
  min = message[:min].to_i

  unless valid_time?(year, month, day, hour, min)
    message.reply('Invalid time.')
    return
  end

  target_unixtime = Time.new(year, month, day, hour, min, 0).to_i

  if past?(target_unixtime)
    message.reply('Already past.')
    return
  end

  task = Ruboty::Reminder::Task.new(
    message.original.except(:robot).merge(
      id: generate_id,
      body: message[:reminder],
      year: year,
      month: month,
      day: day,
      hour: hour,
      min: min,
      unixtime: target_unixtime
    )
  )
  task.start(robot)
  message.reply("Reminder #{task.hash[:id]} is created.")

  reminders[task.hash[:id]] = task.hash
  reminder_threads[task.hash[:id]] = task.thread
end
delete(message) click to toggle source
# File lib/ruboty/handlers/reminder.rb, line 62
def delete(message)
  if reminders.delete(message[:id].to_i)
    reminder_threads[message[:id].to_i].kill
    message.reply("Reminder #{message[:id]} is deleted.")
  else
    message.reply("Reminder #{message[:id]} is not found.")
  end
end
generate_id() click to toggle source
# File lib/ruboty/handlers/reminder.rb, line 101
def generate_id
  loop do
    id = rand(1000)
    return id unless reminders.has_key?(id)
  end
end
list(message) click to toggle source
# File lib/ruboty/handlers/reminder.rb, line 71
def list(message)
  if reminders.empty?
    message.reply("The reminder doesn't exist.")
  else
    sorted_reminders = reminders.sort_by {|_id, hash| hash[:unixtime]}

    reminder_list = sorted_reminders.map do |id, hash|
      date = "#{hash[:year]}/#{'%02d' % hash[:month]}/#{'%02d' % hash[:day]}"
      time = "#{'%02d' % hash[:hour]}:#{'%02d' % hash[:min]}"
      "#{id}: #{date} #{time} -> #{hash[:body]}"
    end
    message.reply(reminder_list.join("\n"), code: true)
  end
end
past?(unixtime) click to toggle source
# File lib/ruboty/handlers/reminder.rb, line 116
def past?(unixtime)
  (unixtime - Time.now.to_i) < 0
end
reminder_threads() click to toggle source
# File lib/ruboty/handlers/reminder.rb, line 97
def reminder_threads
  @temporary_store ||= {}
end
reminders() click to toggle source
# File lib/ruboty/handlers/reminder.rb, line 93
def reminders
  robot.brain.data[Ruboty::Reminder::NAMESPACE] ||= {}
end
restart() click to toggle source
# File lib/ruboty/handlers/reminder.rb, line 86
def restart
  reminders.each do |id, hash|
    task = Ruboty::Reminder::Task.new(hash)
    task.start(robot)
  end
end
valid_time?(year, month, day, hour, min) click to toggle source
# File lib/ruboty/handlers/reminder.rb, line 108
def valid_time?(year, month, day, hour, min)
  YEAR_RANGE.include?(year) &&\
    MONTH_RANGE.include?(month) &&\
    DAY_RANGE.include?(day) &&\
    HOUR_RANGE.include?(hour) &&\
    MIN_RANGE.include?(min)
end