cognition 1.1.4 → 1.2.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: d830169bf46e5d46d3a405eacf69b5d4a7701046
4
- data.tar.gz: 2193e7f57a5ab8f558a09455970c8b0bbc26a364
3
+ metadata.gz: e017dfacd05b7a99e572b9cdb40b2308513ce8bf
4
+ data.tar.gz: 839dc462b29e765ccd608511fc077e0ceeef03be
5
5
  SHA512:
6
- metadata.gz: 67846e449b3b3e2f4edb693ea3cdb779a40c54b820f659b6c033dd11d9e317d91ae65377175e9066914f95452b834c3b32c67b4fa7e69ce2ea2045996e6d469b
7
- data.tar.gz: 60764cd759ba2afa744be491655efbf73f068b0851ab647d84fe6e550102959aa8dff3140cd375d7b573edb23106d9189a29907bc57ec983685da1c54486ab43
6
+ metadata.gz: b4f926880513e1ec548ab07f2ea67244b51cdf92fb6db7aa735b011c9ef3e65c58732cb0d87e0aa86a0c659fd46905a2ea2e52d6ea148f46ecbf31f79054b8da
7
+ data.tar.gz: ac0d2c027685b9ad15eb532a4a1d9a5243241af4cc13ed73367f8146663cbda688bfc6b09da85929327304d020a7b5cc453de80e89203617067785cd4309ddf1
data/README.md CHANGED
@@ -40,6 +40,21 @@ msg = Cognition::Message('another command', {user_id: 15, name: 'Bob'})
40
40
  msg.metadata # Returns { user_id: 15, name: 'Bob' }
41
41
  ```
42
42
 
43
+ ### Special metadata
44
+ If you include a `callback_url` key in your metadata, we'll give you a
45
+ convenience method to reply to it using the `reply` method. This will
46
+ invoke a HTTParty POST back to the URL with your text sent as the
47
+ `content` variable.
48
+ ```ruby
49
+ msg = Cognition::Message('another command', {
50
+ callback_url: "http://foo.bar/baz",
51
+ user_id: 15,
52
+ name: 'Bob'
53
+ })
54
+
55
+ msg.reply("foo") # Posts 'content=foo' to http://foo.bar/baz
56
+ ```
57
+
43
58
  ## Creating a Plugin
44
59
  Creating plugins is easy. Subclass `Cognition::Plugins::Base` and setup your
45
60
  matches and logic that should be run:
@@ -50,20 +65,30 @@ class Hello < Cognition::Plugins::Base
50
65
 
51
66
  # Advanced Regexp based matcher. Capture groups are made available
52
67
  # via MatchData in the matches method
53
- match /hello\s*(?<name>.*)/, 'hello <name>', :hello_person
68
+ match /hello\s*(?<name>.*)/, :hello_person, help: {
69
+ 'hello <name>' => 'Greets you by name!'
70
+ }
54
71
 
55
72
 
56
73
  def hello(*)
57
74
  'Hello World'
58
75
  end
59
76
 
60
- def hello_person(msg)
61
- name = msg.matches[:name]
77
+ def hello_person(msg, match_data = nil)
78
+ name = match_data[:name]
62
79
  "Hello #{name}"
63
80
  end
64
81
  end
65
82
  ```
66
83
 
84
+ After you've done that, you will be able to do:
85
+ ```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"
90
+ ```
91
+
67
92
  ## Contributing
68
93
 
69
94
  1. Fork it ( https://github.com/anoldguy/cognition/fork )
data/lib/cognition.rb CHANGED
@@ -34,7 +34,7 @@ module Cognition
34
34
  end
35
35
 
36
36
  def help
37
- matchers.map(&:help)
37
+ matchers.flat_map(&:help)
38
38
  end
39
39
 
40
40
  private
@@ -2,14 +2,20 @@ module Cognition
2
2
  class Matcher
3
3
  attr_reader :trigger, :action, :response, :help, :match_data
4
4
 
5
- def initialize(trigger, help = 'Undocumented', &action)
5
+ def initialize(trigger, options = {}, &action)
6
6
  raise ArgumentError, 'matcher must have a trigger' unless trigger
7
7
  raise ArgumentError, 'matcher must have a action' unless action
8
8
  @trigger = trigger
9
- @help = help
9
+ @help = options[:help] ||= {}
10
10
  @action = action
11
11
  end
12
12
 
13
+ def help
14
+ @help.map do |command, description|
15
+ "#{command} - #{description}"
16
+ end
17
+ end
18
+
13
19
  def attempt(msg)
14
20
  return false unless matches?(msg)
15
21
 
@@ -4,13 +4,13 @@ module Cognition
4
4
  attr_accessor :matchers
5
5
 
6
6
  def initialize
7
- @matchers = self.class.definitions.collect do |trigger, help, method_name|
8
- Matcher.new(trigger, help, &Proc.new(&method(method_name)))
7
+ @matchers = self.class.definitions.collect do |trigger, method_name, options|
8
+ Matcher.new(trigger, options, &Proc.new(&method(method_name)))
9
9
  end
10
10
  end
11
11
 
12
- def self.match(trigger, help, action)
13
- definitions << [trigger, help, action]
12
+ def self.match(trigger, action, options = {})
13
+ definitions << [trigger, action, options]
14
14
  end
15
15
 
16
16
  def self.definitions
@@ -1,18 +1,24 @@
1
1
  module Cognition
2
2
  module Plugins
3
3
  class Default < Cognition::Plugins::Base
4
- match(/^ping/i, 'ping: Returns "PONG"', :pong)
5
- match(/^help\s*(?<command>.*)/i, 'help: Returns help text for registered plugins', :help)
4
+ match /^ping/i, :pong, help: {
5
+ 'ping' => 'Test if the endpoint is responding. Returns PONG.'
6
+ }
7
+
8
+ match /^help\s*(?<command>.*)/i, :help, help: {
9
+ 'help' => 'Lists all commands with help',
10
+ 'help <command>' => 'Lists help for <command>'
11
+ }
6
12
 
7
13
  def pong(msg, match_data = nil)
8
14
  'PONG'
9
15
  end
10
16
 
11
17
  def help(msg, match_data = nil)
12
- if match_data["command"].empty?
13
- Cognition.help.join("\n")
18
+ if match_data['command'].empty?
19
+ Cognition.help.join('\n')
14
20
  else
15
- Cognition.help.find_all { |text| text.match msg.matches[:command] }.join("\n")
21
+ Cognition.help.find_all { |text| text.match match_data[:command] }.join('\n')
16
22
  end
17
23
  end
18
24
  end
@@ -1,3 +1,3 @@
1
1
  module Cognition
2
- VERSION = "1.1.4"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -1,7 +1,9 @@
1
1
  class Hello < Cognition::Plugins::Base
2
- match 'hello', 'hello: Returns Hello World', :hello
2
+ match 'hello', :hello, help: {
3
+ 'hello' => 'Returns Hello World'
4
+ }
3
5
 
4
- def hello(msg)
6
+ def hello(msg, match_data = nil)
5
7
  'Hello World'
6
8
  end
7
9
  end
@@ -0,0 +1,18 @@
1
+ require 'minitest/autorun'
2
+ require 'cognition'
3
+
4
+ class DefaultPluginTest < Minitest::Test
5
+ def setup
6
+ Cognition.reset
7
+ end
8
+
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")
12
+ end
13
+
14
+ def test_returns_filtered_help
15
+ help = "ping - Test if the endpoint is responding. Returns PONG."
16
+ assert_equal help, Cognition.process("help ping")
17
+ end
18
+ end
data/test/test_matcher.rb CHANGED
@@ -16,54 +16,64 @@ class MatcherTest < Minitest::Test
16
16
 
17
17
  def test_matches_string
18
18
  msg = Cognition::Message.new('help')
19
- matcher = Cognition::Matcher.new('help', 'test', &Proc.new {})
19
+ matcher = Cognition::Matcher.new('help', {}, &Proc.new {})
20
20
  assert matcher.matches?(msg)
21
21
  end
22
22
 
23
23
  def test_string_fails_with_invalid_message
24
24
  msg = Cognition::Message.new('Help')
25
- matcher = Cognition::Matcher.new('help', 'test', &Proc.new {})
25
+ matcher = Cognition::Matcher.new('help', {}, &Proc.new {})
26
26
  refute matcher.matches?(msg)
27
27
  end
28
28
 
29
29
  def test_matches_regexp
30
30
  msg = Cognition::Message.new('ping')
31
- matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {})
31
+ matcher = Cognition::Matcher.new(/ping/, {}, &Proc.new {})
32
32
  assert matcher.matches?(msg)
33
33
  end
34
34
 
35
35
  def test_regexp_fails_with_invalid_message
36
36
  msg = Cognition::Message.new('pink')
37
- matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {})
37
+ matcher = Cognition::Matcher.new(/ping/, {}, &Proc.new {})
38
38
  refute matcher.matches?(msg)
39
39
  end
40
40
 
41
41
  def test_sets_response_on_attemp_if_matches
42
42
  msg = Cognition::Message.new('ping')
43
- matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {'PONG'})
43
+ matcher = Cognition::Matcher.new(/ping/, {}, &Proc.new {'PONG'})
44
44
  matcher.attempt(msg)
45
45
  assert_equal 'PONG', matcher.response
46
46
  end
47
47
 
48
48
  def test_returns_false_on_attemp_if_no_match
49
49
  msg = Cognition::Message.new('pink')
50
- matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {'PONG'})
50
+ matcher = Cognition::Matcher.new(/ping/, {}, &Proc.new {'PONG'})
51
51
  refute matcher.attempt(msg)
52
52
  end
53
53
 
54
54
  def test_sets_match_data
55
55
  msg = Cognition::Message.new('hello john')
56
- matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, 'test', &Proc.new {'PONG'})
56
+ matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, {}, &Proc.new {'PONG'})
57
57
  matcher.matches?(msg)
58
58
  assert_equal "john", matcher.match_data[:name]
59
59
  end
60
60
 
61
61
  def test_captures_response
62
62
  msg = Cognition::Message.new('hello john')
63
- matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, 'test', &Proc.new(&method(:dummy_method)))
63
+ matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, {}, &Proc.new(&method(:dummy_method)))
64
64
  matcher.attempt(msg)
65
65
  assert_equal "Hello john", matcher.response
66
66
  end
67
+
68
+ def test_only_sets_help_when_help_provided
69
+ matcher_without_help = Cognition::Matcher.new(/hello\s*(?<name>.*)/, {}, &Proc.new(&method(:dummy_method)))
70
+ assert_equal [], matcher_without_help.help
71
+ end
72
+
73
+ def test_sets_help
74
+ matcher_with_help = Cognition::Matcher.new(/hello\s*(?<name>.*)/, {help: {'hello' => 'says hello'}}, &Proc.new(&method(:dummy_method)))
75
+ assert_equal ['hello - says hello'], matcher_with_help.help
76
+ end
67
77
  end
68
78
 
69
79
  def dummy_method(msg, match_data)
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.1.4
4
+ version: 1.2.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-02-23 00:00:00.000000000 Z
11
+ date: 2015-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -102,6 +102,7 @@ files:
102
102
  - lib/cognition/version.rb
103
103
  - test/fixtures/hello.rb
104
104
  - test/test_cognition.rb
105
+ - test/test_default_plugin.rb
105
106
  - test/test_helper.rb
106
107
  - test/test_matcher.rb
107
108
  - test/test_message.rb
@@ -134,6 +135,7 @@ summary: A rules engine for running commands.
134
135
  test_files:
135
136
  - test/fixtures/hello.rb
136
137
  - test/test_cognition.rb
138
+ - test/test_default_plugin.rb
137
139
  - test/test_helper.rb
138
140
  - test/test_matcher.rb
139
141
  - test/test_message.rb