Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changed question processsing format.

 * Questions are now enqueued in the Robut Store
 * Processed questions now is done when the question is dequeued
 * Handling a polar question will step through the basic outline
  • Loading branch information...
commit 4d0804bb919924e96a9b0227eff7d229203b9e1b 1 parent a51abc9
@burtlo authored
Showing with 149 additions and 44 deletions.
  1. +83 −37 lib/quiz.rb
  2. +66 −7 spec/quiz_spec.rb
View
120 lib/quiz.rb
@@ -32,57 +32,103 @@ def handle(time, sender_nick, message)
if sent_to_me? message and is_a_valid_question? request
- request =~ QUESTION_REGEX
+ enqueue_the_question time, sender_nick, request
- question_type = Regexp.last_match(1) || 'choice'
- question = Regexp.last_match(2)
- answer_length = Regexp.last_match(4) || '2 minutes'
-
- # 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
-
- puts "Request: #{request}"
- puts %{
- type: #{question_type}
- questions: #{question}
- parameters: #{parameters}
- time: #{answer_length} }
-
- case question_type
- when 'polar'
- handle_polar(question,parameters,answer_length)
- when 'choice'
- handle_choice(question,parameters,answer_length)
- when 'scale'
- handle_scale(question,parameters,answer_length)
- else
- puts "failed to find a question type"
- end
+ end
+
+ if sent_to_me? message and currently_asking_a_question?
+ process_response_for_active_question time, sender_nick, request
end
- # check to see what time of question the user is asking
+ end
+
+ def is_a_valid_question? message
+ QUESTION_REGEX =~ message
+ end
+
+ def enqueue_the_question(time,sender,question)
+ # enqueue the question with a unique identifier
+ (store["quiz::question_queue"] ||= []) << [time,sender,question]
+ # start a worker thread unless one has already been started
+ end
+
+
+ # if there are no active questions then ask the question
+ # TODO: create the question worker
+
+ def currently_asking_a_question?
+ @currently_asking_a_question
+ end
+
+ def process_the_question(time,sender,request)
+
+ request =~ QUESTION_REGEX
+
+ question_type = Regexp.last_match(1) || 'choice'
+ question = Regexp.last_match(2)
+ answer_length = Regexp.last_match(4) || '2 minutes'
+
+ # 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
+
+ # puts "Request: #{request}"
+ # puts %{
+ # type: #{question_type}
+ # questions: #{question}
+ # parameters: #{parameters}
+ # time: #{answer_length} }
+
+
+ case question_type
+ when 'polar'
+ handle_polar(sender,question,answer_length)
+ when 'choice'
+ handle_choice(question,parameters,answer_length)
+ when 'scale'
+ handle_scale(question,parameters,answer_length)
+ else
+ puts "failed to find a question type"
+ end
+
+
+ end
+
+ def handle_polar(sender,question,length)
+
+ start_accepting_responses_for_this_question
+
+ reply "@#{sender} asks '#{question}' (yes/no)"
+ # starting a timer to take robut out of response mode
+ # set up a timer for the length of time
+
+ sleep length.to_i * 60
+ # when timer is done - take robut out of response mode
+
+ stop_accepting_responses_for_this_question
+ # # collect results from the question
+ # send the results to the person that asked the question
- # allow each question to handle the question appropriately for the default
+ process_results_for_question sender, question
end
- def handle_polar(question,parameters,length)
- puts "#{question} #{parameters} #{length}"
+ def start_accepting_responses_for_this_question
+ @currently_asking_a_question = true
end
- def handle_choice(question,parameters,length)
- puts "#{question} #{parameters} #{length}"
+ def stop_accepting_responses_for_this_question
+ @currently_asking_a_question = false
end
- def handle_scale(question,parameters,length)
- puts "#{question} #{parameters} #{length}"
+ def process_response_for_active_question(time,sender,request)
+
end
- def is_a_valid_question? message
- QUESTION_REGEX =~ message
+ def process_results_for_question(sender,question)
+
end
-end
+end
View
73 spec/quiz_spec.rb
@@ -5,9 +5,14 @@
subject {
connection = double("connection")
connection.stub_chain(:config, :nick) { "quizmaster" }
+ connection.stub(:store).and_return(store)
+ connection.stub(:reply).and_return(nil)
Robut::Plugin::Quiz.new connection
}
+ let!(:store) { {} }
+
+ let(:time) { Time.now }
[
"ask choice 'What do you want for lunch?', 'pizza', 'sandwich', 'salad' for 1 minute",
@@ -28,23 +33,77 @@
describe "#handle" do
+ context "when a question is asked" do
+
+ it "should be enqueued to be asked" do
+
+ subject.should_receive(:enqueue_the_question).with(time,'person',"ask polar 'Should I continue the presentation?' for 3 minutes")
+ subject.handle time,'person',"@quizmaster ask polar 'Should I continue the presentation?' for 3 minutes"
+
+ end
+
+ end
+
+ end
+
+
+
+ describe "#process_the_question" do
+
context "when a polar question is asked" do
it "should find all the components of the question" do
-
- subject.should_receive(:handle_polar).with('Should I continue the presentation?',[],'3')
- subject.handle Time.now,'person',"@quizmaster ask polar 'Should I continue the presentation?' for 3 minutes"
+
+ subject.should_receive(:handle_polar).with('person','Should I continue the presentation?','3')
+ subject.process_the_question time,'person',"ask polar 'Should I continue the presentation?' for 3 minutes"
end
end
- context "when a choice question is asked" do
+ end
+
+
+ describe "#handle_polar" do
+
+ before :each do
+ subject.stub(:sleep)
+ end
+
+ it "should place robut in the mode where it is asking a question" do
+
+ subject.should_receive(:start_accepting_responses_for_this_question)
+ subject.handle_polar('person','Should I continue the presentation?','3')
+
+ end
+
+ it "should ask the question" do
- it "should find all the components of the question" do
+ subject.should_receive(:reply).with("@person asks 'Should I continue the presentation?' (yes/no)")
+ subject.handle_polar('person','Should I continue the presentation?','3')
+
+ end
+
+ it "should wait until the question time is done" do
+
+ subject.should_receive(:sleep).with(180)
+ subject.handle_polar('person','Should I continue the presentation?','3')
+
+ end
+
+ context "when it is done waiting" do
+
+ it "should take robut out of the mdoe where it is asking a question" do
+
+ subject.should_receive(:stop_accepting_responses_for_this_question)
+ subject.handle_polar('person','Should I continue the presentation?','3')
+
+ end
+
+ it "should process the results for the question" do
- subject.should_receive(:handle_choice).with('What should I talk about next?',['x', 'y', 'z'],'4')
- subject.handle Time.now,'person',"@quizmaster ask choice 'What should I talk about next?', 'x', 'y', 'z' for 4 minutes"
+ subject.should_receive(:process_results_for_question).with("person",'Should I continue the presentation?')
+ subject.handle_polar('person','Should I continue the presentation?','3')
end
Please sign in to comment.
Something went wrong with that request. Please try again.