cognition 1.0.3 → 1.1.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 +4 -4
- data/cognition.gemspec +3 -0
- data/lib/cognition/matcher.rb +3 -3
- data/lib/cognition/message.rb +7 -2
- data/lib/cognition/plugins/default.rb +3 -3
- data/lib/cognition/responder.rb +18 -0
- data/lib/cognition/version.rb +1 -1
- data/lib/cognition.rb +1 -0
- data/test/fixtures/hello.rb +1 -1
- data/test/test_helper.rb +5 -0
- data/test/test_matcher.rb +13 -2
- data/test/test_message.rb +10 -0
- data/test/test_responder.rb +20 -0
- metadata +35 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc7111ee4e33d5d94ad94ae8ee6e8e6c5466e539
|
4
|
+
data.tar.gz: d1f63422b1a9c8e672b1b4f024fcf4f0e77a5853
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ea98203f67db1f9f1bc18b3133e1f342ad90b11f76af75cde294ecf7ac54e85067f61a52db318551e732f93abec06c0a411d9661864dfc677d8f2f92e0a705e
|
7
|
+
data.tar.gz: ba51ff97c11f54b57b15747d48b89ba707d2e231ea4d6613dcd576b807d8a63d7ce16527d75cfac9563a4415a2fc7bfd78ddfb52160798afc0c58cf34396298c
|
data/cognition.gemspec
CHANGED
@@ -18,7 +18,10 @@ 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.13.3"
|
22
|
+
|
21
23
|
spec.add_development_dependency "bundler", "~> 1.7"
|
22
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
25
|
spec.add_development_dependency "minitest", "~> 5.0.0"
|
26
|
+
spec.add_development_dependency "webmock", "~> 1.20.4"
|
24
27
|
end
|
data/lib/cognition/matcher.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Cognition
|
2
2
|
class Matcher
|
3
|
-
|
3
|
+
attr_reader :trigger, :action, :response, :help, :match_data
|
4
4
|
|
5
5
|
def initialize(trigger, help = 'Undocumented', &action)
|
6
6
|
raise ArgumentError, 'matcher must have a trigger' unless trigger
|
@@ -17,7 +17,7 @@ module Cognition
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def run(msg)
|
20
|
-
@response = action.call(msg)
|
20
|
+
@response = action.call(msg, match_data)
|
21
21
|
end
|
22
22
|
|
23
23
|
def matches?(msg)
|
@@ -25,7 +25,7 @@ module Cognition
|
|
25
25
|
when String
|
26
26
|
trigger == msg.command
|
27
27
|
when Regexp
|
28
|
-
|
28
|
+
@match_data = trigger.match msg.command
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
data/lib/cognition/message.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
module Cognition
|
2
2
|
class Message
|
3
|
-
attr_reader :command, :metadata
|
4
|
-
attr_accessor :matches
|
3
|
+
attr_reader :command, :metadata, :responder
|
5
4
|
|
6
5
|
def initialize(command, metadata = {})
|
7
6
|
@command = command
|
8
7
|
@metadata = metadata
|
8
|
+
@responder = Cognition::Responder.new(metadata[:callback_url]) if metadata[:callback_url]
|
9
|
+
end
|
10
|
+
|
11
|
+
def reply(text)
|
12
|
+
return "No Callback URL provided" unless @responder
|
13
|
+
@responder.reply(text)
|
9
14
|
end
|
10
15
|
end
|
11
16
|
end
|
@@ -4,12 +4,12 @@ module Cognition
|
|
4
4
|
match(/^ping/i, 'ping: Returns "PONG"', :pong)
|
5
5
|
match(/^help\s*(?<command>.*)/i, 'help: Returns help text for registered plugins', :help)
|
6
6
|
|
7
|
-
def pong(
|
7
|
+
def pong(msg, match_data = nil)
|
8
8
|
'PONG'
|
9
9
|
end
|
10
10
|
|
11
|
-
def help(msg)
|
12
|
-
if
|
11
|
+
def help(msg, match_data = nil)
|
12
|
+
if match_data["command"].empty?
|
13
13
|
Cognition.help.join("\n")
|
14
14
|
else
|
15
15
|
Cognition.help.find_all { |text| text.match msg.matches[:command] }.join("\n")
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
module Cognition
|
3
|
+
class Responder
|
4
|
+
include HTTParty
|
5
|
+
attr_reader :url
|
6
|
+
|
7
|
+
def initialize(uri)
|
8
|
+
@options = { timeout: 5 }
|
9
|
+
@uri = uri
|
10
|
+
end
|
11
|
+
|
12
|
+
def reply(text)
|
13
|
+
self.class.post(@uri, @options.merge({ content: text }))
|
14
|
+
rescue Timeout::Error => e
|
15
|
+
"Request to #{@uri} timed out."
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/cognition/version.rb
CHANGED
data/lib/cognition.rb
CHANGED
data/test/fixtures/hello.rb
CHANGED
data/test/test_helper.rb
ADDED
data/test/test_matcher.rb
CHANGED
@@ -51,10 +51,21 @@ class MatcherTest < Minitest::Test
|
|
51
51
|
refute matcher.attempt(msg)
|
52
52
|
end
|
53
53
|
|
54
|
-
def
|
54
|
+
def test_sets_match_data
|
55
55
|
msg = Cognition::Message.new('hello john')
|
56
56
|
matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, 'test', &Proc.new {'PONG'})
|
57
|
+
matcher.matches?(msg)
|
58
|
+
assert_equal "john", matcher.match_data[:name]
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_captures_response
|
62
|
+
msg = Cognition::Message.new('hello john')
|
63
|
+
matcher = Cognition::Matcher.new(/hello\s*(?<name>.*)/, 'test', &Proc.new(&method(:dummy_method)))
|
57
64
|
matcher.attempt(msg)
|
58
|
-
assert_equal "john",
|
65
|
+
assert_equal "Hello john", matcher.response
|
59
66
|
end
|
60
67
|
end
|
68
|
+
|
69
|
+
def dummy_method(msg, match_data)
|
70
|
+
"Hello #{match_data['name']}"
|
71
|
+
end
|
data/test/test_message.rb
CHANGED
@@ -7,4 +7,14 @@ class CognitionTest < Minitest::Test
|
|
7
7
|
assert_equal 15, msg.metadata[:user_id]
|
8
8
|
assert_equal 'bar', msg.metadata[:foo]
|
9
9
|
end
|
10
|
+
|
11
|
+
def test_sets_responder_if_callback_url
|
12
|
+
msg = Cognition::Message.new('ping', callback_url: 'http://foo.bar/')
|
13
|
+
assert_kind_of Cognition::Responder, msg.responder
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_no_responder_if_no_callback_url
|
17
|
+
msg = Cognition::Message.new('ping', user: { name: 'foo', id: 123456 })
|
18
|
+
refute msg.responder
|
19
|
+
end
|
10
20
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'cognition'
|
3
|
+
require 'test_helper'
|
4
|
+
|
5
|
+
class ResponderTest < Minitest::Test
|
6
|
+
def test_sends_reply
|
7
|
+
stub_request(:any, "http://foo.bar/path").
|
8
|
+
to_return(status: 200)
|
9
|
+
responder = Cognition::Responder.new('http://foo.bar/path')
|
10
|
+
|
11
|
+
assert_equal 200, responder.reply('foobar').code
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_handles_timeouts
|
15
|
+
stub_request(:any, "http://foo.bar/path").to_timeout
|
16
|
+
responder = Cognition::Responder.new('http://foo.bar/path')
|
17
|
+
|
18
|
+
assert_equal 'Request to http://foo.bar/path timed out.', responder.reply('foobar')
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cognition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.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-
|
11
|
+
date: 2015-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.13.3
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.13.3
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +66,20 @@ dependencies:
|
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
68
|
version: 5.0.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 1.20.4
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.20.4
|
55
83
|
description: Match text; run commands. Works great for building a chatbot!
|
56
84
|
email:
|
57
85
|
- andnat@gmail.com
|
@@ -70,12 +98,15 @@ files:
|
|
70
98
|
- lib/cognition/message.rb
|
71
99
|
- lib/cognition/plugins/base.rb
|
72
100
|
- lib/cognition/plugins/default.rb
|
101
|
+
- lib/cognition/responder.rb
|
73
102
|
- lib/cognition/version.rb
|
74
103
|
- test/fixtures/hello.rb
|
75
104
|
- test/test_cognition.rb
|
105
|
+
- test/test_helper.rb
|
76
106
|
- test/test_matcher.rb
|
77
107
|
- test/test_message.rb
|
78
108
|
- test/test_plugin.rb
|
109
|
+
- test/test_responder.rb
|
79
110
|
homepage: https://github.com/anoldguy/Cognition
|
80
111
|
licenses:
|
81
112
|
- MIT
|
@@ -103,6 +134,8 @@ summary: A rules engine for running commands.
|
|
103
134
|
test_files:
|
104
135
|
- test/fixtures/hello.rb
|
105
136
|
- test/test_cognition.rb
|
137
|
+
- test/test_helper.rb
|
106
138
|
- test/test_matcher.rb
|
107
139
|
- test/test_message.rb
|
108
140
|
- test/test_plugin.rb
|
141
|
+
- test/test_responder.rb
|