Skip to content
Browse files

Added Range Question

  • Loading branch information...
1 parent 3697797 commit 6516effb70f9b8721553484b4148d32a8b4a2222 @burtlo committed
Showing with 182 additions and 4 deletions.
  1. +4 −0 lib/polar.rb
  2. +1 −1 lib/question.rb
  3. +4 −3 lib/quiz.rb
  4. +73 −0 lib/range.rb
  5. +100 −0 spec/range_spec.rb
View
4 lib/polar.rb
@@ -32,6 +32,10 @@ def results
"#{yes_votes} YES vote#{yes_votes != 1 ? 's' : ''} and #{no_votes} NO vote#{no_votes != 1 ? 's' : ''}"
end
+ def ask
+ "@all Question '#{@question}' (yes/no)"
+ end
+
def store_positive_response_for sender_nick
captured_results[sender_nick] = true
end
View
2 lib/question.rb
@@ -32,7 +32,7 @@ def process_question(question_with_parameters)
@question = parsed_elements.first.first
# 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 = parsed_elements[1..-1].flatten
+ @parameters = Array(parsed_elements[1..-1].flatten)
end
View
7 lib/quiz.rb
@@ -40,7 +40,7 @@ def handle(time, sender_nick, message)
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
QUESTION_REGEX =~ message
@@ -136,7 +136,7 @@ def stop_accepting_responses_for_this_question(question)
# @param [String] response is the answer to the question proposed.
#
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."
else
reply "Sorry, @#{sender_nick}, I was unable to record that answer"
@@ -147,4 +147,5 @@ def process_response_for_active_question(sender_nick, response)
require_relative 'question'
-require_relative 'polar'
+require_relative 'polar'
+require_relative 'range'
View
73 lib/range.rb
@@ -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
View
100 spec/range_spec.rb
@@ -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.
Something went wrong with that request. Please try again.