Class: SlackBot::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/slack/matchers/matcher.rb

Instance Method Summary collapse

Constructor Details

#initializeMatcher

Returns a new instance of Matcher.



2
3
4
5
# File 'lib/slack/matchers/matcher.rb', line 2

def initialize
  @tests = []
  @finally = nil
end

Instance Method Details

#from?(user) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/slack/matchers/matcher.rb', line 17

def from?(user)
  @tests << lambda do |msg|
    user = user.to_s.downcase
    msg_user = msg.user
    return false unless msg_user
    if msg_user.id == user || msg_user.name.downcase == user
      return true
    else
      real = msg_user.real_name
      return real == user || real.split(' ').map { |n| user == n }.any?
    end
  end
  self
end

#in?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/slack/matchers/matcher.rb', line 32

def in?(options={})
  @tests << lambda do |msg|
    if options[:id]
      return msg.channel == options[:id]
    elsif options[:name]
      chan = msg.bot.channel(options[:name])
      return chan['id'] == msg.channel
    end
  end
  self
end

#include?(text) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/slack/matchers/matcher.rb', line 44

def include?(text)
  @tests << Proc.new do |msg|
    msg.text.downcase.include? text
  end
  self
end

#match?(reg) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/slack/matchers/matcher.rb', line 51

def match?(reg)
  @tests << Proc.new { |msg| msg.text.downcase.match reg }
  self
end

#run_on(msg) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/slack/matchers/matcher.rb', line 7

def run_on(msg)
  @tests.each do |test|
    unless test.call(msg)
      return false
    end
  end
  @finally.call(msg) if @finally
  return true
end

#then(&block) ⇒ Object



61
62
63
# File 'lib/slack/matchers/matcher.rb', line 61

def then(&block)
  @finally = block
end

#then_reply(*args) ⇒ Object



65
66
67
68
69
# File 'lib/slack/matchers/matcher.rb', line 65

def then_reply(*args)
  @finally = Proc.new do |msg|
    msg.reply(*args)
  end
end

#try?(&block) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/slack/matchers/matcher.rb', line 56

def try?(&block)
  @tests << block
  self
end