This repository has been archived by the owner on Jan 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v0.3.27] Refactored & added
QueryPattern
- Added QueryPattern to extract commands easily (not tested) - Minor code refactoring Signed-off-by: Kenneth Ham <kenneth.ham@comp.nus.edu.sg>
- Loading branch information
Showing
5 changed files
with
111 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
module NUSBotgram | ||
class QueryPattern | ||
def ===(_) | ||
false | ||
end | ||
|
||
def arguments(message) | ||
[] | ||
end | ||
end | ||
|
||
class TextQuery < QueryPattern | ||
attr_accessor :pattern | ||
|
||
def initialize(pattern = nil) | ||
@pattern = pattern | ||
end | ||
|
||
def ===(message) | ||
return false unless message.type == :text | ||
return true if @pattern.nil? | ||
@pattern === message.text | ||
end | ||
|
||
def arguments(message) | ||
if Regexp === @pattern | ||
query = @pattern.match(message.text) | ||
query.to_a | ||
else | ||
[message.text] | ||
end | ||
end | ||
end | ||
|
||
class CommandQuery < QueryPattern | ||
attr_accessor :command | ||
|
||
def initialize(command = nil, no_split: false) | ||
@command = command | ||
@no_split = no_split | ||
end | ||
|
||
def ===(message) | ||
start_with = '/' | ||
|
||
if !@command.nil? | ||
start_with += @command.to_s | ||
end | ||
return false if message.type != :text | ||
return false if !message.text.start_with? start_with | ||
|
||
true | ||
end | ||
|
||
def arguments(message) | ||
case | ||
when @no_split | ||
cmd, _, args = message.text.parition(/\s/) | ||
[cmd[1..-1], args] | ||
when @comamnd.nil? | ||
cmd, *args = message.text.split | ||
[cmd[1..-1], *args] | ||
else | ||
cmd, *args = message.text.split | ||
args | ||
end | ||
end | ||
end | ||
|
||
class StarQuery < QueryPattern | ||
def ===(_) | ||
true | ||
end | ||
end | ||
|
||
class FallbackQuery < StarQuery | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module NUSBotgram | ||
VERSION = "0.3.26" | ||
VERSION = "0.3.27" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters