cognition 0.1.0 → 1.0.0
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 +21 -3
- data/lib/cognition.rb +22 -10
- data/lib/cognition/message.rb +1 -1
- data/lib/cognition/version.rb +1 -1
- data/test/test_cognition.rb +10 -2
- data/test/test_message.rb +10 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8d86f234aca4e8b72043f630090f7708e67edb2
|
4
|
+
data.tar.gz: 688bf3eb7f504c65735e1b9a72c2622e3d2ddeda
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8baa3a9b470d6e10b6ff86fa106fb1add6581e81b5a27b6175c144393e1602725b607b121b83481cb98777ab31187b1a3132b7f9ef27768532a9e72d18270883
|
7
|
+
data.tar.gz: 24ac9d126b4332d6f9a42db3b0e38fd48370de3c545f1709205072010b12ccdd83a52f2f7fd0e839524d8e110b4f0e2cde311a66db91ca339acf779067ba10f6
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Cognition
|
2
2
|
|
3
3
|
This is a gem that parses a message, and compares it to various matchers.
|
4
|
-
When it finds
|
5
|
-
returning the output of whatever was run.
|
4
|
+
When it finds the **first match**, it executes an associated block of code or
|
5
|
+
method, returning the output of whatever was run.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -22,7 +22,25 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
Process your message:
|
26
|
+
```ruby
|
27
|
+
result = Cognition.process('command I need to process')
|
28
|
+
```
|
29
|
+
|
30
|
+
You can also include metadata with your message, like user info, or whatever:
|
31
|
+
```ruby
|
32
|
+
result = Cognition.process('another command', {user_id: 15, name: 'Bob'})
|
33
|
+
```
|
34
|
+
|
35
|
+
Internally, `Cognition` will turn your values into a `Cognition::Message` so
|
36
|
+
the metadata will be passed along with the message, and the keys will be made
|
37
|
+
into methods that return the value. The raw metadata is also made available:
|
38
|
+
```ruby
|
39
|
+
msg = Cognition::Message('another command', {user_id: 15, name: 'Bob'})
|
40
|
+
msg.user_id # Returns 15
|
41
|
+
msg.name # Returns 'Bob'
|
42
|
+
msg.metadata # Returns { user_id: 15, name: 'Bob' }
|
43
|
+
```
|
26
44
|
|
27
45
|
## Contributing
|
28
46
|
|
data/lib/cognition.rb
CHANGED
@@ -9,18 +9,15 @@ module Cognition
|
|
9
9
|
|
10
10
|
attr_accessor :plugins, :matchers
|
11
11
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
def register(klass)
|
19
|
-
return false if plugin_names.include? klass.to_s
|
20
|
-
plugins << klass.new
|
12
|
+
def process(msg, metadata = {})
|
13
|
+
if msg.is_a? Cognition::Message
|
14
|
+
process_msg(msg)
|
15
|
+
else
|
16
|
+
process_string(msg.to_s, metadata)
|
17
|
+
end
|
21
18
|
end
|
22
19
|
|
23
|
-
def
|
20
|
+
def process_msg(msg)
|
24
21
|
response = false
|
25
22
|
matchers.each do |matcher|
|
26
23
|
if matcher.attempt(msg)
|
@@ -31,6 +28,21 @@ module Cognition
|
|
31
28
|
response ? response : help
|
32
29
|
end
|
33
30
|
|
31
|
+
def process_string(message, metadata = {})
|
32
|
+
process_msg(Cognition::Message.new(message, metadata))
|
33
|
+
end
|
34
|
+
|
35
|
+
def register(klass)
|
36
|
+
return false if plugin_names.include? klass.to_s
|
37
|
+
plugins << klass.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def reset
|
41
|
+
@matchers = []
|
42
|
+
@plugins = []
|
43
|
+
register(Cognition::Plugins::Ping)
|
44
|
+
end
|
45
|
+
|
34
46
|
def help
|
35
47
|
"No such command:\n\n #{matchers.map(&:help).join('\n')}"
|
36
48
|
end
|
data/lib/cognition/message.rb
CHANGED
data/lib/cognition/version.rb
CHANGED
data/test/test_cognition.rb
CHANGED
@@ -16,13 +16,21 @@ class CognitionTest < Minitest::Test
|
|
16
16
|
|
17
17
|
def test_processes_messages
|
18
18
|
msg = Cognition::Message.new('ping')
|
19
|
-
assert_equal 'PONG', Cognition.
|
19
|
+
assert_equal 'PONG', Cognition.process(msg)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_processes_strings
|
23
|
+
assert_equal 'PONG', Cognition.process('ping')
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_processes_strings_with_metadata
|
27
|
+
assert_equal 'PONG', Cognition.process('ping', foo: 'bar')
|
20
28
|
end
|
21
29
|
|
22
30
|
def test_shows_help_if_no_matches
|
23
31
|
Cognition.register(Hello)
|
24
32
|
msg = Cognition::Message.new('pong')
|
25
|
-
output = Cognition.
|
33
|
+
output = Cognition.process(msg)
|
26
34
|
assert_match 'No such command:', output
|
27
35
|
assert_match 'ping: Returns "PONG"', output
|
28
36
|
assert_match 'hello: Returns Hello World', output
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cognition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nathan Anderson
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- test/fixtures/hello.rb
|
75
75
|
- test/test_cognition.rb
|
76
76
|
- test/test_matcher.rb
|
77
|
+
- test/test_message.rb
|
77
78
|
- test/test_plugin.rb
|
78
79
|
homepage: https://github.com/anoldguy/Cognition
|
79
80
|
licenses:
|
@@ -103,4 +104,5 @@ test_files:
|
|
103
104
|
- test/fixtures/hello.rb
|
104
105
|
- test/test_cognition.rb
|
105
106
|
- test/test_matcher.rb
|
107
|
+
- test/test_message.rb
|
106
108
|
- test/test_plugin.rb
|