Class: WeChat::Bot::Handler
- Inherits:
-
Object
- Object
- WeChat::Bot::Handler
- Defined in:
- lib/wechat/bot/handler.rb
Overview
Handler
Instance Attribute Summary collapse
- #args ⇒ Array readonly
- #block ⇒ Proc readonly
- #bot ⇒ Core readonly
- #event ⇒ Symbol readonly
- #group ⇒ Symbol readonly
- #pattern ⇒ String readonly
- #thread_group ⇒ ThreadGroup readonly private
Instance Method Summary collapse
-
#call(message, captures, arguments) ⇒ Thread
执行 Handler.
-
#initialize(bot, event, pattern, options = {}, &block) ⇒ Handler
constructor
A new instance of Handler.
- #stop ⇒ void
- #to_s ⇒ String
Constructor Details
#initialize(bot, event, pattern, options = {}, &block) ⇒ Handler
Returns a new instance of Handler.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/wechat/bot/handler.rb', line 26 def initialize(bot, event, pattern, = {}, &block) = { :group => nil, :execute_in_callback => false, :strip_colors => false, :args => [] }.merge() @bot = bot @event = event @pattern = pattern @block = block @group = [:group] @execute_in_callback = [:execute_in_callback] @args = [:args] @thread_group = ThreadGroup.new end |
Instance Attribute Details
#args ⇒ Array (readonly)
14 15 16 |
# File 'lib/wechat/bot/handler.rb', line 14 def args @args end |
#block ⇒ Proc (readonly)
17 18 19 |
# File 'lib/wechat/bot/handler.rb', line 17 def block @block end |
#event ⇒ Symbol (readonly)
8 9 10 |
# File 'lib/wechat/bot/handler.rb', line 8 def event @event end |
#group ⇒ Symbol (readonly)
20 21 22 |
# File 'lib/wechat/bot/handler.rb', line 20 def group @group end |
#pattern ⇒ String (readonly)
11 12 13 |
# File 'lib/wechat/bot/handler.rb', line 11 def pattern @pattern end |
#thread_group ⇒ ThreadGroup (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 |
# File 'lib/wechat/bot/handler.rb', line 24 def thread_group @thread_group end |
Instance Method Details
#call(message, captures, arguments) ⇒ Thread
执行 Handler
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/wechat/bot/handler.rb', line 51 def call(, captures, arguments) = captures + arguments thread = Thread.new { @bot.logger.debug "[New thread] For #{self}: #{Thread.current} -- #{@thread_group.list.size} in total." begin if @execute_in_callback @bot.callback.instance_exec(, *@args, *, &@block) else @block.call(, *@args, *) end rescue => e @bot.logger.error "[Thread error] #{e.} -> #{e.backtrace.join("\n")}" ensure @bot.logger.debug "[Thread done] For #{self}: #{Thread.current} -- #{@thread_group.list.size - 1} remaining." end } @thread_group.add(thread) thread end |
#stop ⇒ void
This method returns an undefined value.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/wechat/bot/handler.rb', line 74 def stop @bot.logger.debug "[Stopping handler] Stopping all threads of handler #{self}: #{@thread_group.list.size} threads..." @thread_group.list.each do |thread| Thread.new do @bot.logger.debug "[Ending thread] Waiting 10 seconds for #{thread} to finish..." thread.join(10) @bot.logger.debug "[Killing thread] Killing #{thread}" thread.kill end end end |
#to_s ⇒ String
87 88 89 90 |
# File 'lib/wechat/bot/handler.rb', line 87 def to_s # TODO maybe add the number of running threads to the output? "#<Cinch::Handler @event=#{@event.inspect} pattern=#{@pattern.inspect}>" end |