lita-poll 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a511e94ce5c62bb96ceeb489ca3fcafe84c327d6
4
+ data.tar.gz: 304e9b757437ca92237a4589bb2090a146687508
5
+ SHA512:
6
+ metadata.gz: 05caa1b67b770a5b0aa074d7be02a98f9c7dab2db87ab740c082577c0236e31860ddd642a1c601af0d2672ea8c86a472db65df51745f783222e0c9fa41042d9b
7
+ data.tar.gz: d14b14a7d66890fb64bf00d3d7d2f807ef958d419aea69cca6937e8b9afc7121720f2279c1fb117667a70ef1411bde9ed27d0b8ac74de73f78f02df4d9aabada
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
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 Michael Chua
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.
data/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # lita-poll
2
+
3
+ Plugin that enables polling functionality for a lita bot.
4
+
5
+ ## Installation
6
+
7
+ Add lita-poll to your Lita instance's Gemfile:
8
+
9
+ ``` ruby
10
+ gem "lita-poll"
11
+ ```
12
+ ## License
13
+
14
+ [MIT](http://opensource.org/licenses/MIT)
data/Rakefile ADDED
@@ -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,51 @@
1
+ require "json"
2
+ require "digest/sha1"
3
+
4
+ class Poll
5
+ ID_LEN = 3
6
+ ID_REGEXP = "(\\w{#{ID_LEN}})"
7
+ def initialize(topic, id=nil, options=nil, votes=nil)
8
+ @topic = topic
9
+ @id = id.nil? ? Digest::SHA1.hexdigest(topic).slice(0, ID_LEN) : id
10
+ @options = options.nil? ? [] : options
11
+ @votes = votes.nil? ? {} : votes
12
+ end
13
+
14
+ def topic
15
+ return @topic
16
+ end
17
+
18
+ def id
19
+ return @id
20
+ end
21
+
22
+ def options
23
+ return @options
24
+ end
25
+
26
+ def votes
27
+ return @votes
28
+ end
29
+
30
+ def set_option(opt)
31
+ @options.push(opt)
32
+ end
33
+
34
+ def valid_vote(optNum)
35
+ optNum-1 < @options.length
36
+ end
37
+
38
+ def vote(user, optNum)
39
+ @votes[user.id] = optNum
40
+ end
41
+
42
+ def to_json(*args)
43
+ {
44
+ "id" => @id, "topic" => @topic, "options" => @options, "votes" => @votes
45
+ }.to_json(*args)
46
+ end
47
+
48
+ def self.json_create(o)
49
+ new(o["topic"], o["id"], o["options"], o["votes"])
50
+ end
51
+ end
@@ -0,0 +1,151 @@
1
+ require "json"
2
+ require "lita/handlers/poll"
3
+
4
+ module Lita
5
+ module Handlers
6
+ class PollHandler < Handler
7
+ route /^poll list$/, :list, help: {
8
+ t("help.list.usage") => t("help.list.description")
9
+ }
10
+ def list(response)
11
+ ids = redis.keys('*')
12
+ if ids.empty?
13
+ response.reply(t("replies.list.no_polls"))
14
+ else
15
+ response.reply(t("replies.list.header"))
16
+ redis.keys('*').each do |id|
17
+ poll = Poll.json_create(JSON.parse(redis.get(id)))
18
+ response.reply(t("replies.list.poll", poll_id: poll.id, poll_topic: poll.topic))
19
+ end
20
+ end
21
+ end
22
+
23
+ route /^poll clear$/, :clear, help: {
24
+ t("help.clear.usage") => t("help.clear.description")
25
+ }, restrict_to: "poll_admins"
26
+ def clear(response)
27
+ redis.flushdb
28
+ response.reply(t("replies.clear.success"))
29
+ end
30
+
31
+ route /^poll make (.+)$/, :make, help: {
32
+ t("help.make.usage") => t("help.make.description")
33
+ }, restrict_to: "poll_admins"
34
+ def make(response)
35
+ made = Poll.new(response.matches.pop[0])
36
+ redis.set(made.id, made.to_json)
37
+ response.reply(t("replies.make.success", poll_id: made.id, poll_topic: made.topic))
38
+ end
39
+
40
+ route Regexp.new("^poll option #{Poll::ID_REGEXP} (.+)$"), :option, help: {
41
+ t("help.option.usage") => t("help.option.description")
42
+ }
43
+ def option(response)
44
+ args = response.matches.pop
45
+ id = args[0]
46
+ poll = redis.get(id)
47
+ option = args[1]
48
+ if poll.nil?
49
+ response.reply(t("replies.general.poll_not_found", id: id))
50
+ else
51
+ poll = Poll.json_create(JSON.parse(poll))
52
+ poll.set_option(option)
53
+ redis.set(poll.id, poll.to_json)
54
+ response.reply(t("replies.option.success", option: option, poll_id: poll.id))
55
+ end
56
+ end
57
+
58
+ route Regexp.new("poll info #{Poll::ID_REGEXP}$"), :info, help: {
59
+ t("help.info.usage") => t("help.info.description")
60
+ }
61
+ def info(response)
62
+ args = response.matches.pop
63
+ id = args[0]
64
+ poll = redis.get(id)
65
+ if poll.nil?
66
+ response.reply(t("replies.general.poll_not_found", id: id))
67
+ else
68
+ poll = Poll.json_create(JSON.parse(poll))
69
+ idx = 0
70
+ response.reply(t("replies.info.header", poll_topic: poll.topic))
71
+ every(1) do |timer|
72
+ if idx >= poll.options.length
73
+ timer.stop
74
+ else
75
+ option = poll.options[idx]
76
+ idx += 1
77
+ response.reply(t("replies.info.option", idx: idx, option: option))
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ route Regexp.new("^poll vote #{Poll::ID_REGEXP} (\\d+)$"), :vote, help: {
84
+ t("help.vote.usage") => t("help.vote.description")
85
+ }
86
+ def vote(response)
87
+ args = response.matches.pop
88
+ id = args[0]
89
+ optNum = args[1].to_i
90
+ poll = redis.get(id)
91
+ if poll.nil?
92
+ response.reply(t("replies.general.poll_not_found", id: id))
93
+ else
94
+ poll = Poll.json_create(JSON.parse(poll))
95
+ if poll.valid_vote(optNum)
96
+ poll.vote(response.user, optNum)
97
+ redis.set(poll.id, poll.to_json)
98
+ response.reply_privately(t(
99
+ "replies.vote.success", option: poll.options[optNum-1]
100
+ ))
101
+ else
102
+ response.reply_privately(t("replies.vote.not_found"))
103
+ end
104
+ end
105
+ end
106
+
107
+ route Regexp.new("^poll tally #{Poll::ID_REGEXP}$"), :tally, help: {
108
+ t("help.tally.usage") => t("help.tally.description")
109
+ }
110
+ def tally(response)
111
+ args = response.matches.pop
112
+ id = args[0]
113
+ poll = redis.get(id)
114
+ if poll.nil?
115
+ response.reply(t("replies.general.poll_not_found", id: id))
116
+ else
117
+ tally = []
118
+ poll = Poll.json_create(JSON.parse(poll))
119
+ poll.votes.each do |user, vote|
120
+ vote = vote-1
121
+ tally[vote] = tally[vote] ? tally[vote]+1 : 1
122
+ end
123
+
124
+ idx = 0
125
+ response.reply(t("replies.tally.header"))
126
+ every(1) do |timer|
127
+ if idx >= poll.options.length
128
+ timer.stop
129
+ else
130
+ option = poll.options[idx]
131
+ response.reply(t(
132
+ "replies.tally.option", option: option, votes: tally[idx] ? tally[idx] : 0
133
+ ))
134
+ idx += 1
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+ route Regexp.new("^poll complete #{Poll::ID_REGEXP}$"), :complete, help: {
141
+ t("help.complete.usage") => t("help.complete.description")
142
+ }, restrict_to: "poll_admins"
143
+ def complete(response)
144
+ redis.del(response.matches.pop[0])
145
+ response.reply(t("replies.complete.success"))
146
+ end
147
+ end
148
+
149
+ Lita.register_handler(PollHandler)
150
+ end
151
+ end
data/lib/lita-poll.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "lita"
2
+
3
+ Lita.load_locales Dir[File.expand_path(
4
+ File.join("..", "..", "locales", "*.yml"), __FILE__
5
+ )]
6
+
7
+ require "lita/handlers/pollHandler"
data/lita-poll.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-poll"
3
+ spec.version = "1.0.0"
4
+ spec.authors = ["Michael Chua"]
5
+ spec.email = ["chua.mbt@gmail.com"]
6
+ spec.summary = %q{Plugin that enables polling functionality for a lita bot.}
7
+ spec.license = "MIT"
8
+ spec.metadata = { "lita_plugin_type" => "handler" }
9
+ spec.homepage = "https://github.com/chua-mbt/lita-poll"
10
+
11
+ spec.files = `git ls-files`.split($/)
12
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.add_runtime_dependency "lita", ">= 3.3"
17
+
18
+ spec.add_development_dependency "bundler", "~> 1.3"
19
+ spec.add_development_dependency "rake"
20
+ spec.add_development_dependency "rspec", ">= 3.0.0"
21
+ end
data/locales/en.yml ADDED
@@ -0,0 +1,53 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ poll_handler:
5
+ help:
6
+ list:
7
+ usage: "poll list"
8
+ description: "List existing polls"
9
+ clear:
10
+ usage: "poll clear"
11
+ description: "Clear existing polls"
12
+ make:
13
+ usage: "poll make [topic]"
14
+ description: "Create a new poll on [topic]"
15
+ option:
16
+ usage: "poll option [pollId] [option]"
17
+ description: "Add [option] to poll with [pollId]"
18
+ info:
19
+ usage: "poll info [pollId]"
20
+ description: "Shows information on poll with [pollId]"
21
+ vote:
22
+ usage: "poll vote [pollId] [optNum]"
23
+ description: "Vote for [optNum] on poll with [pollId]"
24
+ tally:
25
+ usage: "poll tally [pollId]"
26
+ description: "Tally poll with [pollId]"
27
+ complete:
28
+ usage: "poll complete [pollId]"
29
+ description: "End poll with [pollId]"
30
+ replies:
31
+ general:
32
+ poll_not_found: "Poll with id '%{id}' not found."
33
+ list:
34
+ no_polls: "No polls currently exist."
35
+ header: "Active polls:"
36
+ poll: "%{poll_id} : %{poll_topic}"
37
+ clear:
38
+ success: "All polls cleared."
39
+ make:
40
+ success: "Poll on '%{poll_topic}' created with id '%{poll_id}'."
41
+ option:
42
+ success: "New option '%{option}' for poll '%{poll_id}'!"
43
+ info:
44
+ header: "Topic: %{poll_topic}"
45
+ option: "%{idx} : %{option}"
46
+ vote:
47
+ not_found: "Selected option does not exist for specified poll."
48
+ success: "Vote for '%{option}' registered."
49
+ tally:
50
+ header: "Tallied votes:"
51
+ option: "%{option} : %{votes}"
52
+ complete:
53
+ success: "Poll removed."
@@ -0,0 +1,4 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Poll, lita_handler: true do
4
+ end
@@ -0,0 +1,2 @@
1
+ require "lita-poll"
2
+ require "lita/rspec"
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-poll
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Chua
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-05 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: '3.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.3'
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: 3.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
69
+ description:
70
+ email:
71
+ - chua.mbt@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - lib/lita-poll.rb
82
+ - lib/lita/handlers/poll.rb
83
+ - lib/lita/handlers/pollHandler.rb
84
+ - lita-poll.gemspec
85
+ - locales/en.yml
86
+ - spec/lita/handlers/poll_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/chua-mbt/lita-poll
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ lita_plugin_type: handler
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Plugin that enables polling functionality for a lita bot.
113
+ test_files:
114
+ - spec/lita/handlers/poll_spec.rb
115
+ - spec/spec_helper.rb