cognition 1.2.1 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9cf8b8eab5eee3318c21ef862aee4b4d9b509e3e
4
- data.tar.gz: ab9a34f493162655e20f027fefb7760958363e8e
3
+ metadata.gz: 3da7685b2dd87abbbb4700e49844520a5cc78e69
4
+ data.tar.gz: 16ed1c2b9b0b02544e322e65c245b1befcd62821
5
5
  SHA512:
6
- metadata.gz: aea4ad3b0b5bf8719ba3aa83be20ac23ceb6c46de384f5ed635f15fb3ac8abe0342dcd73099b28d38a0a7eb51cf2a5a111d2813041d626a2fdf61374f59187aa
7
- data.tar.gz: d55bab9be99fff93027edb9b23cbfcd3ffd3c689a1191aea9e85385498ffd466d72225aa3c8fa8cb7f04abf47a730b25480bd53e90e897b1853e31e2edf121a4
6
+ metadata.gz: 276a3ef0f303cc54354a69be7b7a541d2e8e6e54a5abd7fd742538d64d5dc4fa5518b364685560c071f0116f18478dfa953cb8a34e1c06f37fe2ebfdb4ebec7a
7
+ data.tar.gz: eb2379fee7ea23e0f4de7a9f0f0c2ffd05378efa9a5599aba5e262987f65c4e3fc85998c387379c5846c0ca24ec6893c5177162ec1b10e3c934180a801fddb65
data/README.md CHANGED
@@ -22,14 +22,19 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
+ Instantiate:
26
+ ```ruby
27
+ bot = Cognition::Bot.new
28
+ ```
29
+
25
30
  Process your message:
26
31
  ```ruby
27
- result = Cognition.process('command I need to process')
32
+ result = bot.process('command I need to process')
28
33
  ```
29
34
 
30
35
  You can also include metadata with your message, like user info, or whatever:
31
36
  ```ruby
32
- result = Cognition.process('another command', {user_id: 15, name: 'Bob'})
37
+ result = bot.process('another command', {user_id: 15, name: 'Bob'})
33
38
  ```
34
39
 
35
40
  Internally, `Cognition` will turn your values into a `Cognition::Message` so
@@ -83,10 +88,10 @@ end
83
88
 
84
89
  After you've done that, you will be able to do:
85
90
  ```ruby
86
- Cognition.register(Hello)
87
- Cognition.process("help hello") # "hello <name> - Greets you by name!"
88
- Cognition.process("hello") # "Hello World"
89
- Cognition.process("hello foo") # "Hello foo"
91
+ bot.register(Hello)
92
+ bot.process("help hello") # "hello <name> - Greets you by name!"
93
+ bot.process("hello") # "Hello World"
94
+ bot.process("hello foo") # "Hello foo"
90
95
  ```
91
96
 
92
97
  ## Contributing
@@ -1,71 +1,5 @@
1
1
  require 'cognition/version'
2
- require 'cognition/message'
3
- require 'cognition/matcher'
4
- require 'cognition/responder'
5
- require 'cognition/plugins/base'
6
- require 'cognition/plugins/default'
2
+ require 'cognition/bot'
7
3
 
8
4
  module Cognition
9
- extend self
10
-
11
- attr_accessor :plugins, :matchers
12
-
13
- def process(msg, metadata = {})
14
- if msg.respond_to? :command
15
- process_msg(msg)
16
- else
17
- process_string(msg.to_s, metadata)
18
- end
19
- end
20
-
21
- def register(klass)
22
- return false if plugin_names.include? klass.to_s
23
- plugins << klass.new
24
- end
25
-
26
- def reset
27
- @matchers = []
28
- @plugins = []
29
- register(Cognition::Plugins::Default)
30
- end
31
-
32
- def plugin_names
33
- plugins.map { |p| p.class.name }
34
- end
35
-
36
- def help
37
- matchers.flat_map(&:help)
38
- end
39
-
40
- private
41
-
42
- def process_msg(msg)
43
- response = false
44
- matchers.each do |matcher|
45
- if matcher.attempt(msg)
46
- response = matcher.response
47
- break
48
- end
49
- end
50
- response ? response : not_found(msg.command)
51
- end
52
-
53
- def process_string(message, metadata = {})
54
- process_msg(Cognition::Message.new(message.strip, metadata))
55
- end
56
-
57
- def matchers
58
- plugins.flat_map(&:matchers).compact
59
- end
60
-
61
- def plugins
62
- @plugins ||= []
63
- end
64
-
65
- def not_found(message)
66
- "No such command: #{message}\nUse 'help' for available commands!"
67
- end
68
5
  end
69
-
70
- # Default plugin, responds to 'ping' with 'PONG' and provides help text
71
- Cognition.register(Cognition::Plugins::Default)
@@ -0,0 +1,72 @@
1
+ require 'cognition/message'
2
+ require 'cognition/matcher'
3
+ require 'cognition/responder'
4
+ require 'cognition/plugins/base'
5
+ require 'cognition/plugins/default'
6
+
7
+ module Cognition
8
+ class Bot
9
+ attr_accessor :plugins, :matchers
10
+
11
+ def initialize
12
+ # Default plugin, responds to 'ping' with 'PONG' and provides help text
13
+ register(Cognition::Plugins::Default)
14
+ end
15
+
16
+ def process(msg, metadata = {})
17
+ if msg.respond_to? :command
18
+ process_msg(msg)
19
+ else
20
+ process_string(msg.to_s, metadata)
21
+ end
22
+ end
23
+
24
+ def register(klass)
25
+ return false if plugin_names.include? klass.to_s
26
+ plugins << klass.new(self)
27
+ end
28
+
29
+ def reset
30
+ @matchers = []
31
+ @plugins = []
32
+ register(Cognition::Plugins::Default)
33
+ end
34
+
35
+ def plugin_names
36
+ plugins.map { |p| p.class.name }
37
+ end
38
+
39
+ def help
40
+ matchers.flat_map(&:help)
41
+ end
42
+
43
+ private
44
+
45
+ def process_msg(msg)
46
+ response = false
47
+ matchers.each do |matcher|
48
+ if matcher.attempt(msg)
49
+ response = matcher.response
50
+ break
51
+ end
52
+ end
53
+ response ? response : not_found(msg.command)
54
+ end
55
+
56
+ def process_string(message, metadata = {})
57
+ process_msg(Cognition::Message.new(message.strip, metadata))
58
+ end
59
+
60
+ def matchers
61
+ plugins.flat_map(&:matchers).compact
62
+ end
63
+
64
+ def plugins
65
+ @plugins ||= []
66
+ end
67
+
68
+ def not_found(message)
69
+ "No such command: #{message}\nUse 'help' for available commands!"
70
+ end
71
+ end
72
+ end
@@ -1,12 +1,13 @@
1
1
  module Cognition
2
2
  module Plugins
3
3
  class Base
4
- attr_accessor :matchers
4
+ attr_accessor :matchers, :bot
5
5
 
6
- def initialize
6
+ def initialize(bot = nil)
7
7
  @matchers = self.class.definitions.collect do |trigger, method_name, options|
8
8
  Matcher.new(trigger, options, &Proc.new(&method(method_name)))
9
9
  end
10
+ @bot = bot
10
11
  end
11
12
 
12
13
  def self.match(trigger, action, options = {})
@@ -16,9 +16,9 @@ module Cognition
16
16
 
17
17
  def help(msg, match_data = nil)
18
18
  if match_data['command'].empty?
19
- Cognition.help.join("\n")
19
+ bot.help.join("\n")
20
20
  else
21
- Cognition.help.find_all { |text| text.match match_data[:command] }.join("\n")
21
+ bot.help.find_all { |text| text.match match_data[:command] }.join("\n")
22
22
  end
23
23
  end
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module Cognition
2
- VERSION = "1.2.1"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -4,39 +4,39 @@ require_relative 'fixtures/hello'
4
4
 
5
5
  class CognitionTest < Minitest::Test
6
6
  def setup
7
- Cognition.reset
7
+ @bot = Cognition::Bot.new
8
8
  end
9
9
 
10
10
  def test_registers_plugins
11
- Cognition.register(Hello)
11
+ @bot.register(Hello)
12
12
 
13
- assert_equal 2, Cognition.plugin_names.count
13
+ assert_equal 2, @bot.plugin_names.count
14
14
  end
15
15
 
16
16
  def test_does_not_register_duplicate_plugins
17
- Cognition.register(Hello)
18
- Cognition.register(Hello)
17
+ @bot.register(Hello)
18
+ @bot.register(Hello)
19
19
 
20
- assert_equal 2, Cognition.plugin_names.count
20
+ assert_equal 2, @bot.plugin_names.count
21
21
  end
22
22
 
23
23
  def test_processes_messages
24
24
  msg = Cognition::Message.new('ping')
25
- assert_equal 'PONG', Cognition.process(msg)
25
+ assert_equal 'PONG', @bot.process(msg)
26
26
  end
27
27
 
28
28
  def test_processes_strings
29
- assert_equal 'PONG', Cognition.process('ping')
29
+ assert_equal 'PONG', @bot.process('ping')
30
30
  end
31
31
 
32
32
  def test_processes_strings_with_metadata
33
- assert_equal 'PONG', Cognition.process('ping', foo: 'bar')
33
+ assert_equal 'PONG', @bot.process('ping', foo: 'bar')
34
34
  end
35
35
 
36
36
  def test_shows_help_if_no_matches
37
- Cognition.register(Hello)
37
+ @bot.register(Hello)
38
38
  msg = Cognition::Message.new('pong')
39
- output = Cognition.process(msg)
39
+ output = @bot.process(msg)
40
40
  assert_match "No such command: pong\nUse 'help' for available commands!", output
41
41
  end
42
42
  end
@@ -3,16 +3,16 @@ require 'cognition'
3
3
 
4
4
  class DefaultPluginTest < Minitest::Test
5
5
  def setup
6
- Cognition.reset
6
+ @bot = Cognition::Bot.new
7
7
  end
8
8
 
9
9
  def test_returns_help
10
- help = "ping - Test if the endpoint is responding. Returns PONG.\\nhelp - Lists all commands with help\\nhelp <command> - Lists help for <command>"
11
- assert_equal help, Cognition.process("help")
10
+ help = "ping - Test if the endpoint is responding. Returns PONG.\nhelp - Lists all commands with help\nhelp <command> - Lists help for <command>"
11
+ assert_equal help, @bot.process("help")
12
12
  end
13
13
 
14
14
  def test_returns_filtered_help
15
15
  help = "ping - Test if the endpoint is responding. Returns PONG."
16
- assert_equal help, Cognition.process("help ping")
16
+ assert_equal help, @bot.process("help ping")
17
17
  end
18
18
  end
@@ -4,7 +4,8 @@ require 'cognition'
4
4
  class PluginTest < Minitest::Test
5
5
  def test_sets_matchers
6
6
  require_relative "fixtures/hello"
7
- hello = Hello.new
7
+ bot = Cognition::Bot.new
8
+ hello = Hello.new(bot)
8
9
 
9
10
  assert_equal 1, hello.matchers.count
10
11
  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.2.1
4
+ version: 2.0.0
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-03-02 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -94,6 +94,7 @@ files:
94
94
  - Rakefile
95
95
  - cognition.gemspec
96
96
  - lib/cognition.rb
97
+ - lib/cognition/bot.rb
97
98
  - lib/cognition/matcher.rb
98
99
  - lib/cognition/message.rb
99
100
  - lib/cognition/plugins/base.rb