Skip to content
Browse files

Add some tests and refactor methods

  • Loading branch information...
1 parent 37a3731 commit 24b3b76c02a89680035476ee2d59da7376123eb0 Gareth Rees committed Sep 27, 2011
Showing with 60 additions and 5 deletions.
  1. +17 −1 lib/readingtime.rb
  2. +9 −4 lib/readingtime/core_ext.rb
  3. +3 −0 readingtime.gemspec
  4. +31 −0 spec/readingtime_spec.rb
View
18 lib/readingtime.rb
@@ -2,5 +2,21 @@
require "readingtime/core_ext"
module Readingtime
- # Your code goes here...
+
+ def self.minutes(words)
+ (words / 200).floor
+ end
+
+ def self.seconds(words)
+ (words % 200 / (200 / 60)).floor
+ end
+
+ def self.time_in_words(time)
+ if time < 10
+ return "0#{time}"
+ else
+ return time.to_s
+ end
+ end
+
end
View
13 lib/readingtime/core_ext.rb
@@ -1,11 +1,16 @@
String.class_eval do
def reading_time
- word = self.scan(/(\w|-)+/).size
+ word_size = self.calculate_size
- minutes = (word / 200).floor
- seconds = (word % 200 / (200 / 60)).floor
+ minutes = Readingtime.minutes(word_size)
+ seconds = Readingtime.seconds(word_size)
- return minutes.to_s + ":" + seconds.to_s
+ return minutes.to_s + ":" + Readingtime.time_in_words(seconds)
end
+
+ def calculate_size
+ self.scan(/(\w|-)+/).size
+ end
+
end
View
3 readingtime.gemspec
@@ -17,4 +17,7 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
+
+ s.add_development_dependency "rspec", "~>2.6"
+
end
View
31 spec/readingtime_spec.rb
@@ -0,0 +1,31 @@
+# encoding: utf-8
+path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
+$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
+require "readingtime"
+
+
+describe Readingtime do
+ let(:short_text) { "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." }
+ let(:long_text) { ("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." * 100) }
+
+
+ it "should calculate the length of the string input" do
+ short_text.calculate_size.should == short_text.scan(/(\w|-)+/).size
+ end
+
+
+ it "should calculate the number of minutes the reading should take" do
+ words = short_text.calculate_size
+ Readingtime.minutes(words).should == (words / 200).floor
+ end
+
+ it "should calculate the remaining seconds the reading should take" do
+ words = short_text.calculate_size
+ Readingtime.seconds(words).should == (words % 200 / (200 / 60)).floor
+ end
+
+ it "should display the reading time in a readable format" do
+ short_text.reading_time.should == "0:23"
+ end
+
+end

0 comments on commit 24b3b76

Please sign in to comment.
Something went wrong with that request. Please try again.