cognition 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -4
- data/lib/cognition/matcher.rb +5 -5
- data/lib/cognition/message.rb +0 -5
- data/lib/cognition/plugins/ping.rb +3 -3
- data/lib/cognition/version.rb +1 -1
- data/lib/cognition.rb +32 -26
- data/test/fixtures/hello.rb +1 -1
- data/test/test_cognition.rb +9 -5
- data/test/test_message.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c351675cea9e838d9bc3af6a197ea43e204e5a4
|
4
|
+
data.tar.gz: 6136d88fc7e55bd093459ff2f7f9d58ceb81973f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4919b519fdd119a480fe0944bd95a9d95237bf3f6c9ef7cf6eb5c913934d9dd5d537767a8e710cc2c2bc10892b3f6562040384a91c01f633e8b2f51d9997a85
|
7
|
+
data.tar.gz: cbade8a82a96f76504c0c44d0444e8601be9ccd6533164653fa475d70ffffdfda0133ad332d50e411cdb25ba661672bfec7ffe2890f39cea359b9957331766fa
|
data/README.md
CHANGED
@@ -33,15 +33,26 @@ result = Cognition.process('another command', {user_id: 15, name: 'Bob'})
|
|
33
33
|
```
|
34
34
|
|
35
35
|
Internally, `Cognition` will turn your values into a `Cognition::Message` so
|
36
|
-
the metadata will be passed along with the message, and
|
37
|
-
|
36
|
+
the metadata will be passed along with the message, and arbitrary metadata
|
37
|
+
is available in the #metadata Hash:
|
38
38
|
```ruby
|
39
39
|
msg = Cognition::Message('another command', {user_id: 15, name: 'Bob'})
|
40
|
-
msg.user_id # Returns 15
|
41
|
-
msg.name # Returns 'Bob'
|
42
40
|
msg.metadata # Returns { user_id: 15, name: 'Bob' }
|
43
41
|
```
|
44
42
|
|
43
|
+
## Creating a Plugin
|
44
|
+
Creating plugins is easy. Subclass `Cognition::Plugins::Base` and setup your
|
45
|
+
matches and logic that should be run:
|
46
|
+
```ruby
|
47
|
+
class Hello < Cognition::Plugins::Base
|
48
|
+
match 'hello', 'hello: Returns Hello World', :hello
|
49
|
+
|
50
|
+
def hello(*)
|
51
|
+
'Hello World'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
45
56
|
## Contributing
|
46
57
|
|
47
58
|
1. Fork it ( https://github.com/anoldguy/cognition/fork )
|
data/lib/cognition/matcher.rb
CHANGED
@@ -21,12 +21,12 @@ module Cognition
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def matches?(msg)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
case trigger
|
25
|
+
when String
|
26
|
+
trigger == msg.command
|
27
|
+
when Regexp
|
28
|
+
trigger.match msg.command
|
28
29
|
end
|
29
|
-
false
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
data/lib/cognition/message.rb
CHANGED
data/lib/cognition/version.rb
CHANGED
data/lib/cognition.rb
CHANGED
@@ -10,28 +10,13 @@ module Cognition
|
|
10
10
|
attr_accessor :plugins, :matchers
|
11
11
|
|
12
12
|
def process(msg, metadata = {})
|
13
|
-
if msg.
|
13
|
+
if msg.respond_to? :command
|
14
14
|
process_msg(msg)
|
15
15
|
else
|
16
16
|
process_string(msg.to_s, metadata)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def process_msg(msg)
|
21
|
-
response = false
|
22
|
-
matchers.each do |matcher|
|
23
|
-
if matcher.attempt(msg)
|
24
|
-
response = matcher.response
|
25
|
-
break
|
26
|
-
end
|
27
|
-
end
|
28
|
-
response ? response : help
|
29
|
-
end
|
30
|
-
|
31
|
-
def process_string(message, metadata = {})
|
32
|
-
process_msg(Cognition::Message.new(message, metadata))
|
33
|
-
end
|
34
|
-
|
35
20
|
def register(klass)
|
36
21
|
return false if plugin_names.include? klass.to_s
|
37
22
|
plugins << klass.new
|
@@ -43,21 +28,42 @@ module Cognition
|
|
43
28
|
register(Cognition::Plugins::Ping)
|
44
29
|
end
|
45
30
|
|
46
|
-
def help
|
47
|
-
"No such command:\n\n #{matchers.map(&:help).join('\n')}"
|
48
|
-
end
|
49
|
-
|
50
|
-
def matchers
|
51
|
-
plugins.collect(&:matchers).flatten.compact
|
52
|
-
end
|
53
|
-
|
54
31
|
def plugin_names
|
55
32
|
plugins.map { |p| p.class.name }
|
56
33
|
end
|
57
34
|
|
58
|
-
def
|
59
|
-
|
35
|
+
def help
|
36
|
+
matchers.map(&:help).join('\n')
|
60
37
|
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def process_msg(msg)
|
42
|
+
response = false
|
43
|
+
matchers.each do |matcher|
|
44
|
+
if matcher.attempt(msg)
|
45
|
+
response = matcher.response
|
46
|
+
break
|
47
|
+
end
|
48
|
+
end
|
49
|
+
response ? response : not_found(msg.command)
|
50
|
+
end
|
51
|
+
|
52
|
+
def process_string(message, metadata = {})
|
53
|
+
process_msg(Cognition::Message.new(message, metadata))
|
54
|
+
end
|
55
|
+
|
56
|
+
def matchers
|
57
|
+
plugins.flat_map(&:matchers).compact
|
58
|
+
end
|
59
|
+
|
60
|
+
def plugins
|
61
|
+
@plugins ||= []
|
62
|
+
end
|
63
|
+
|
64
|
+
def not_found(message)
|
65
|
+
"No such command: #{message}\nUse 'help' for available commands!"
|
66
|
+
end
|
61
67
|
end
|
62
68
|
|
63
69
|
# Default plugin, responds to 'ping' with 'PONG'
|
data/test/fixtures/hello.rb
CHANGED
data/test/test_cognition.rb
CHANGED
@@ -10,8 +10,14 @@ class CognitionTest < Minitest::Test
|
|
10
10
|
def test_registers_plugins
|
11
11
|
Cognition.register(Hello)
|
12
12
|
|
13
|
-
assert_equal 2, Cognition.
|
14
|
-
|
13
|
+
assert_equal 2, Cognition.plugin_names.count
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_does_not_register_duplicate_plugins
|
17
|
+
Cognition.register(Hello)
|
18
|
+
Cognition.register(Hello)
|
19
|
+
|
20
|
+
assert_equal 2, Cognition.plugin_names.count
|
15
21
|
end
|
16
22
|
|
17
23
|
def test_processes_messages
|
@@ -31,8 +37,6 @@ class CognitionTest < Minitest::Test
|
|
31
37
|
Cognition.register(Hello)
|
32
38
|
msg = Cognition::Message.new('pong')
|
33
39
|
output = Cognition.process(msg)
|
34
|
-
assert_match
|
35
|
-
assert_match 'ping: Returns "PONG"', output
|
36
|
-
assert_match 'hello: Returns Hello World', output
|
40
|
+
assert_match "No such command: pong\nUse 'help' for available commands!", output
|
37
41
|
end
|
38
42
|
end
|
data/test/test_message.rb
CHANGED
@@ -4,7 +4,7 @@ require 'cognition'
|
|
4
4
|
class CognitionTest < Minitest::Test
|
5
5
|
def test_sets_metadata
|
6
6
|
msg = Cognition::Message.new('test', user_id: 15, foo: 'bar')
|
7
|
-
assert_equal 15, msg.user_id
|
8
|
-
assert_equal 'bar', msg.foo
|
7
|
+
assert_equal 15, msg.metadata[:user_id]
|
8
|
+
assert_equal 'bar', msg.metadata[:foo]
|
9
9
|
end
|
10
10
|
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.1
|
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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|