Class: RemindMePlugin

Inherits:
Campfire::PollingBot::Plugin show all
Defined in:
lib/campfire/polling_bot/plugins/remind_me/remind_me_plugin.rb

Constant Summary collapse

NUMBER_WORDS =
%w{zero one two three four five six seven eight nine ten}
PRONOUN_SUBS =
[
  ["I'm", "you're"],
  ['my', 'your'],
  ['I', 'you'],
]

Constants inherited from Campfire::PollingBot::Plugin

Campfire::PollingBot::Plugin::HALT

Instance Attribute Summary

Attributes inherited from Campfire::PollingBot::Plugin

#bot, #config

Instance Method Summary collapse

Methods inherited from Campfire::PollingBot::Plugin

accepts, accepts?, #accepts?, bot, bot=, directory, directory=, inherited, #initialize, load_all, load_plugin_classes, logger, #logger, priority, #priority, requires_config, requires_config?, #requires_config?, setup_database, subclasses, #to_s

Constructor Details

This class inherits a constructor from Campfire::PollingBot::Plugin

Instance Method Details

#heartbeatObject


79
80
81
82
83
84
85
86
87
88
# File 'lib/campfire/polling_bot/plugins/remind_me/remind_me_plugin.rb', line 79

def heartbeat
  # check reminders every 9 seconds (heartbeat is called every 3 sec)
  @heartbeat_counter ||= 0
  @heartbeat_counter += 1
  return unless (@heartbeat_counter % 3) == 1
  due_reminders.each do |reminder|
    bot.say("#{reminder.person}, I'm reminding you to #{reminder.action}")
    reminder.destroy!
  end
end

#helpObject

return array of available commands and descriptions


91
92
93
94
95
96
97
# File 'lib/campfire/polling_bot/plugins/remind_me/remind_me_plugin.rb', line 91

def help
  [['remind (me|<person>) [in] <time string> to <message>', "set up a reminder"],
   ['remind (me|<person>) to <message> (in|on|at|next|this) <time string>', "set up a reminder"],
   ["[list|show] [person]['s] reminders", "display current reminders for yourself or person"],
   ["delete reminder <n>", "delete your reminder #n"],
  ]
end

#process(message) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/campfire/polling_bot/plugins/remind_me/remind_me_plugin.rb', line 14

def process(message)
  case message.command
  when /remind\s+(.*?)\s+(in\s+(\S+)\s+(?:seconds|minutes|hours|days|weeks|years))\s+to\s+(.*)/i
    reminder_person = $1
    time_string = $2
    time_value = $3
    action = $4
    if i = NUMBER_WORDS.index(time_value.downcase)    
      time_string.sub!(time_value, i.to_s)     
    end
  when /remind\s+(.*?)\s+to\s+(.*?)\s+((?:in|on|at|next|this).*)/i
    reminder_person = $1
    action = $2
    time_string = $3
  when /remind\s+(.*?)\s+(.*?)\s+to\s+(.*)/i
    reminder_person = $1
    time_string = $2
    action = $3
  when /(?:list|show) (\S+) reminders/i, /(?:list|show) reminders for (\S+)/i
    list_reminders(message.person, $1)
    return HALT
  when /(list|show) reminders$/
    list_reminders(message.person, message.person)
    return HALT
  when /delete reminder (?:#\s*)?(\d+)/
    delete_reminder(message.person, $1.to_i)
    return HALT
  else
    return
  end

  PRONOUN_SUBS.each do |sub|
    action.gsub!(/\b#{sub[0]}\b/i, sub[1])
  end
  
  begin
    time = Chronic.parse(time_string.sub(/^at/, ''))
  rescue RangeError
    bot.say("Whatever, #{message.person}.")
    return HALT
  end
  
  if !time || time.kind_of?(Chronic::Span)
    bot.say("sorry, #{message.person}, but I don't know what '#{time_string}' means.")
    return HALT
  end
  
  if reminder_person.downcase == 'me'
    reminder_person = message.person      
  else
    reminder_person.gsub!(/^(\w)/) {$1.upcase}
  end
  
  Reminder.create(:person => reminder_person, :action => action, :reminder_time => time)
  time_now = Time.now
  if [time.year,time.month,time.day] == [time_now.year,time_now.month,time_now.day]
    converted_time_string = time.strftime('%I:%M %p').gsub(/^0/,'')
  else
    converted_time_string = time.strftime('%c')
  end
  
  bot.say("ok, #{message.person}, I will remind #{reminder_person == message.person ? 'you' : reminder_person} #{time_string} (#{converted_time_string}) to #{action}")
  return HALT
end