cognition 2.0.8 → 2.1.2

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
- SHA1:
3
- metadata.gz: 32aec8b43747dc3d4e93dde52b11976060dee440
4
- data.tar.gz: 29694fbdf558c9599f7867d14985ccd05e50bce0
2
+ SHA256:
3
+ metadata.gz: 69dc233f7a8848fc086d96634c1e5687655be914ef9da4a2f20b07bbc3f72950
4
+ data.tar.gz: ab407741b7a2e99fd230c3cea96ce5f2b45bb45f23cce75798109ff689535a2a
5
5
  SHA512:
6
- metadata.gz: f14281cbedf9470605b6fcf454b1f862c5f0e0a58297a0afa6e16f7279a04ffafdbfa71824a8963d6c0bd8ba900bbfc0c90513238c837107e3e6678886569e59
7
- data.tar.gz: b6bae92cad86c46c055d33ef13ca60f67f6159c8628d4710e195ae0f73e10db90510b5fada35a6d820c3aaaa313d4e3a200e95b3ecf172d39e1c23a2a1d3dc99
6
+ metadata.gz: df8f7e99e12f13fd2fba01593fd101423e15f699d4f202227bc9febe57865f8837db4cc26368b925df634d95226c38af833714ebd21167a1d986eeff617ef16d
7
+ data.tar.gz: 31862c0f869f4506468a0ba0ba1165be038d82c38f9fce563e6b70624b577e70a561c793577bcdb417a982e7e1f0f8886e73e28236e01c1439e6ff955986521a
data/README.md CHANGED
@@ -62,7 +62,9 @@ msg.reply("foo") # Posts 'content=foo' to http://foo.bar/baz
62
62
 
63
63
  ## Creating a Plugin
64
64
  Creating plugins is easy. Subclass `Cognition::Plugins::Base` and setup your
65
- matches and logic that should be run:
65
+ matches and logic that should be run. Your methods will be passed the `msg`
66
+ object and any `metadata` you've passed to the bot's `process` method. Here's
67
+ an example plugin:
66
68
  ```ruby
67
69
  class Hello < Cognition::Plugins::Base
68
70
  # Simple string based matcher. Must match *EXACTLY*
@@ -18,12 +18,12 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "httparty", "~> 0.8"
22
- spec.add_dependency "tilt", "~> 1.4"
21
+ spec.add_dependency "httparty", "~> 0.15"
22
+ spec.add_dependency "tilt", "~> 2.0"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.7"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "minitest", "~> 5.0"
27
27
  spec.add_development_dependency "webmock", "~> 1.20"
28
- spec.add_development_dependency "haml", "~> 4.0"
28
+ spec.add_development_dependency "haml", "~> 5.0"
29
29
  end
@@ -45,7 +45,7 @@ module Cognition
45
45
  def process_msg(msg)
46
46
  response = false
47
47
  matchers.each do |matcher|
48
- if matcher.attempt(msg)
48
+ if matcher.attempt(msg) != false
49
49
  response = matcher.response
50
50
  break
51
51
  end
@@ -4,7 +4,7 @@ module Cognition
4
4
 
5
5
  def initialize(trigger, options = {}, &action)
6
6
  fail ArgumentError, "matcher must have a trigger" unless trigger
7
- fail ArgumentError, "matcher must have a action" unless action
7
+ fail ArgumentError, "matcher must have an action" unless action
8
8
  @trigger = trigger
9
9
  @help = options[:help] ||= {}
10
10
  @action = action
@@ -18,12 +18,13 @@ module Cognition
18
18
 
19
19
  def attempt(msg)
20
20
  return false unless matches?(msg)
21
-
22
21
  run(msg)
23
22
  end
24
23
 
25
24
  def run(msg)
26
- @response = action.call(msg, match_data)
25
+ @response = action.call(msg, match_data).to_s
26
+ rescue => e
27
+ @response = "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
27
28
  end
28
29
 
29
30
  def matches?(msg)
@@ -19,7 +19,7 @@ module Cognition
19
19
  def help(msg, match_data = nil)
20
20
  type = msg.metadata[:type] ||= "text"
21
21
  if match_data[:command].nil? || match_data[:command].empty?
22
- @help = bot.help
22
+ @help = bot.help.sort
23
23
  else
24
24
  @help = bot.help.find_all { |text| text.match match_data[:command] }
25
25
  end
@@ -1,3 +1,3 @@
1
1
  module Cognition
2
- VERSION = "2.0.8"
2
+ VERSION = "2.1.2"
3
3
  end
@@ -7,7 +7,7 @@ class DefaultPluginTest < Minitest::Test
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>"
10
+ help = "help - Lists all commands with help\nhelp <command> - Lists help for <command>\nping - Test if the endpoint is responding. Returns PONG."
11
11
  assert_equal help, @bot.process("help")
12
12
  end
13
13
 
@@ -74,8 +74,26 @@ class MatcherTest < Minitest::Test
74
74
  matcher_with_help = Cognition::Matcher.new(/hello\s*(?<name>.*)/, { help: { "hello" => "says hello" } }, &proc(&method(:dummy_method)))
75
75
  assert_equal ["hello - says hello"], matcher_with_help.help
76
76
  end
77
+
78
+ def test_raises_exception_when_command_fails
79
+ msg = Cognition::Message.new("boom")
80
+ matcher = Cognition::Matcher.new(/boom/, {}, &proc(&method(:blow_up)))
81
+ matcher.attempt(msg)
82
+ assert matcher.response.include? "ZeroDivisionError"
83
+ end
84
+
85
+ def test_empty_response_is_not_a_failure
86
+ msg = Cognition::Message.new("empty")
87
+ matcher = Cognition::Matcher.new(/empty/, {}, &proc {})
88
+ matcher.attempt(msg)
89
+ assert_equal "", matcher.response
90
+ end
77
91
  end
78
92
 
79
93
  def dummy_method(_, match_data)
80
94
  "Hello #{match_data['name']}"
81
95
  end
96
+
97
+ def blow_up(msg, match_data)
98
+ raise ZeroDivisionError, "divided by 0"
99
+ 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: 2.0.8
4
+ version: 2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Anderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-14 00:00:00.000000000 Z
11
+ date: 2018-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.8'
19
+ version: '0.15'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.8'
26
+ version: '0.15'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: tilt
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.4'
33
+ version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.4'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '4.0'
103
+ version: '5.0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '4.0'
110
+ version: '5.0'
111
111
  description: Match text; run commands. Works great for building a chatbot!
112
112
  email:
113
113
  - andnat@gmail.com
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  version: '0'
170
170
  requirements: []
171
171
  rubyforge_project:
172
- rubygems_version: 2.5.1
172
+ rubygems_version: 2.7.6
173
173
  signing_key:
174
174
  specification_version: 4
175
175
  summary: A rules engine for running commands.