cognition 1.0.0 → 1.0.1

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: f8d86f234aca4e8b72043f630090f7708e67edb2
4
- data.tar.gz: 688bf3eb7f504c65735e1b9a72c2622e3d2ddeda
3
+ metadata.gz: 0c351675cea9e838d9bc3af6a197ea43e204e5a4
4
+ data.tar.gz: 6136d88fc7e55bd093459ff2f7f9d58ceb81973f
5
5
  SHA512:
6
- metadata.gz: 8baa3a9b470d6e10b6ff86fa106fb1add6581e81b5a27b6175c144393e1602725b607b121b83481cb98777ab31187b1a3132b7f9ef27768532a9e72d18270883
7
- data.tar.gz: 24ac9d126b4332d6f9a42db3b0e38fd48370de3c545f1709205072010b12ccdd83a52f2f7fd0e839524d8e110b4f0e2cde311a66db91ca339acf779067ba10f6
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 the keys will be made
37
- into methods that return the value. The raw metadata is also made available:
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 )
@@ -21,12 +21,12 @@ module Cognition
21
21
  end
22
22
 
23
23
  def matches?(msg)
24
- if trigger.is_a? String
25
- return true if trigger == msg.command
26
- elsif trigger.is_a? Regexp
27
- return true if (@match = trigger.match msg.command)
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
@@ -5,11 +5,6 @@ module Cognition
5
5
  def initialize(command, metadata = {})
6
6
  @command = command
7
7
  @metadata = metadata
8
- metadata.each do |arg, value|
9
- define_singleton_method arg do
10
- value
11
- end
12
- end
13
8
  end
14
9
  end
15
10
  end
@@ -1,11 +1,11 @@
1
1
  module Cognition
2
2
  module Plugins
3
3
  class Ping < Cognition::Plugins::Base
4
- def pong(_msg)
4
+ match(/ping/i, 'ping: Returns "PONG"', :pong)
5
+
6
+ def pong(*)
5
7
  'PONG'
6
8
  end
7
-
8
- match(/ping/i, 'ping: Returns "PONG"', :pong)
9
9
  end
10
10
  end
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module Cognition
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
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.is_a? Cognition::Message
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 plugins
59
- @plugins ||= []
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'
@@ -1,7 +1,7 @@
1
1
  class Hello < Cognition::Plugins::Base
2
2
  match 'hello', 'hello: Returns Hello World', :hello
3
3
 
4
- def hello(_msg)
4
+ def hello(*)
5
5
  'Hello World'
6
6
  end
7
7
  end
@@ -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.plugins.count
14
- assert_instance_of Hello, Cognition.plugins.last
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 'No such command:', output
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.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-20 00:00:00.000000000 Z
11
+ date: 2015-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler