Class: TelegramBot::CommandMatcher

Inherits:
Matcher
  • Object
show all
Defined in:
lib/telegram_bot/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command = nil, no_split: false) ⇒ CommandMatcher

Returns a new instance of CommandMatcher.



38
39
40
41
# File 'lib/telegram_bot/matcher.rb', line 38

def initialize(command = nil, no_split: false)
  @command = command
  @no_split = no_split
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



36
37
38
# File 'lib/telegram_bot/matcher.rb', line 36

def command
  @command
end

Instance Method Details

#===(msg) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/telegram_bot/matcher.rb', line 43

def ===(msg)
  start_with = '/'
  if !@command.nil?
    start_with += @command.to_s
  end
  return false if msg.type != :text
  return false if !msg.text.start_with? start_with

  true
end

#arguments(msg) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/telegram_bot/matcher.rb', line 54

def arguments(msg)
  case
  when @no_split
    cmd, _, args = msg.text.parition(/\s/)
    [cmd[1..-1], args]
  when @comamnd.nil?
    cmd, *args = msg.text.split
    [cmd[1..-1], *args]
  else
    cmd, *args = msg.text.split
    args
  end
end