cognition 1.0.1 → 1.0.3

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: 0c351675cea9e838d9bc3af6a197ea43e204e5a4
4
- data.tar.gz: 6136d88fc7e55bd093459ff2f7f9d58ceb81973f
3
+ metadata.gz: 0e50eb1ebaaaa6fdfc1c8d760cf3bad97359bf14
4
+ data.tar.gz: 15e38a680a5cba0aa9652a4d47597afffdeee25a
5
5
  SHA512:
6
- metadata.gz: d4919b519fdd119a480fe0944bd95a9d95237bf3f6c9ef7cf6eb5c913934d9dd5d537767a8e710cc2c2bc10892b3f6562040384a91c01f633e8b2f51d9997a85
7
- data.tar.gz: cbade8a82a96f76504c0c44d0444e8601be9ccd6533164653fa475d70ffffdfda0133ad332d50e411cdb25ba661672bfec7ffe2890f39cea359b9957331766fa
6
+ metadata.gz: f67e1f05ff1a2862dbb4fb501b8cb31be1fe62b741083fbffff997c2310849ed5ce44e8f4376eabc5725e45c0063a1db372b5660ad7dfc20318f691fe56d772d
7
+ data.tar.gz: d4f667e367fef56e5b43168de85346bcc918ba1f69ed575f71cd823eeaa0231744c9b31c621e4e87c3ef6a9f9387a141292aa77efeae996d65280c1ed5104886
data/README.md CHANGED
@@ -45,11 +45,22 @@ Creating plugins is easy. Subclass `Cognition::Plugins::Base` and setup your
45
45
  matches and logic that should be run:
46
46
  ```ruby
47
47
  class Hello < Cognition::Plugins::Base
48
+ # Simple string based matcher. Must match *EXACTLY*
48
49
  match 'hello', 'hello: Returns Hello World', :hello
49
50
 
51
+ # Advanced Regexp based matcher. Capture groups are made available
52
+ # via MatchData in the matches method
53
+ match /hello\s*(?<name>.*)/, 'hello <name>', :hello_person
54
+
55
+
50
56
  def hello(*)
51
57
  'Hello World'
52
58
  end
59
+
60
+ def hello_person(msg)
61
+ name = msg.matches[:name]
62
+ "Hello #{name}"
63
+ end
53
64
  end
54
65
  ```
55
66
 
@@ -25,7 +25,7 @@ module Cognition
25
25
  when String
26
26
  trigger == msg.command
27
27
  when Regexp
28
- trigger.match msg.command
28
+ msg.matches = trigger.match msg.command
29
29
  end
30
30
  end
31
31
  end
@@ -1,6 +1,7 @@
1
1
  module Cognition
2
2
  class Message
3
3
  attr_reader :command, :metadata
4
+ attr_accessor :matches
4
5
 
5
6
  def initialize(command, metadata = {})
6
7
  @command = command
@@ -0,0 +1,20 @@
1
+ module Cognition
2
+ module Plugins
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)
6
+
7
+ def pong(*)
8
+ 'PONG'
9
+ end
10
+
11
+ def help(msg)
12
+ if msg.matches["command"].empty?
13
+ Cognition.help.join("\n")
14
+ else
15
+ Cognition.help.find_all { |text| text.match msg.matches[:command] }.join("\n")
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Cognition
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.3"
3
3
  end
data/lib/cognition.rb CHANGED
@@ -2,7 +2,7 @@ require 'cognition/version'
2
2
  require 'cognition/message'
3
3
  require 'cognition/matcher'
4
4
  require 'cognition/plugins/base'
5
- require 'cognition/plugins/ping'
5
+ require 'cognition/plugins/default'
6
6
 
7
7
  module Cognition
8
8
  extend self
@@ -25,7 +25,7 @@ module Cognition
25
25
  def reset
26
26
  @matchers = []
27
27
  @plugins = []
28
- register(Cognition::Plugins::Ping)
28
+ register(Cognition::Plugins::Default)
29
29
  end
30
30
 
31
31
  def plugin_names
@@ -33,7 +33,7 @@ module Cognition
33
33
  end
34
34
 
35
35
  def help
36
- matchers.map(&:help).join('\n')
36
+ matchers.map(&:help)
37
37
  end
38
38
 
39
39
  private
@@ -50,7 +50,7 @@ module Cognition
50
50
  end
51
51
 
52
52
  def process_string(message, metadata = {})
53
- process_msg(Cognition::Message.new(message, metadata))
53
+ process_msg(Cognition::Message.new(message.strip, metadata))
54
54
  end
55
55
 
56
56
  def matchers
@@ -66,5 +66,5 @@ module Cognition
66
66
  end
67
67
  end
68
68
 
69
- # Default plugin, responds to 'ping' with 'PONG'
70
- Cognition.register(Cognition::Plugins::Ping)
69
+ # Default plugin, responds to 'ping' with 'PONG' and provides help text
70
+ Cognition.register(Cognition::Plugins::Default)
data/test/test_matcher.rb CHANGED
@@ -50,4 +50,11 @@ class MatcherTest < Minitest::Test
50
50
  matcher = Cognition::Matcher.new(/ping/, 'test', &Proc.new {'PONG'})
51
51
  refute matcher.attempt(msg)
52
52
  end
53
+
54
+ def test_sets_matches_on_message
55
+ msg = Cognition::Message.new('hello john')
56
+ matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, 'test', &Proc.new {'PONG'})
57
+ matcher.attempt(msg)
58
+ assert_equal "john", msg.matches[:name]
59
+ end
53
60
  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.1
4
+ version: 1.0.3
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-21 00:00:00.000000000 Z
11
+ date: 2015-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -69,7 +69,7 @@ files:
69
69
  - lib/cognition/matcher.rb
70
70
  - lib/cognition/message.rb
71
71
  - lib/cognition/plugins/base.rb
72
- - lib/cognition/plugins/ping.rb
72
+ - lib/cognition/plugins/default.rb
73
73
  - lib/cognition/version.rb
74
74
  - test/fixtures/hello.rb
75
75
  - test/test_cognition.rb
@@ -1,11 +0,0 @@
1
- module Cognition
2
- module Plugins
3
- class Ping < Cognition::Plugins::Base
4
- match(/ping/i, 'ping: Returns "PONG"', :pong)
5
-
6
- def pong(*)
7
- 'PONG'
8
- end
9
- end
10
- end
11
- end