module AiWhereIsTrigger
AiWhereIsTrigger — detect unprefixed “where is …” channel questions for !ai. Public: channel_trigger?, parse_question, extract_subject Depends: (none) Tests: test/lib/ai_where_is_trigger_test.rb
Constants
- COMMAND_PREFIX_PATTERN
- WHERE_IS_PATTERN
Public Instance Methods
Source
# File lib/ai/where_is_trigger.rb, line 14 def channel_trigger?(text) sanitized = sanitize_trigger_text(text) return false if sanitized.empty? return false if command_prefix?(sanitized) !parse_question(sanitized).nil? end
Source
# File lib/ai/where_is_trigger.rb, line 44 def command_prefix?(text) text.to_s.lstrip.match?(COMMAND_PREFIX_PATTERN) end
Source
# File lib/ai/where_is_trigger.rb, line 32 def extract_subject(text) match = sanitize_trigger_text(text).match(WHERE_IS_PATTERN) return nil unless match subject = match[1].to_s.strip.sub(/\?+\z/, "").strip subject.empty? ? nil : subject end
Source
# File lib/ai/where_is_trigger.rb, line 22 def parse_question(text) match = sanitize_trigger_text(text).match(WHERE_IS_PATTERN) return nil unless match remainder = match[1].to_s.strip.sub(/\?+\z/, "").strip return nil if remainder.empty? "where is #{remainder}" end
Source
# File lib/ai/where_is_trigger.rb, line 40 def sanitize_trigger_text(text) text.to_s.gsub(/[\x00-\x1f]/, " ").squeeze(" ").strip end