Skip to content

Commit

Permalink
Added Range Question
Browse files Browse the repository at this point in the history
  • Loading branch information
Franklin Webber committed Oct 27, 2011
1 parent 3697797 commit 6516eff
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/polar.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def results
"#{yes_votes} YES vote#{yes_votes != 1 ? 's' : ''} and #{no_votes} NO vote#{no_votes != 1 ? 's' : ''}" "#{yes_votes} YES vote#{yes_votes != 1 ? 's' : ''} and #{no_votes} NO vote#{no_votes != 1 ? 's' : ''}"
end end


def ask
"@all Question '#{@question}' (yes/no)"
end

def store_positive_response_for sender_nick def store_positive_response_for sender_nick
captured_results[sender_nick] = true captured_results[sender_nick] = true
end end
Expand Down
2 changes: 1 addition & 1 deletion lib/question.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def process_question(question_with_parameters)
@question = parsed_elements.first.first @question = parsed_elements.first.first
# After the target, question type, question, and answer length has been determined # 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 # look at all the single quoted items to find the list of parameters if there are any
@parameters = parsed_elements[1..-1].flatten @parameters = Array(parsed_elements[1..-1].flatten)


end end


Expand Down
7 changes: 4 additions & 3 deletions lib/quiz.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def handle(time, sender_nick, message)


end end


QUESTION_REGEX = /^ask ?(choice|polar|scale)? (?:question )?(?:(?:for )?(\d+) minutes?)?(.+)$/ QUESTION_REGEX = /^ask ?(choice|polar|range)? (?:question )?(?:(?:for )?(\d+) minutes?)?(.+)$/


def is_a_valid_question? message def is_a_valid_question? message
QUESTION_REGEX =~ message QUESTION_REGEX =~ message
Expand Down Expand Up @@ -136,7 +136,7 @@ def stop_accepting_responses_for_this_question(question)
# @param [String] response is the answer to the question proposed. # @param [String] response is the answer to the question proposed.
# #
def process_response_for_active_question(sender_nick, response) def process_response_for_active_question(sender_nick, response)
if @@current_question.handle_response sender_nick, response if @@current_question.handle_response sender_nick, response[/^answer (.+)$/,1]
reply "Thank you, @#{sender_nick}, I have recorded your response." reply "Thank you, @#{sender_nick}, I have recorded your response."
else else
reply "Sorry, @#{sender_nick}, I was unable to record that answer" reply "Sorry, @#{sender_nick}, I was unable to record that answer"
Expand All @@ -147,4 +147,5 @@ def process_response_for_active_question(sender_nick, response)




require_relative 'question' require_relative 'question'
require_relative 'polar' require_relative 'polar'
require_relative 'range'
73 changes: 73 additions & 0 deletions lib/range.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,73 @@
# encoding: UTF-8

class Robut::Plugin::Quiz::Range
include Robut::Plugin::Quiz::Question

RANGE_VALUES = /(\d+)(?:-|\.\.)(\d+)/

def handle_response(sender_nick,response)

if within_range? response
store_range_response_for sender_nick, response
true
else
false
end

end

def start_value
if Array(@parameters).first.to_s =~ RANGE_VALUES
Regexp.last_match(1).to_i
else
1
end
end

def highest_value
if Array(@parameters).first.to_s =~ RANGE_VALUES
Regexp.last_match(2).to_i
else
5
end
end

def within_range?(value)
value.to_i >= start_value and value.to_i <= highest_value
end

def ask
"@all Question '#{@question}' (#{start_value}..#{highest_value})"
end

def results
return "I'm sorry, not enough votes were cast to be create a summary." if captured_results.length == 0

result_values = captured_results.values.extend Robut::Plugin::Quiz::Range::Averages
"#{result_values.length} votes with a mean of #{result_values.mean}"
end

def store_range_response_for(sender_nick,value)
captured_results[sender_nick] = value.to_i
end


module Averages

def sum
inject(:+)
end

def mean
return 0 if length == 0
sum.to_f / length.to_f
end

def mode
frequency = inject(Hash.new(0)) { |hash,value| hash[value] += 1; hash }
sort_by {|value| frequency[value] }.last
end

end

end
100 changes: 100 additions & 0 deletions spec/range_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,100 @@
require_relative 'spec_helper'

describe Robut::Plugin::Quiz::Range do

subject do
Robut::Plugin::Quiz::Range.new 'person',"ask range for 3 minutes 'Should I continue the presentation?'"
end

describe "#start_value" do

context "when no range has been specified" do

it "should default to the value 1" do
subject.start_value.should == 1
end

end

end

describe "#highest_value" do

context "when no range has been specified" do

it "should default to the value of 5" do
subject.highest_value.should == 5
end

end

end

describe "#within_range?" do

context "when the value is within range" do

it "should respond with true" do
subject.within_range?(3).should be_true
end

end

context "when the value is not within range" do

it "should respond with false" do
subject.within_range?(6).should be_false
end

end

end

describe "#handle_response" do

context "with a valid response" do

it "should return true" do
subject.should_receive(:store_range_response_for).with("sender","1")
subject.handle_response("sender","1").should be_true
end

end

end

describe Robut::Plugin::Quiz::Range::Averages do

subject do
array = [1,1,2,2,2,3,4,5]
array.extend Robut::Plugin::Quiz::Range::Averages
end


describe "#sum" do

it "should return the correct value" do
subject.sum.should == 20
end

end

describe "#mean" do

it "should return the correct value" do
subject.mean.should == 2.5
end

end

describe "#mode" do

it "should return the correct value" do
subject.mode.should == 2
end

end

end

end

0 comments on commit 6516eff

Please sign in to comment.