lita-answers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 297fd6da8e062c56e013e86abcf600af325cd041
4
+ data.tar.gz: 0b6ce0b9f3a766f6a59398673ef77bcdbea102c7
5
+ SHA512:
6
+ metadata.gz: e8b47b804c391ed7c9b147c6735fd56680bc81df436b0b7b22ea396de812c1c708b193fb7365f7479eec7daaf1ce23ee05bacc256843d0dbe94b32dc6de0d4cf
7
+ data.tar.gz: 782ed8d1cc103f0ba5611002820615640369bf546a54d3637a7c1d35fe41ee0783e9a9ce0e057fd46ae10716c43495d10e91e3e60010245759492687f46b56a4
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .ruby-gemset
20
+ dump.rdb
21
+ lita_config.rb
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 Sergey Alekseev
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ # lita-answers
2
+
3
+ A Lita handler for a question answering (QA) system. Allows you to use CRUD operations.
4
+ More functionality is coming soon...
5
+
6
+ ## Installation
7
+
8
+ Add lita-answers to your Lita instance's Gemfile:
9
+
10
+ ``` ruby
11
+ gem "lita-answers"
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ You could perform simple CRUD operations:
17
+ *CREATE* `remember 'question?' with 'answer.'`
18
+ *READ* `all questions`
19
+ *READ* `answer 'question?'`
20
+ *UPDATE* `change 'question?' to 'new answer.'`
21
+ *DELETE* `forget 'question?'`
22
+
23
+ ## License
24
+
25
+ [MIT](http://opensource.org/licenses/MIT)
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,39 @@
1
+ class Knowledgebase
2
+ REDIS_KEY = 'knowledgebase'
3
+
4
+ class << self
5
+ def all
6
+ redis.hkeys(REDIS_KEY)
7
+ end
8
+
9
+ def create(question, answer)
10
+ redis.hset(REDIS_KEY, question, answer)
11
+ end
12
+
13
+ def read(question)
14
+ redis.hget(REDIS_KEY, question)
15
+ end
16
+
17
+ def update(question, answer)
18
+ redis.hset(REDIS_KEY, question, answer)
19
+ end
20
+
21
+ def destroy(question)
22
+ redis.hdel(REDIS_KEY, question)
23
+ end
24
+
25
+ def exists?(question)
26
+ redis.hexists(REDIS_KEY, question)
27
+ end
28
+
29
+ def redis
30
+ @redis ||= Redis::Namespace.new(redis_namespace, redis: Lita.redis)
31
+ end
32
+
33
+ private
34
+
35
+ def redis_namespace
36
+ ENV['env'] == 'test' ? 'handlers:answers:test' : 'handlers:answers'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,2 @@
1
+ require 'lita/handlers/answers'
2
+ require 'knowledgebase'
@@ -0,0 +1,90 @@
1
+ require 'lita'
2
+
3
+ module Lita
4
+ module Handlers
5
+ class Answers < Handler
6
+ TEXT = /[\w\s\,\.\-–]+/
7
+ QUESTION = /(?:'|")(#{TEXT.source}\?)(?:'|")/
8
+ ANSWER = /(?:'|")(#{TEXT.source}\.?)(?:'|")/
9
+
10
+ route(/^all\squestions$/i, :index, command: true, help: {
11
+ "all questions" => "You could ask me the following questions:\n1) ...\n2) ..."
12
+ })
13
+
14
+ route(/^remember\s#{QUESTION.source}\swith\s#{ANSWER.source}$/i, :create, command: true, help: {
15
+ "remember 'question?' with 'answer.'" => "The answer for 'question' is 'answer'."
16
+ })
17
+
18
+ route(/^answer\s#{QUESTION.source}$/i, :show, command: true, help: {
19
+ "answer 'question?'" => "answer."
20
+ })
21
+
22
+ route(/^change\s#{QUESTION.source}\sto\s#{ANSWER.source}$/i, :update, command: true, help: {
23
+ "change 'question?' to 'new answer.'" => "The new answer for 'question' is 'new answer'."
24
+ })
25
+
26
+ route(/^forget\s#{QUESTION.source}$/i, :destroy, command: true, help: {
27
+ "forget 'question?'" => "Forgot 'question?'"
28
+ })
29
+
30
+ def index(response)
31
+ questions = Knowledgebase.all
32
+ if questions.any?
33
+ reply = "You could ask me the following questions:"
34
+ questions.map.with_index do |question, index|
35
+ reply << "\n#{index+1}) #{question}"
36
+ end
37
+ else
38
+ reply = "There are no questions yet! " \
39
+ "Use REMEMBER 'question?' WITH 'answer.' syntax for creating questions!\n" \
40
+ "For more info see: help remember."
41
+ end
42
+ response.reply(reply)
43
+ end
44
+
45
+ def create(response)
46
+ question, answer = response.matches.first
47
+ if Knowledgebase.exists?(question)
48
+ answer = Knowledgebase.read(question)
49
+ reply = "Use CHANGE 'question?' TO 'new answer.' syntax for existing questions! " \
50
+ "For more info see: help change.\n" \
51
+ "The answer for '#{question}' is still '#{answer}'"
52
+ else
53
+ Knowledgebase.create(question, answer)
54
+ reply = "The answer for '#{question}' is '#{answer}'"
55
+ end
56
+ response.reply(reply)
57
+ end
58
+
59
+ def show(response)
60
+ question = response.matches.first
61
+ reply = Knowledgebase.read(question) || 'There is no such a question! Use ALL QUESTIONS command.'
62
+ response.reply(reply)
63
+ end
64
+
65
+ def update(response)
66
+ question, new_answer = response.matches.first
67
+ if Knowledgebase.exists?(question)
68
+ Knowledgebase.update(question, new_answer)
69
+ reply = "The new answer for '#{question}' is '#{new_answer}'."
70
+ else
71
+ reply = 'There is no such a question! Use ALL QUESTIONS command.'
72
+ end
73
+ response.reply(reply)
74
+ end
75
+
76
+ def destroy(response)
77
+ question = response.matches.first[0]
78
+ if Knowledgebase.exists?(question)
79
+ Knowledgebase.destroy(question)
80
+ reply = "Forgot '#{question}'"
81
+ else
82
+ reply = 'There is no such a question! Use ALL QUESTIONS command.'
83
+ end
84
+ response.reply(reply)
85
+ end
86
+ end
87
+
88
+ Lita.register_handler(Answers)
89
+ end
90
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-answers"
3
+ spec.version = "0.0.1"
4
+ spec.authors = ["Sergey Alekseev"]
5
+ spec.email = ["sergey.alekseev.minsk@gmail.com"]
6
+ spec.description = "A Lita handler for a question answering (QA) system. Allows you to use CRUD operations." \
7
+ "More functionality is coming soon..."
8
+ spec.summary = %q{A Lita handler for a question answering (QA) system}
9
+ spec.homepage = "https://github.com/sergey-alekseev/lita-answers"
10
+ spec.license = "MIT"
11
+ spec.metadata = { "lita_plugin_type" => "handler" }
12
+
13
+ spec.files = `git ls-files`.split($/)
14
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_runtime_dependency "lita", "~> 2.7"
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec", ">= 2.14"
23
+ spec.add_development_dependency "simplecov"
24
+ spec.add_development_dependency "coveralls"
25
+ spec.add_development_dependency 'pry'
26
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Knowledgebase do
4
+ include_context 'variables'
5
+
6
+ subject { Knowledgebase }
7
+ before { subject.create(question, answer) }
8
+
9
+ describe '.all' do
10
+ it 'returns all questions' do
11
+ expect(subject.all).to eq([question])
12
+ end
13
+ end
14
+
15
+ describe '.create(question, answer)' do
16
+ it 'sets the key to question and the value to answer' do
17
+ subject.create(question, answer)
18
+ expect(subject.read(question)).to eq(answer)
19
+ end
20
+ end
21
+
22
+ describe '.read(question)' do
23
+ it 'returns the answer for the question' do
24
+ expect(subject.read(question)).to eq(answer)
25
+ end
26
+ end
27
+
28
+ describe '.update(question, answer)' do
29
+ it 'updates the answer for the question' do
30
+ subject.create(question, answer)
31
+ subject.update(question, new_answer)
32
+ expect(subject.read(question)).to eq(new_answer)
33
+ end
34
+ end
35
+
36
+ describe '.destroy(question)' do
37
+ it 'destroys question' do
38
+ subject.create(question, answer)
39
+ subject.destroy(question)
40
+ expect(subject.read(question)).to be_nil
41
+ end
42
+ end
43
+
44
+ describe '.exists?(question)' do
45
+ it 'returns true if question exists' do
46
+ expect(subject.exists?(question)).to be_true
47
+ end
48
+
49
+ it "returns false if question does not exist" do
50
+ subject.destroy(question)
51
+ expect(subject.exists?(question)).to be_false
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lita::Handlers::Answers, lita_handler: true do
4
+ include_context 'variables'
5
+
6
+ describe 'routes' do
7
+ it { routes_command("remember 'question?' with 'answer.'").to :create }
8
+ it { routes_command("change 'question?' to 'answer.'").to :update }
9
+ it { routes_command("answer 'question?'").to :show }
10
+ it { routes_command("forget 'question?'").to :destroy }
11
+ it { routes_command("all questions").to :index }
12
+ end
13
+
14
+ describe 'commands' do
15
+ before { send_command "remember '#{question}' with '#{answer}'" }
16
+
17
+ describe '#create' do
18
+ it 'remembers the question with an answer' do
19
+ expect(replies.last).to eq("The answer for '#{question}' is '#{answer}'")
20
+ end
21
+
22
+ it "doesn't remember the existing question" do
23
+ send_command "remember '#{question}' with 'abrakadabra'"
24
+ expect(replies.last).to eq("Use CHANGE 'question?' TO 'new answer.' syntax for existing questions! " \
25
+ "For more info see: help change.\n" \
26
+ "The answer for '#{question}' is still '#{answer}'")
27
+ end
28
+ end
29
+
30
+ describe '#index' do
31
+ it 'displays all questions' do
32
+ send_command 'all questions'
33
+ expect(replies.last).to eq "You could ask me the following questions:\n" \
34
+ "1) #{question}"
35
+ end
36
+
37
+ context 'no questions' do
38
+ it 'displays help explanation' do
39
+ send_command "forget '#{question}'"
40
+ send_command 'all questions'
41
+ expect(replies.last).to eq("There are no questions yet! " \
42
+ "Use REMEMBER 'question?' WITH 'answer.' syntax for creating questions!\n" \
43
+ "For more info see: help remember.")
44
+ end
45
+ end
46
+ end
47
+
48
+ describe '#show' do
49
+ it 'answers the question' do
50
+ send_command "answer '#{question}'"
51
+ expect(replies.last).to eq(answer)
52
+ end
53
+
54
+ it_behaves_like 'absent question', "answer 'abrakadabra?'"
55
+ end
56
+
57
+ describe '#destroy' do
58
+ it 'forgets the question' do
59
+ send_command "forget '#{question}'"
60
+ expect(replies.last).to eq("Forgot '#{question}'")
61
+ end
62
+
63
+ it_behaves_like 'absent question', "forget 'abrakadabra?'"
64
+ end
65
+
66
+ describe '#update' do
67
+ it 'changes the answer for the question' do
68
+ send_command "change '#{question}' to '#{new_answer}'"
69
+ expect(replies.last).to eq("The new answer for '#{question}' is '#{new_answer}'.")
70
+ end
71
+
72
+ it_behaves_like 'absent question', "change 'abrakadabra?' to 'abrakadabra'"
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,13 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
+ SimpleCov::Formatter::HTMLFormatter,
5
+ Coveralls::SimpleCov::Formatter
6
+ ]
7
+ SimpleCov.start { add_filter '/spec/' }
8
+
9
+ require 'lita-answers'
10
+ require 'lita/rspec'
11
+ Dir["#{Dir.pwd}/spec/support/**/*.rb"].each { |f| require f }
12
+
13
+ ENV['env'] = 'test'
@@ -0,0 +1,5 @@
1
+ shared_context 'variables' do
2
+ let(:question) { 'What is the w3c?' }
3
+ let(:answer) { 'The World Wide Web Consortium is the main international standards organization for the World Wide Web.' }
4
+ let(:new_answer) { 'The World Wide Web Consortium' }
5
+ end
@@ -0,0 +1,6 @@
1
+ shared_examples 'absent question' do |command|
2
+ it 'says that there is no such a question' do
3
+ send_command command
4
+ expect(replies.last).to eq("There is no such a question! Use ALL QUESTIONS command.")
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-answers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Alekseev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '2.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '2.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A Lita handler for a question answering (QA) system. Allows you to use
112
+ CRUD operations.More functionality is coming soon...
113
+ email:
114
+ - sergey.alekseev.minsk@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - Gemfile
121
+ - LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - lib/knowledgebase.rb
125
+ - lib/lita-answers.rb
126
+ - lib/lita/handlers/answers.rb
127
+ - lita-answers.gemspec
128
+ - spec/knowledgebase_spec.rb
129
+ - spec/lita/handlers/answers_spec.rb
130
+ - spec/spec_helper.rb
131
+ - spec/support/contexts/variables.rb
132
+ - spec/support/examples/absent_question.rb
133
+ homepage: https://github.com/sergey-alekseev/lita-answers
134
+ licenses:
135
+ - MIT
136
+ metadata:
137
+ lita_plugin_type: handler
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 2.1.11
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: A Lita handler for a question answering (QA) system
158
+ test_files:
159
+ - spec/knowledgebase_spec.rb
160
+ - spec/lita/handlers/answers_spec.rb
161
+ - spec/spec_helper.rb
162
+ - spec/support/contexts/variables.rb
163
+ - spec/support/examples/absent_question.rb