Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Broke apart classes and specs into their particular files

  • Loading branch information...
commit 938f0272f34e82c7849a7894a2cfabb8751969bc 1 parent 49a9a15
@burtlo authored
View
43 lib/polar.rb
@@ -0,0 +1,43 @@
+class Robut::Plugin::Quiz::Polar
+ include Robut::Plugin::Quiz::Question
+
+ YES_ANSWER = /y|yes/i
+ NO_ANSWER = /n|no/i
+
+ def handle_response(sender_nick,response)
+
+ if response =~ YES_ANSWER
+ store_positive_response_for sender_nick
+ elsif response =~ NO_ANSWER
+ store_negative_response_for sender_nick
+ else
+ nil
+ end
+
+ end
+
+ 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
37 lib/question.rb
@@ -0,0 +1,37 @@
+module Robut::Plugin::Quiz::Question
+
+ # @see ask%20%3F(choice%7Cpolar%7Cscale)%3F%20(%3F%3Aquestion%20)%3F'(%5B%5E'%5D%2B)'%5B%5Cs%2C%5D*((%3F%3A'%5B%5E'%5D%2B'%5B%5Cs%2C%5D*)*)%2B(%3F%3A(%3F%3Afor%20)%3F(%5Cd%2B)%20minutes%3F)%3F
+ #
+ # 1st: question type - choice, polar, scale
+ # 2nd: question
+ # 3rd: choices
+ #
+ QUESTION_REGEX = /^ask ?(choice|polar|scale)? (?:question )?['"]([^'"]+)['"][\s,]*((?:['"][^'"]+['"][\s,]*)*)*(?:(?:for )?(\d+) minutes?)?$/
+
+ # This regex will find all the questions and parameters specified with the question
+ GROUP_REGEX = /['"]([^'"]+)['"]/
+
+ def initialize(sender,request)
+ @sender = sender
+ process_question(request)
+ end
+
+ def process_question(request)
+
+ request =~ QUESTION_REGEX
+ @question = Regexp.last_match(2)
+ # After the target, question type, question, and answer length has been determined
+ # 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
+
+ end
+
+ def to_s
+ @question
+ end
+
+ def ask
+ "@all Question '#{@question}'"
+ end
+
+end
View
85 lib/quiz.rb
@@ -118,86 +118,5 @@ def process_response_for_active_question(sender_nick, request)
end
-
-
-module Robut::Plugin::Quiz::Question
-
- # @see ask%20%3F(choice%7Cpolar%7Cscale)%3F%20(%3F%3Aquestion%20)%3F'(%5B%5E'%5D%2B)'%5B%5Cs%2C%5D*((%3F%3A'%5B%5E'%5D%2B'%5B%5Cs%2C%5D*)*)%2B(%3F%3A(%3F%3Afor%20)%3F(%5Cd%2B)%20minutes%3F)%3F
- #
- # 1st: question type - choice, polar, scale
- # 2nd: question
- # 3rd: choices
- #
- QUESTION_REGEX = /^ask ?(choice|polar|scale)? (?:question )?['"]([^'"]+)['"][\s,]*((?:['"][^'"]+['"][\s,]*)*)*(?:(?:for )?(\d+) minutes?)?$/
-
- # This regex will find all the questions and parameters specified with the question
- GROUP_REGEX = /['"]([^'"]+)['"]/
-
- def initialize(sender,request)
- @sender = sender
- process_question(request)
- end
-
- def process_question(request)
-
- request =~ QUESTION_REGEX
- @question = Regexp.last_match(2)
- # After the target, question type, question, and answer length has been determined
- # 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
-
- end
-
- def to_s
- @question
- end
-
- def ask
- "@all Question '#{@question}'"
- end
-
-end
-
-class Robut::Plugin::Quiz::Polar
- include Robut::Plugin::Quiz::Question
-
- YES_ANSWER = /y|yes/i
- NO_ANSWER = /n|no/i
-
- def handle_response(sender_nick,response)
-
- if response =~ YES_ANSWER
- store_positive_response_for sender_nick
- elsif response =~ NO_ANSWER
- store_negative_response_for sender_nick
- else
- nil
- end
-
- end
-
- 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
+require_relative 'question'
+require_relative 'polar'
View
117 spec/polar_spec.rb
@@ -0,0 +1,117 @@
+require_relative 'spec_helper'
+
+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('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('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('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('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('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('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('person','no')
+ subject.handle_response('person','no')
+ subject.handle_response('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('person','yes')
+ subject.handle_response('person','yes``')
+ subject.handle_response('person','no')
+ subject.results.should eq "0 YES votes and 1 NO vote"
+
+ end
+
+ end
+
+ end
+
+
+end
View
117 spec/quiz_spec.rb
@@ -153,121 +153,4 @@
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('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('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('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('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('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('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('person','no')
- subject.handle_response('person','no')
- subject.handle_response('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('person','yes')
- subject.handle_response('person','yes``')
- subject.handle_response('person','no')
- subject.results.should eq "0 YES votes and 1 NO vote"
-
- end
-
- end
-
- end
-
-
end
Please sign in to comment.
Something went wrong with that request. Please try again.