Class: Lita::Handlers::PollHandler
- Inherits:
-
Handler
- Object
- Handler
- Lita::Handlers::PollHandler
- Defined in:
- lib/lita/handlers/pollHandler.rb
Instance Method Summary collapse
- #clear(response) ⇒ Object
- #complete(response) ⇒ Object
- #info(response) ⇒ Object
- #list(response) ⇒ Object
- #make(response) ⇒ Object
- #option(response) ⇒ Object
- #tally(response) ⇒ Object
- #vote(response) ⇒ Object
Instance Method Details
#clear(response) ⇒ Object
26 27 28 29 |
# File 'lib/lita/handlers/pollHandler.rb', line 26 def clear(response) redis.flushdb response.reply(t("replies.clear.success")) end |
#complete(response) ⇒ Object
143 144 145 146 |
# File 'lib/lita/handlers/pollHandler.rb', line 143 def complete(response) redis.del(response.matches.pop[0]) response.reply(t("replies.complete.success")) end |
#info(response) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/lita/handlers/pollHandler.rb', line 61 def info(response) args = response.matches.pop id = args[0] poll = redis.get(id) if poll.nil? response.reply(t("replies.general.poll_not_found", id: id)) else poll = Poll.json_create(JSON.parse(poll)) idx = 0 response.reply(t("replies.info.header", poll_topic: poll.topic)) every(0.5) do |timer| if idx >= poll..length timer.stop else option = poll.[idx] idx += 1 response.reply(t("replies.info.option", idx: idx, option: option)) end end end end |
#list(response) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/lita/handlers/pollHandler.rb', line 10 def list(response) ids = redis.keys('*') if ids.empty? response.reply(t("replies.list.no_polls")) else response.reply(t("replies.list.header")) redis.keys('*').each do |id| poll = Poll.json_create(JSON.parse(redis.get(id))) response.reply(t("replies.list.poll", poll_id: poll.id, poll_topic: poll.topic)) end end end |
#make(response) ⇒ Object
34 35 36 37 38 |
# File 'lib/lita/handlers/pollHandler.rb', line 34 def make(response) made = Poll.new(response.matches.pop[0]) redis.set(made.id, made.to_json) response.reply(t("replies.make.success", poll_id: made.id, poll_topic: made.topic)) end |
#option(response) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/lita/handlers/pollHandler.rb', line 43 def option(response) args = response.matches.pop id = args[0] poll = redis.get(id) option = args[1] if poll.nil? response.reply(t("replies.general.poll_not_found", id: id)) else poll = Poll.json_create(JSON.parse(poll)) poll.set_option(option) redis.set(poll.id, poll.to_json) response.reply(t("replies.option.success", option: option, poll_id: poll.id)) end end |
#tally(response) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/lita/handlers/pollHandler.rb', line 110 def tally(response) args = response.matches.pop id = args[0] poll = redis.get(id) if poll.nil? response.reply(t("replies.general.poll_not_found", id: id)) else tally = [] poll = Poll.json_create(JSON.parse(poll)) poll.votes.each do |user, vote| vote = vote-1 tally[vote] = tally[vote] ? tally[vote]+1 : 1 end idx = 0 response.reply(t("replies.tally.header")) every(0.5) do |timer| if idx >= poll..length timer.stop else option = poll.[idx] response.reply(t( "replies.tally.option", option: option, votes: tally[idx] ? tally[idx] : 0 )) idx += 1 end end end end |
#vote(response) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/lita/handlers/pollHandler.rb', line 86 def vote(response) args = response.matches.pop id = args[0] optNum = args[1].to_i poll = redis.get(id) if poll.nil? response.reply(t("replies.general.poll_not_found", id: id)) else poll = Poll.json_create(JSON.parse(poll)) if poll.valid_vote?(optNum) poll.vote(response.user, optNum) redis.set(poll.id, poll.to_json) response.reply_privately(t( "replies.vote.success", option: poll.[optNum-1] )) else response.reply_privately(t("replies.vote.not_found")) end end end |