Skip to content

Commit

Permalink
Some basic question logic in place
Browse files Browse the repository at this point in the history
  • Loading branch information
Franklin Webber committed Oct 14, 2011
1 parent 047fd03 commit b0723c3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -6,4 +6,5 @@ group :test do
gem 'rspec'
gem 'guard'
gem 'guard-rspec'
gem 'growl_notify'
end
2 changes: 1 addition & 1 deletion Guardfile
Expand Up @@ -2,7 +2,7 @@

guard 'rspec', :version => 2, :cli => '--color --format d' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end

34 changes: 32 additions & 2 deletions lib/quiz.rb
Expand Up @@ -24,10 +24,28 @@ def usage
def handle(time, sender_nick, message)

# check to see if the user is asking the bot a question
request = words(message).join(" ")

if sent_to_me? message and is_a_question? message
if sent_to_me? message and is_a_valid_question? request
request =~ QUESTION_REGEX

question_type = Regexp.last_match(1) || 'choice'
question = Regexp.last_match(2)
parameters = Regexp.last_match(3)
answer_length = Regexp.last_match(4) || '2'

puts %{ #{question_type} }

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
Expand All @@ -38,8 +56,20 @@ def handle(time, sender_nick, message)

end

def handle_polar(question,parameters,length)
puts "#{question} #{parameters} #{length}"
end

def handle_choice(question,parameters,length)
puts "#{question} #{parameters} #{length}"
end

def handle_scale(question,parameters,length)
puts "#{question} #{parameters} #{length}"
end

def is_a_valid_question? message
message =~ QUESTION_REGEX
QUESTION_REGEX =~ message
end

end
26 changes: 26 additions & 0 deletions spec/quiz_spec.rb
Expand Up @@ -24,5 +24,31 @@
end

end

describe "#handle" 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?',nil,'3')
subject.handle Time.now,'person',"@quizmaster ask polar 'Should I continue the presentation?' for 3 minutes"

end

end

context "when a choice question is asked" do

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

subject.should_receive(:handle_choice).with('What should I talk about next?',"'x', 'y', 'z'",'2')
subject.handle Time.now,'person',"@quizmaster ask choice 'What should I talk about next?', 'x', 'y', 'z'"

end

end

end

end

0 comments on commit b0723c3

Please sign in to comment.