Class: RSlack::CronAction
Instance Attribute Summary
Attributes inherited from Action
Instance Method Summary collapse
- #add(msg) ⇒ Object
- #del(msg) ⇒ Object
-
#initialize(bot) ⇒ CronAction
constructor
A new instance of CronAction.
- #list(msg) ⇒ Object
- #start(msg) ⇒ Object
- #stop ⇒ Object
Methods inherited from Action
Constructor Details
#initialize(bot) ⇒ CronAction
Returns a new instance of CronAction.
20 21 22 23 24 |
# File 'lib/rslack/actions/cron.rb', line 20 def initialize(bot) @timer = nil @channel = {} super(bot) end |
Instance Method Details
#add(msg) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/rslack/actions/cron.rb', line 26 def add(msg) start(msg.dup) cron_id = unique_id memory.update(cron_id => msg.dup) bot.reply(msg, text: "_added schedule_ `[#{cron_id}] #{msgtostr(msg)}`") end |
#del(msg) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/rslack/actions/cron.rb', line 35 def del(msg) stop cron_id = msg.var.id.to_i deleted = memory.delete(cron_id) raise "no such cron id: #{cron_id}" unless deleted bot.reply(msg, text: "_deleted schedule_ `[#{cron_id}] #{msgtostr(deleted)}`") end |
#list(msg) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rslack/actions/cron.rb', line 45 def list(msg) items = memory.select { |_, item| msg.channel == item.channel } unless items.empty? content = StringIO.new content.puts "```" content.puts items.map { |cron_id, item| "[#{cron_id}] #{msgtostr(item)}" } content.puts "```" bot.reply(msg, text: content.string) else bot.reply(msg, text: "_no schedules found_.") end end |
#start(msg) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rslack/actions/cron.rb', line 60 def start(msg) parser = CronParser.new(msg.var.syntax) sleep = parser.next(Time.now) - Time.now @timer = EM::Timer.new(sleep) do item = OpenStruct.new(msg.to_h.merge(text: msg.var.command)) item.delete_field(:var) bot.react(item) start(msg) end end |
#stop ⇒ Object
72 73 74 |
# File 'lib/rslack/actions/cron.rb', line 72 def stop @timer.cancel if @timer end |