Class: Filter::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.



23
24
25
26
# File 'lib/vk_cozy/framework/labeler/filters/filters.rb', line 23

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



44
45
46
# File 'lib/vk_cozy/framework/labeler/filters/filters.rb', line 44

def check_bot(event)
  check_text(event.message.text)
end

#check_text(text) ⇒ Object

Text example: my name is Volk



36
37
38
39
40
41
42
# File 'lib/vk_cozy/framework/labeler/filters/filters.rb', line 36

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



48
49
50
51
52
53
# File 'lib/vk_cozy/framework/labeler/filters/filters.rb', line 48

def check_user(event)
  if event.from_me
    return false
  end
  check_text(event.text)
end

#valid_pattern(pattern) ⇒ Object



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

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