Class: WeChat::Bot::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/bot/handler.rb

Overview

Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {}, &block)
  options              = {
    :group => nil,
    :execute_in_callback => false,
    :strip_colors => false,
    :args => []
  }.merge(options)

  @bot = bot
  @event = event
  @pattern = pattern
  @block = block
  @group = options[:group]
  @execute_in_callback = options[:execute_in_callback]
  @args = options[:args]

  @thread_group = ThreadGroup.new
end

Instance Attribute Details

#argsArray (readonly)

Returns:

  • (Array)


14
15
16
# File 'lib/wechat/bot/handler.rb', line 14

def args
  @args
end

#blockProc (readonly)

Returns:

  • (Proc)


17
18
19
# File 'lib/wechat/bot/handler.rb', line 17

def block
  @block
end

#botCore (readonly)

Returns:



5
6
7
# File 'lib/wechat/bot/handler.rb', line 5

def bot
  @bot
end

#eventSymbol (readonly)

Returns:

  • (Symbol)


8
9
10
# File 'lib/wechat/bot/handler.rb', line 8

def event
  @event
end

#groupSymbol (readonly)

Returns:

  • (Symbol)


20
21
22
# File 'lib/wechat/bot/handler.rb', line 20

def group
  @group
end

#patternString (readonly)

Returns:



11
12
13
# File 'lib/wechat/bot/handler.rb', line 11

def pattern
  @pattern
end

#thread_groupThreadGroup (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.

Returns:

  • (ThreadGroup)


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

Parameters:

  • message (Symbol)
  • captures (String)
  • arguments (Array)

Returns:

  • (Thread)


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(message, captures, arguments)
  bargs = 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(message, *@args, *bargs, &@block)
      else
        @block.call(message, *@args, *bargs)
      end
    rescue => e
      @bot.logger.error "[Thread error] #{e.message} -> #{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

#stopvoid

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_sString

Returns:



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