Skip to content

Commit

Permalink
Changed question processsing format.
Browse files Browse the repository at this point in the history
 * 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
Franklin Webber committed Oct 23, 2011
1 parent a51abc9 commit 4d0804b
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 44 deletions.
120 changes: 83 additions & 37 deletions lib/quiz.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,57 +32,103 @@ def handle(time, sender_nick, message)


if sent_to_me? message and is_a_valid_question? request 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' end
question = Regexp.last_match(2)
answer_length = Regexp.last_match(4) || '2 minutes' if sent_to_me? message and currently_asking_a_question?

# 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


process_response_for_active_question time, sender_nick, request


end 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 end


def handle_polar(question,parameters,length) def start_accepting_responses_for_this_question
puts "#{question} #{parameters} #{length}" @currently_asking_a_question = true
end end


def handle_choice(question,parameters,length) def stop_accepting_responses_for_this_question
puts "#{question} #{parameters} #{length}" @currently_asking_a_question = false
end end


def handle_scale(question,parameters,length) def process_response_for_active_question(time,sender,request)
puts "#{question} #{parameters} #{length}"
end end


def is_a_valid_question? message def process_results_for_question(sender,question)
QUESTION_REGEX =~ message
end end


end end
73 changes: 66 additions & 7 deletions spec/quiz_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
subject { subject {
connection = double("connection") connection = double("connection")
connection.stub_chain(:config, :nick) { "quizmaster" } connection.stub_chain(:config, :nick) { "quizmaster" }
connection.stub(:store).and_return(store)
connection.stub(:reply).and_return(nil)
Robut::Plugin::Quiz.new connection 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", "ask choice 'What do you want for lunch?', 'pizza', 'sandwich', 'salad' for 1 minute",
Expand All @@ -28,23 +33,77 @@


describe "#handle" do 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 context "when a polar question is asked" do


it "should find all the components of the question" do it "should find all the components of the question" do

subject.should_receive(:handle_polar).with('Should I continue the presentation?',[],'3') subject.should_receive(:handle_polar).with('person','Should I continue the presentation?','3')
subject.handle Time.now,'person',"@quizmaster ask polar 'Should I continue the presentation?' for 3 minutes" subject.process_the_question time,'person',"ask polar 'Should I continue the presentation?' for 3 minutes"


end end


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.should_receive(:process_results_for_question).with("person",'Should I continue the presentation?')
subject.handle Time.now,'person',"@quizmaster ask choice 'What should I talk about next?', 'x', 'y', 'z' for 4 minutes" subject.handle_polar('person','Should I continue the presentation?','3')


end end


Expand Down

0 comments on commit 4d0804b

Please sign in to comment.