cognition 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b556dd703f6cedceb65c61a882701d00470e75c
4
- data.tar.gz: 7290e4591bf836a68d124e7c2655a2b10fdff48c
3
+ metadata.gz: f8d86f234aca4e8b72043f630090f7708e67edb2
4
+ data.tar.gz: 688bf3eb7f504c65735e1b9a72c2622e3d2ddeda
5
5
  SHA512:
6
- metadata.gz: 46abe2f2530cc77c9e36004ff1821d9af9031a4f06107125e66687ab178014bdcc3a69b9eca8038bd2794b8e6ec7da5a5880d880ac1fe2a975da426bbb3d2825
7
- data.tar.gz: d8c46b14fefa4d54e44e2afeed0f4d3eee41a50d0171296adde5540e40fdd8f083769fc424ada51a33ebd3affa965ac77002bc1b8c347c160b5e0f72308c5c73
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 a match, it executes an associated block of code or method,
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
- Coming Soon!
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
 
@@ -9,18 +9,15 @@ module Cognition
9
9
 
10
10
  attr_accessor :plugins, :matchers
11
11
 
12
- def reset
13
- @matchers = []
14
- @plugins = []
15
- register(Cognition::Plugins::Ping)
16
- end
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 process_message(msg)
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
@@ -1,6 +1,6 @@
1
1
  module Cognition
2
2
  class Message
3
- attr_reader :match, :command, :metadata
3
+ attr_reader :command, :metadata
4
4
 
5
5
  def initialize(command, metadata = {})
6
6
  @command = command
@@ -1,3 +1,3 @@
1
1
  module Cognition
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -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.process_message(msg)
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.process_message(msg)
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
@@ -0,0 +1,10 @@
1
+ require 'minitest/autorun'
2
+ require 'cognition'
3
+
4
+ class CognitionTest < Minitest::Test
5
+ def test_sets_metadata
6
+ msg = Cognition::Message.new('test', user_id: 15, foo: 'bar')
7
+ assert_equal 15, msg.user_id
8
+ assert_equal 'bar', msg.foo
9
+ end
10
+ end
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: 0.1.0
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