Class: VkCozy::YaScan

Inherits:
BaseFilter show all
Defined in:
lib/vk_cozy/framework/labeler/filters/filters.rb

Instance Method Summary collapse

Constructor Details

#initialize(pattern, flags: Regexp::IGNORECASE) ⇒ YaScan

Returns a new instance of YaScan.



13
14
15
16
# File 'lib/vk_cozy/framework/labeler/filters/filters.rb', line 13

def initialize(pattern, flags: Regexp::IGNORECASE)
  @flags = flags
  @pattern = valid_pattern(pattern) # Pattern example: my name is <name> 
end

Instance Method Details

#check_bot(event) ⇒ Object



34
35
36
37
38
# File 'lib/vk_cozy/framework/labeler/filters/filters.rb', line 34

def check_bot(event)
  if event.type == VkCozy::BotEventType::MESSAGE_NEW
    check_text(event.message.text)
  end
end

#check_text(text) ⇒ Object

Text example: my name is Volk



26
27
28
29
30
31
32
# File 'lib/vk_cozy/framework/labeler/filters/filters.rb', line 26

def check_text(text) # Text example: my name is Volk
  text_match = @pattern.match(text)
  if text_match.nil?
    return false
  end
  return Hash[ text_match.names.zip( text_match.captures ) ] # Return: {'name' => 'Volk'}
end

#check_user(event) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/vk_cozy/framework/labeler/filters/filters.rb', line 40

def check_user(event)
  if event.type == VkCozy::UserEventType::MESSAGE_NEW
    if event.from_me
      return false
    end
    check_text(event.text)
  end
end

#valid_pattern(pattern) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/vk_cozy/framework/labeler/filters/filters.rb', line 18

def valid_pattern(pattern)
  arguments = pattern.scan(/<([\w_]*)>/).flatten
  for i in arguments
    pattern = pattern.sub("<#{i}>", "(?<#{i}>.*)")
  end
  return Regexp.new(pattern, @flags)
end