Browse files

Polar Question calculating results

  • Loading branch information...
1 parent 18a6042 commit 8c1a9c505dd3bba47324808c2891e994750e8c0c @burtlo committed Oct 23, 2011
Showing with 150 additions and 13 deletions.
  1. +32 −12 lib/quiz.rb
  2. +118 −1 spec/quiz_spec.rb
View
44 lib/quiz.rb
@@ -79,7 +79,7 @@ def set_current_question(question,length_of_time)
stop_accepting_responses_for_this_question question
- question.declare_results
+ reply question.results
end
@@ -103,7 +103,7 @@ module Robut::Plugin::Quiz::Question
#
# 1st: question type - choice, polar, scale
# 2nd: question
- # 3rd: choices
+ # 3rd: choices
#
QUESTION_REGEX = /^ask ?(choice|polar|scale)? (?:question )?'([^']+)'[\s,]*((?:'[^']+'[\s,]*)*)*(?:(?:for )?(\d+) minutes?)?$/
@@ -123,33 +123,53 @@ def process_question(request)
# look at all the single quoted items to find the list of parameters if there are any
@parameters = request.scan(GROUP_REGEX)[1..-1].flatten
- # puts "Request: #{request}"
- # puts %{
- # type: #{question_type}
- # questions: #{question}
- # parameters: #{parameters}
- # time: #{answer_length} }
-
end
end
class Robut::Plugin::Quiz::Polar
include Robut::Plugin::Quiz::Question
+ YES_ANSWER = /y|yes/i
+ NO_ANSWER = /n|no/i
+
+
def ask
"@#{@sender} asks '#{@question}' (yes/no)"
end
def handle_response(time,sender_nick,response)
+ if response =~ YES_ANSWER
+ store_positive_response_for sender_nick
+ elsif response =~ NO_ANSWER
+ store_negative_response_for sender_nick
+ end
-
- # process a yes/no y/n - store results per user
end
- def declare_results
+ def results
+
+ yes_votes = no_votes = 0
+
+ captured_results.each_pair do |key,value|
+ yes_votes = yes_votes + 1 if value
+ no_votes = no_votes + 1 unless value
+ end
+ "#{yes_votes} YES vote#{yes_votes != 1 ? 's' : ''} and #{no_votes} NO vote#{no_votes != 1 ? 's' : ''}"
+ end
+
+ def captured_results
+ @captured_results ||= {}
+ end
+
+ def store_positive_response_for sender_nick
+ captured_results[sender_nick] = true
+ end
+
+ def store_negative_response_for sender_nick
+ captured_results[sender_nick] = false
end
end
View
119 spec/quiz_spec.rb
@@ -144,7 +144,7 @@
it "should process the results for the question" do
- question.should_receive(:declare_results)
+ question.should_receive(:results)
subject.set_current_question(question,'3')
end
@@ -153,4 +153,121 @@
end
+end
+
+
+describe Robut::Plugin::Quiz::Polar do
+
+ subject do
+ Robut::Plugin::Quiz::Polar.new 'person',"ask polar 'Should I continue the presentation?' for 3 minutes"
+ end
+
+ let(:time) { Time.now }
+
+ describe "#handle_response" do
+
+ context "when the response is a 'yes'" do
+
+ it "should store positive response for the user" do
+
+ subject.should_receive(:store_positive_response_for).with('person')
+ subject.handle_response(time,'person','yes')
+
+ end
+
+ end
+
+ context "when the response is a 'y'" do
+
+ it "should store a positive response for the user" do
+
+ subject.should_receive(:store_positive_response_for).with('person')
+ subject.handle_response(time,'person','y')
+
+ end
+ end
+
+ context "when the response is a 'no'" do
+
+ it "should store negative response for the user" do
+
+ subject.should_receive(:store_negative_response_for).with('person')
+ subject.handle_response(time,'person','no')
+
+ end
+
+ end
+
+ context "when the response is a 'n'" do
+
+ it "should store negative response for the user" do
+
+ subject.should_receive(:store_negative_response_for).with('person')
+ subject.handle_response(time,'person','n')
+
+ end
+
+ end
+
+ end
+
+ describe "#declare_results" do
+
+ context "when there have been no votes" do
+
+ it "should reply that there are zero YES votes and zero NO votes" do
+
+ subject.results.should eq "0 YES votes and 0 NO votes"
+
+ end
+
+ end
+
+ context "when there has been a yes vote" do
+
+ it "should reply that there is 1 YES vote" do
+
+ subject.handle_response(time,'person','yes')
+ subject.results.should eq "1 YES vote and 0 NO votes"
+
+ end
+
+ end
+
+ context "when there has been a no vote" do
+
+ it "should reply that there is 1 NO vote" do
+
+ subject.handle_response(time,'person','no')
+ subject.results.should eq "0 YES votes and 1 NO vote"
+
+ end
+
+ end
+
+ context "when there has been multiple votes by the same user" do
+
+ it "should count as one vote" do
+
+ subject.handle_response(time,'person','no')
+ subject.handle_response(time,'person','no')
+ subject.handle_response(time,'person','no')
+ subject.results.should eq "0 YES votes and 1 NO vote"
+
+ end
+
+ it "should only count the last vote" do
+
+ subject.handle_response(time,'person','yes')
+ subject.handle_response(time,'person','yes')
+ subject.handle_response(time,'person','no')
+ subject.results.should eq "0 YES votes and 1 NO vote"
+
+ end
+
+ end
+
+ end
+
+
end

0 comments on commit 8c1a9c5

Please sign in to comment.