readingtime 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in readingtime.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # Estimate Reading Time
2
+
3
+ [![Build Status](https://secure.travis-ci.org/garethrees/readingtime.png)](http://travis-ci.org/garethrees/readingtime)
4
+
5
+
6
+ I use [iA Writer](http://iawriter.com "iA Writer") and find the *estimated reading time* feature pretty handy. I thought it would be cool to add at the top of my blog articles on the web as [others](http://nicepaul.com "The personal blog of @nicepaul") do.
7
+
8
+ Full credit goes to [Brian Cray](http://briancray.com/2010/04/09/estimated-reading-time-web-design "Brian Cray - Estimated Reading Time in Web Design") for explaining how to do it in PHP.
9
+
10
+ ## How to Use
11
+
12
+ [readingtime](http://github.com/garethrees/readingtime "Gem to estimate reading time") extends the Ruby String class, so you can call `reading_time` on any string object.
13
+
14
+ ### Example
15
+
16
+ @words = "Lorem ipsum dolor sit amet"
17
+ @words.reading_time
18
+ => "0:01"
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :default do
4
+ exec('rspec spec/.')
5
+ end
@@ -0,0 +1,16 @@
1
+ String.class_eval do
2
+ def reading_time
3
+ word_size = self.calculate_size
4
+
5
+ minutes = Readingtime.minutes(word_size)
6
+ seconds = Readingtime.seconds(word_size)
7
+
8
+ return Readingtime.format_seconds((minutes + seconds))
9
+
10
+ end
11
+
12
+ def calculate_size
13
+ self.scan(/(\w|-)+/).size
14
+ end
15
+
16
+ end
@@ -0,0 +1,3 @@
1
+ module Readingtime
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,18 @@
1
+ require "readingtime/version"
2
+ require "readingtime/core_ext"
3
+
4
+ module Readingtime
5
+
6
+ def self.minutes(words)
7
+ (words / 200).floor
8
+ end
9
+
10
+ def self.seconds(words)
11
+ (words % 200 / (200 / 60)).floor
12
+ end
13
+
14
+ def self.format_seconds(seconds)
15
+ '%d:%02d' % seconds.divmod(60)
16
+ end
17
+
18
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "readingtime/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "readingtime"
7
+ s.version = Readingtime::VERSION
8
+ s.authors = ["Gareth Rees"]
9
+ s.email = ["gareth@garethrees.co.uk"]
10
+ s.homepage = "http://github.com/garethrees/readingtime"
11
+ s.summary = %q{Estimates reading time}
12
+ s.description = %q{Estimates reading time of a Ruby String object}
13
+
14
+ s.rubyforge_project = "readingtime"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency "rspec", "~>2.6"
23
+
24
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
3
+ $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
4
+ require "readingtime"
5
+
6
+
7
+ describe Readingtime do
8
+ 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." }
9
+ 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) }
10
+
11
+
12
+ it "should calculate the length of the string input" do
13
+ short_text.calculate_size.should == short_text.scan(/(\w|-)+/).size
14
+ end
15
+
16
+
17
+ it "should calculate the number of minutes the reading should take" do
18
+ words = short_text.calculate_size
19
+ Readingtime.minutes(words).should == (words / 200).floor
20
+ end
21
+
22
+ it "should calculate the remaining seconds the reading should take" do
23
+ words = short_text.calculate_size
24
+ Readingtime.seconds(words).should == (words % 200 / (200 / 60)).floor
25
+ end
26
+
27
+ it "should format the reading time in an HH:MM format" do
28
+ Readingtime.format_seconds(3600).should == "60:00"
29
+ Readingtime.format_seconds(60).should == "1:00"
30
+ Readingtime.format_seconds(10).should == "0:10"
31
+ Readingtime.format_seconds(1).should == "0:01"
32
+ end
33
+
34
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: readingtime
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.3
6
+ platform: ruby
7
+ authors:
8
+ - Gareth Rees
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-27 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: "2.6"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: Estimates reading time of a Ruby String object
38
+ email:
39
+ - gareth@garethrees.co.uk
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - README.md
50
+ - Rakefile
51
+ - lib/readingtime.rb
52
+ - lib/readingtime/core_ext.rb
53
+ - lib/readingtime/version.rb
54
+ - readingtime.gemspec
55
+ - spec/readingtime_spec.rb
56
+ homepage: http://github.com/garethrees/readingtime
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project: readingtime
79
+ rubygems_version: 1.8.10
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Estimates reading time
83
+ test_files:
84
+ - spec/readingtime_spec.rb