cognition 1.0.1 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/lib/cognition/matcher.rb +1 -1
- data/lib/cognition/message.rb +1 -0
- data/lib/cognition/plugins/default.rb +20 -0
- data/lib/cognition/version.rb +1 -1
- data/lib/cognition.rb +6 -6
- data/test/test_matcher.rb +7 -0
- metadata +3 -3
- data/lib/cognition/plugins/ping.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e50eb1ebaaaa6fdfc1c8d760cf3bad97359bf14
|
4
|
+
data.tar.gz: 15e38a680a5cba0aa9652a4d47597afffdeee25a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f67e1f05ff1a2862dbb4fb501b8cb31be1fe62b741083fbffff997c2310849ed5ce44e8f4376eabc5725e45c0063a1db372b5660ad7dfc20318f691fe56d772d
|
7
|
+
data.tar.gz: d4f667e367fef56e5b43168de85346bcc918ba1f69ed575f71cd823eeaa0231744c9b31c621e4e87c3ef6a9f9387a141292aa77efeae996d65280c1ed5104886
|
data/README.md
CHANGED
@@ -45,11 +45,22 @@ Creating plugins is easy. Subclass `Cognition::Plugins::Base` and setup your
|
|
45
45
|
matches and logic that should be run:
|
46
46
|
```ruby
|
47
47
|
class Hello < Cognition::Plugins::Base
|
48
|
+
# Simple string based matcher. Must match *EXACTLY*
|
48
49
|
match 'hello', 'hello: Returns Hello World', :hello
|
49
50
|
|
51
|
+
# Advanced Regexp based matcher. Capture groups are made available
|
52
|
+
# via MatchData in the matches method
|
53
|
+
match /hello\s*(?<name>.*)/, 'hello <name>', :hello_person
|
54
|
+
|
55
|
+
|
50
56
|
def hello(*)
|
51
57
|
'Hello World'
|
52
58
|
end
|
59
|
+
|
60
|
+
def hello_person(msg)
|
61
|
+
name = msg.matches[:name]
|
62
|
+
"Hello #{name}"
|
63
|
+
end
|
53
64
|
end
|
54
65
|
```
|
55
66
|
|
data/lib/cognition/matcher.rb
CHANGED
data/lib/cognition/message.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Cognition
|
2
|
+
module Plugins
|
3
|
+
class Default < Cognition::Plugins::Base
|
4
|
+
match(/^ping/i, 'ping: Returns "PONG"', :pong)
|
5
|
+
match(/^help\s*(?<command>.*)/i, 'help: Returns help text for registered plugins', :help)
|
6
|
+
|
7
|
+
def pong(*)
|
8
|
+
'PONG'
|
9
|
+
end
|
10
|
+
|
11
|
+
def help(msg)
|
12
|
+
if msg.matches["command"].empty?
|
13
|
+
Cognition.help.join("\n")
|
14
|
+
else
|
15
|
+
Cognition.help.find_all { |text| text.match msg.matches[:command] }.join("\n")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/cognition/version.rb
CHANGED
data/lib/cognition.rb
CHANGED
@@ -2,7 +2,7 @@ require 'cognition/version'
|
|
2
2
|
require 'cognition/message'
|
3
3
|
require 'cognition/matcher'
|
4
4
|
require 'cognition/plugins/base'
|
5
|
-
require 'cognition/plugins/
|
5
|
+
require 'cognition/plugins/default'
|
6
6
|
|
7
7
|
module Cognition
|
8
8
|
extend self
|
@@ -25,7 +25,7 @@ module Cognition
|
|
25
25
|
def reset
|
26
26
|
@matchers = []
|
27
27
|
@plugins = []
|
28
|
-
register(Cognition::Plugins::
|
28
|
+
register(Cognition::Plugins::Default)
|
29
29
|
end
|
30
30
|
|
31
31
|
def plugin_names
|
@@ -33,7 +33,7 @@ module Cognition
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def help
|
36
|
-
matchers.map(&:help)
|
36
|
+
matchers.map(&:help)
|
37
37
|
end
|
38
38
|
|
39
39
|
private
|
@@ -50,7 +50,7 @@ module Cognition
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def process_string(message, metadata = {})
|
53
|
-
process_msg(Cognition::Message.new(message, metadata))
|
53
|
+
process_msg(Cognition::Message.new(message.strip, metadata))
|
54
54
|
end
|
55
55
|
|
56
56
|
def matchers
|
@@ -66,5 +66,5 @@ module Cognition
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
# Default plugin, responds to 'ping' with 'PONG'
|
70
|
-
Cognition.register(Cognition::Plugins::
|
69
|
+
# Default plugin, responds to 'ping' with 'PONG' and provides help text
|
70
|
+
Cognition.register(Cognition::Plugins::Default)
|
data/test/test_matcher.rb
CHANGED
@@ -50,4 +50,11 @@ class MatcherTest < Minitest::Test
|
|
50
50
|
matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {'PONG'})
|
51
51
|
refute matcher.attempt(msg)
|
52
52
|
end
|
53
|
+
|
54
|
+
def test_sets_matches_on_message
|
55
|
+
msg = Cognition::Message.new('hello john')
|
56
|
+
matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, 'test', &Proc.new {'PONG'})
|
57
|
+
matcher.attempt(msg)
|
58
|
+
assert_equal "john", msg.matches[:name]
|
59
|
+
end
|
53
60
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cognition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Anderson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,7 +69,7 @@ files:
|
|
69
69
|
- lib/cognition/matcher.rb
|
70
70
|
- lib/cognition/message.rb
|
71
71
|
- lib/cognition/plugins/base.rb
|
72
|
-
- lib/cognition/plugins/
|
72
|
+
- lib/cognition/plugins/default.rb
|
73
73
|
- lib/cognition/version.rb
|
74
74
|
- test/fixtures/hello.rb
|
75
75
|
- test/test_cognition.rb
|