cognition 2.0.0 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +40 -0
- data/cognition.gemspec +5 -3
- data/lib/cognition/plugins/base.rb +50 -0
- data/lib/cognition/plugins/cognition/plugins/default/views/help.html.erb +7 -0
- data/lib/cognition/plugins/cognition/plugins/default/views/help.text.erb +1 -0
- data/lib/cognition/plugins/default.rb +5 -2
- data/lib/cognition/version.rb +1 -1
- data/test/fixtures/hello/views/bonjour.text.erb +1 -0
- data/test/fixtures/hello/views/hey.html.haml +1 -0
- data/test/fixtures/hello/views/hi.html.erb +1 -0
- data/test/fixtures/hello/views/hola.haml +1 -0
- data/test/fixtures/hello/views/yo.html.erb +3 -0
- data/test/fixtures/hello.rb +26 -0
- data/test/test_plugin.rb +30 -3
- metadata +48 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f4da3fb15fa2cf643a7e989c81e5467cd35284d
|
4
|
+
data.tar.gz: 9c440ceee3f1cca18f71c0485a57723248e3f139
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 866e3d8727d945165488ed69e611ca84f1063acab94061464d5c28b41087f445a33f0d1ce3f927ba7a6cf13e6d2515668cd67179fcdef87649144ca83cadd5d2
|
7
|
+
data.tar.gz: 47be6d5c494c3db31744c47b72848e9b00f70fa470a8d82c2f7d57ad32255f9cd17a21cfc3aa50d099349a607aa07c97dbf1bcaf2c798bd03e792eaf7425f999
|
data/README.md
CHANGED
@@ -94,6 +94,46 @@ bot.process("hello") # "Hello World"
|
|
94
94
|
bot.process("hello foo") # "Hello foo"
|
95
95
|
```
|
96
96
|
|
97
|
+
### Rendering templates
|
98
|
+
Templates are opt-in right now, you need to call `render` yourself, and it
|
99
|
+
will return a string with the rendered contents of a template. What template,
|
100
|
+
you ask? The default for `/path/to/hello.rb` will look for a templates in
|
101
|
+
`/path/to/hello/views/`.
|
102
|
+
|
103
|
+
Given the following plugin:
|
104
|
+
```ruby
|
105
|
+
class Hello < Cognition::Plugins::Base
|
106
|
+
# ...snipped
|
107
|
+
|
108
|
+
def hello(*)
|
109
|
+
render
|
110
|
+
end
|
111
|
+
|
112
|
+
def hi(*)
|
113
|
+
render(template: "/path/to/template.html.erb")
|
114
|
+
end
|
115
|
+
|
116
|
+
def hey(*)
|
117
|
+
render(type: "text", extension: "haml")
|
118
|
+
end
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
1. The `hello` method will look for `/path/to/hello/views/hello.html.erb`
|
123
|
+
2. The `hi` method will look for `/path/to/template.html.erb`
|
124
|
+
3. The `hey` method will look for `/path/to/hello/views/hey.text.haml`
|
125
|
+
|
126
|
+
Setting instance variables or passing locals is up to the plugin creator.
|
127
|
+
The `render` method takes a hash with the following keys:
|
128
|
+
```ruby
|
129
|
+
{
|
130
|
+
template: "full/path/to/template/file", # FULL path to template file
|
131
|
+
type: "type of response" # text, html, json, etc...
|
132
|
+
extension: "engine file extension" # erb, haml, etc...
|
133
|
+
locals: {x: "foo", y: "bar"} # local variables, access as x & y
|
134
|
+
}
|
135
|
+
```
|
136
|
+
|
97
137
|
## Contributing
|
98
138
|
|
99
139
|
1. Fork it ( https://github.com/anoldguy/cognition/fork )
|
data/cognition.gemspec
CHANGED
@@ -18,10 +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
|
21
|
+
spec.add_dependency "httparty", "~> 0.8"
|
22
|
+
spec.add_dependency "tilt", "~> 1.4"
|
22
23
|
|
23
24
|
spec.add_development_dependency "bundler", "~> 1.7"
|
24
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
-
spec.add_development_dependency "minitest", "~> 5.0
|
26
|
-
spec.add_development_dependency "webmock", "~> 1.20
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
spec.add_development_dependency "webmock", "~> 1.20"
|
28
|
+
spec.add_development_dependency "haml", "~> 4.0"
|
27
29
|
end
|
@@ -1,6 +1,27 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'tilt'
|
3
|
+
|
1
4
|
module Cognition
|
2
5
|
module Plugins
|
6
|
+
class PluginTemplateNotFound < StandardError; end
|
7
|
+
|
3
8
|
class Base
|
9
|
+
class <<self
|
10
|
+
attr_accessor :view_root
|
11
|
+
end
|
12
|
+
|
13
|
+
RENDER_DEFAULTS = {
|
14
|
+
type: "html",
|
15
|
+
extension: "erb"
|
16
|
+
}
|
17
|
+
|
18
|
+
# Inherited callback to set a class-level instance variable on the
|
19
|
+
# subclass, since we can't use __FILE__ here, and have it use the
|
20
|
+
# subclass's location.
|
21
|
+
def self.inherited(plugin)
|
22
|
+
plugin.view_root = File.dirname(caller[0].split(':',2).first)
|
23
|
+
end
|
24
|
+
|
4
25
|
attr_accessor :matchers, :bot
|
5
26
|
|
6
27
|
def initialize(bot = nil)
|
@@ -17,6 +38,35 @@ module Cognition
|
|
17
38
|
def self.definitions
|
18
39
|
@definitions ||= []
|
19
40
|
end
|
41
|
+
|
42
|
+
def render(opts = {})
|
43
|
+
options = RENDER_DEFAULTS.merge(opts)
|
44
|
+
calling_method = caller[0][/`([^']*)'/, 1]
|
45
|
+
template = options[:template] || template_file(calling_method, options[:type], options[:extension])
|
46
|
+
Tilt.new(template).render(self, options[:locals])
|
47
|
+
rescue Errno::ENOENT => e
|
48
|
+
raise PluginTemplateNotFound, e
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def template_file(name, type, extension)
|
54
|
+
# Defaults to html ERB for now. Override when calling #render(path_to_file)
|
55
|
+
File.join(templates_path, "#{name}.#{type}.#{extension}")
|
56
|
+
end
|
57
|
+
|
58
|
+
def underscore(string)
|
59
|
+
word = string.to_s.gsub(/::/, '/')
|
60
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
|
61
|
+
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
|
62
|
+
word.tr!("-", "_")
|
63
|
+
word.downcase!
|
64
|
+
word
|
65
|
+
end
|
66
|
+
|
67
|
+
def templates_path
|
68
|
+
File.expand_path("#{underscore(self.class.name)}/views", self.class.view_root)
|
69
|
+
end
|
20
70
|
end
|
21
71
|
end
|
22
72
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @help.join("\n") %>
|
@@ -15,11 +15,14 @@ module Cognition
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def help(msg, match_data = nil)
|
18
|
+
type = msg.metadata[:type] ||= "text"
|
18
19
|
if match_data['command'].empty?
|
19
|
-
bot.help
|
20
|
+
@help = bot.help
|
20
21
|
else
|
21
|
-
bot.help.find_all { |text| text.match match_data[:command] }
|
22
|
+
@help = bot.help.find_all { |text| text.match match_data[:command] }
|
23
|
+
|
22
24
|
end
|
25
|
+
render(type: type)
|
23
26
|
end
|
24
27
|
end
|
25
28
|
end
|
data/lib/cognition/version.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
<%= @message %> is french!
|
@@ -0,0 +1 @@
|
|
1
|
+
%h1= @message
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= @message %> from a default template!
|
@@ -0,0 +1 @@
|
|
1
|
+
%h1= @message
|
data/test/fixtures/hello.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'haml'
|
1
2
|
class Hello < Cognition::Plugins::Base
|
2
3
|
match 'hello', :hello, help: {
|
3
4
|
'hello' => 'Returns Hello World'
|
@@ -6,4 +7,29 @@ class Hello < Cognition::Plugins::Base
|
|
6
7
|
def hello(msg, match_data = nil)
|
7
8
|
'Hello World'
|
8
9
|
end
|
10
|
+
|
11
|
+
def hi(msg, match_data = nil)
|
12
|
+
@message = 'Hi'
|
13
|
+
render
|
14
|
+
end
|
15
|
+
|
16
|
+
def hola(msg, match_data = nil)
|
17
|
+
@message = 'Hola'
|
18
|
+
render(template: File.expand_path('../hello/views/hola.haml', __FILE__))
|
19
|
+
end
|
20
|
+
|
21
|
+
def bonjour(msg, match_data = nil)
|
22
|
+
@message = 'Bonjour'
|
23
|
+
render(type: "text")
|
24
|
+
end
|
25
|
+
|
26
|
+
def hey(msg, match_data = nil)
|
27
|
+
@message = 'Hey'
|
28
|
+
render(extension: "haml")
|
29
|
+
end
|
30
|
+
|
31
|
+
def yo(msg, match_data = nil)
|
32
|
+
@message = 'Yo'
|
33
|
+
render(locals: {num_yos: 3})
|
34
|
+
end
|
9
35
|
end
|
data/test/test_plugin.rb
CHANGED
@@ -2,11 +2,38 @@ require 'minitest/autorun'
|
|
2
2
|
require 'cognition'
|
3
3
|
|
4
4
|
class PluginTest < Minitest::Test
|
5
|
-
def
|
5
|
+
def setup
|
6
6
|
require_relative "fixtures/hello"
|
7
7
|
bot = Cognition::Bot.new
|
8
|
-
hello = Hello.new(bot)
|
8
|
+
@hello = Hello.new(bot)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_sets_matchers
|
12
|
+
assert_equal 1, @hello.matchers.count
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_renders_default_template
|
16
|
+
assert_equal "Hi from a default template!\n", @hello.hi("foo")
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_renders_passed_template
|
20
|
+
assert_equal "<h1>Hola</h1>\n", @hello.hola("foo")
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_renders_specified_type
|
24
|
+
assert_equal "Bonjour is french!\n", @hello.bonjour("foo")
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_renders_specified_extension
|
28
|
+
assert_equal "<h1>Hey</h1>\n", @hello.hey("foo")
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_renders_with_locals
|
32
|
+
assert_equal "<p>Yo</p>\n<p>Yo</p>\n<p>Yo</p>\n", @hello.yo("foo")
|
33
|
+
end
|
9
34
|
|
10
|
-
|
35
|
+
def test_multiple_render_calls
|
36
|
+
assert_equal "<p>Yo</p>\n<p>Yo</p>\n<p>Yo</p>\n", @hello.yo("foo")
|
37
|
+
assert_equal "<h1>Hey</h1>\n", @hello.hey("foo")
|
11
38
|
end
|
12
39
|
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.
|
4
|
+
version: 2.0.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: 2015-03-
|
11
|
+
date: 2015-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -16,14 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.8
|
19
|
+
version: '0.8'
|
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.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: tilt
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.4'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,28 +72,42 @@ dependencies:
|
|
58
72
|
requirements:
|
59
73
|
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: 5.0
|
75
|
+
version: '5.0'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: 5.0
|
82
|
+
version: '5.0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: webmock
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
87
|
- - "~>"
|
74
88
|
- !ruby/object:Gem::Version
|
75
|
-
version: 1.20
|
89
|
+
version: '1.20'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.20'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: haml
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '4.0'
|
76
104
|
type: :development
|
77
105
|
prerelease: false
|
78
106
|
version_requirements: !ruby/object:Gem::Requirement
|
79
107
|
requirements:
|
80
108
|
- - "~>"
|
81
109
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
110
|
+
version: '4.0'
|
83
111
|
description: Match text; run commands. Works great for building a chatbot!
|
84
112
|
email:
|
85
113
|
- andnat@gmail.com
|
@@ -98,10 +126,17 @@ files:
|
|
98
126
|
- lib/cognition/matcher.rb
|
99
127
|
- lib/cognition/message.rb
|
100
128
|
- lib/cognition/plugins/base.rb
|
129
|
+
- lib/cognition/plugins/cognition/plugins/default/views/help.html.erb
|
130
|
+
- lib/cognition/plugins/cognition/plugins/default/views/help.text.erb
|
101
131
|
- lib/cognition/plugins/default.rb
|
102
132
|
- lib/cognition/responder.rb
|
103
133
|
- lib/cognition/version.rb
|
104
134
|
- test/fixtures/hello.rb
|
135
|
+
- test/fixtures/hello/views/bonjour.text.erb
|
136
|
+
- test/fixtures/hello/views/hey.html.haml
|
137
|
+
- test/fixtures/hello/views/hi.html.erb
|
138
|
+
- test/fixtures/hello/views/hola.haml
|
139
|
+
- test/fixtures/hello/views/yo.html.erb
|
105
140
|
- test/test_cognition.rb
|
106
141
|
- test/test_default_plugin.rb
|
107
142
|
- test/test_helper.rb
|
@@ -135,6 +170,11 @@ specification_version: 4
|
|
135
170
|
summary: A rules engine for running commands.
|
136
171
|
test_files:
|
137
172
|
- test/fixtures/hello.rb
|
173
|
+
- test/fixtures/hello/views/bonjour.text.erb
|
174
|
+
- test/fixtures/hello/views/hey.html.haml
|
175
|
+
- test/fixtures/hello/views/hi.html.erb
|
176
|
+
- test/fixtures/hello/views/hola.haml
|
177
|
+
- test/fixtures/hello/views/yo.html.erb
|
138
178
|
- test/test_cognition.rb
|
139
179
|
- test/test_default_plugin.rb
|
140
180
|
- test/test_helper.rb
|