readingtime 0.3.1 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -0
- data/lib/readingtime.rb +15 -26
- data/lib/readingtime/calculator.rb +86 -0
- data/lib/readingtime/core_ext.rb +3 -20
- data/lib/readingtime/version.rb +2 -1
- data/readingtime.gemspec +2 -1
- data/spec/readingtime/calculator_spec.rb +102 -0
- metadata +17 -9
- checksums.yaml +0 -7
data/.travis.yml
CHANGED
data/lib/readingtime.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
Dir[File.dirname(__FILE__) + '/readingtime/*.rb'].each do |file|
|
2
3
|
require file
|
3
4
|
end
|
@@ -9,50 +10,38 @@ module Readingtime
|
|
9
10
|
end
|
10
11
|
|
11
12
|
def self.hms(secs)
|
12
|
-
|
13
|
-
|
14
|
-
secs -= h * 3600
|
15
|
-
m = secs / 60
|
16
|
-
secs -= m * 60
|
17
|
-
[h, m, secs]
|
13
|
+
calculator = Calculator.new(:reading_speed => self.reading_speed)
|
14
|
+
calculator.hms(secs)
|
18
15
|
end
|
19
16
|
|
20
17
|
def self.minutes_in_seconds(words)
|
21
|
-
(
|
18
|
+
calculator = Calculator.new(:reading_speed => self.reading_speed)
|
19
|
+
calculator.minutes_in_seconds(words)
|
22
20
|
end
|
23
21
|
|
24
22
|
def self.seconds(words)
|
25
|
-
|
23
|
+
calculator = Calculator.new(:reading_speed => self.reading_speed)
|
24
|
+
calculator.seconds(words)
|
26
25
|
end
|
27
26
|
|
28
27
|
# TODO: Account for HH:MM:00
|
29
28
|
def self.format_seconds(seconds)
|
30
|
-
|
29
|
+
calculator = Calculator.new(:reading_speed => self.reading_speed)
|
30
|
+
calculator.format_seconds(seconds)
|
31
31
|
end
|
32
32
|
|
33
33
|
def self.format_words(seconds)
|
34
|
-
|
35
|
-
|
36
|
-
else
|
37
|
-
"#{ seconds } seconds"
|
38
|
-
end
|
39
|
-
|
34
|
+
calculator = Calculator.new(:reading_speed => self.reading_speed)
|
35
|
+
calculator.format_words(seconds)
|
40
36
|
end
|
41
37
|
|
42
38
|
def self.format_approx(seconds)
|
43
|
-
|
44
|
-
|
45
|
-
else
|
46
|
-
'%d seconds' % seconds
|
47
|
-
end
|
39
|
+
calculator = Calculator.new(:reading_speed => self.reading_speed)
|
40
|
+
calculator.format_approx(seconds)
|
48
41
|
end
|
49
42
|
|
50
43
|
def self.format_full(hms)
|
51
|
-
|
52
|
-
|
53
|
-
r << "#{m} #{m == 1 ? 'min' : 'mins'}" if m > 0
|
54
|
-
r << "#{s} #{s == 1 ? 'sec' : 'secs'}" if s > 0
|
55
|
-
r.join(" ")
|
44
|
+
calculator = Calculator.new(:reading_speed => self.reading_speed)
|
45
|
+
calculator.format_full(hms)
|
56
46
|
end
|
57
|
-
|
58
47
|
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Readingtime
|
3
|
+
class Calculator
|
4
|
+
attr_reader :reading_speed
|
5
|
+
|
6
|
+
def initialize(opts = {})
|
7
|
+
@reading_speed = opts[:reading_speed] || 200
|
8
|
+
end
|
9
|
+
|
10
|
+
def reading_time(words, opts = {})
|
11
|
+
format_options = opts[:format] || :basic
|
12
|
+
|
13
|
+
word_size = calculate_size(words)
|
14
|
+
minutes = Readingtime.minutes_in_seconds(word_size)
|
15
|
+
seconds = Readingtime.seconds(word_size)
|
16
|
+
|
17
|
+
case format_options
|
18
|
+
when :basic
|
19
|
+
format_seconds((minutes + seconds))
|
20
|
+
when :long
|
21
|
+
format_words((minutes + seconds))
|
22
|
+
when :approx
|
23
|
+
format_approx((minutes + seconds))
|
24
|
+
when :full
|
25
|
+
hms = hms(minutes + seconds)
|
26
|
+
format_full(hms)
|
27
|
+
when :raw
|
28
|
+
hms(minutes + seconds)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Calculations
|
33
|
+
|
34
|
+
def minutes_in_seconds(words)
|
35
|
+
(words / reading_speed).floor * 60
|
36
|
+
end
|
37
|
+
|
38
|
+
def seconds(words)
|
39
|
+
(words % reading_speed / (reading_speed / 60)).floor
|
40
|
+
end
|
41
|
+
|
42
|
+
def calculate_size(words)
|
43
|
+
words.scan(/(\w|-)+/).size
|
44
|
+
end
|
45
|
+
|
46
|
+
# Formatting
|
47
|
+
|
48
|
+
def hms(secs)
|
49
|
+
h, m, s = 0, 0, 0
|
50
|
+
h = secs / 3600
|
51
|
+
secs -= h * 3600
|
52
|
+
m = secs / 60
|
53
|
+
secs -= m * 60
|
54
|
+
[h, m, secs]
|
55
|
+
end
|
56
|
+
|
57
|
+
# TODO: Account for HH:MM:00
|
58
|
+
def format_seconds(seconds)
|
59
|
+
'%02d:%02d' % seconds.divmod(60)
|
60
|
+
end
|
61
|
+
|
62
|
+
def format_words(seconds)
|
63
|
+
if seconds >= 60
|
64
|
+
'%d minutes and %d seconds' % seconds.divmod(60)
|
65
|
+
else
|
66
|
+
"#{ seconds } seconds"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def format_approx(seconds)
|
71
|
+
if seconds > 59
|
72
|
+
'%d minutes' % (seconds.to_f/60).round
|
73
|
+
else
|
74
|
+
'%d seconds' % seconds
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def format_full(hms)
|
79
|
+
r, h, m, s = [], hms[0], hms[1], hms[2]
|
80
|
+
r << "#{h} #{h == 1 ? 'hr' : 'hrs'}" if h > 0
|
81
|
+
r << "#{m} #{m == 1 ? 'min' : 'mins'}" if m > 0
|
82
|
+
r << "#{s} #{s == 1 ? 'sec' : 'secs'}" if s > 0
|
83
|
+
r.join(" ")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/readingtime/core_ext.rb
CHANGED
@@ -1,29 +1,12 @@
|
|
1
|
+
# encoding: utf-8
|
1
2
|
String.class_eval do
|
2
3
|
def reading_time(options = {})
|
4
|
+
calculator = options[:calculator] || Readingtime::Calculator.new
|
3
5
|
format_options = options[:format] || :basic
|
4
|
-
|
5
|
-
word_size = self.calculate_size
|
6
|
-
minutes = Readingtime.minutes_in_seconds(word_size)
|
7
|
-
seconds = Readingtime.seconds(word_size)
|
8
|
-
|
9
|
-
case format_options
|
10
|
-
when :basic
|
11
|
-
Readingtime.format_seconds((minutes + seconds))
|
12
|
-
when :long
|
13
|
-
Readingtime.format_words((minutes + seconds))
|
14
|
-
when :approx
|
15
|
-
Readingtime.format_approx((minutes + seconds))
|
16
|
-
when :full
|
17
|
-
hms = Readingtime.hms(minutes + seconds)
|
18
|
-
Readingtime.format_full(hms)
|
19
|
-
when :raw
|
20
|
-
Readingtime.hms(minutes + seconds)
|
21
|
-
end
|
22
|
-
|
6
|
+
calculator.reading_time(self, :format => format_options)
|
23
7
|
end
|
24
8
|
|
25
9
|
def calculate_size
|
26
10
|
self.scan(/(\w|-)+/).size
|
27
11
|
end
|
28
|
-
|
29
12
|
end
|
data/lib/readingtime/version.rb
CHANGED
data/readingtime.gemspec
CHANGED
@@ -18,7 +18,8 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
|
21
|
-
s.add_development_dependency 'rake'
|
21
|
+
s.add_development_dependency 'rake', '~> 10.5.0' if RUBY_VERSION < '1.9.3' # see: https://github.com/travis-ci/travis.rb/issues/380
|
22
|
+
s.add_development_dependency 'rake' unless RUBY_VERSION < '1.9.3'
|
22
23
|
s.add_development_dependency "rspec", "~>2.6"
|
23
24
|
|
24
25
|
end
|
@@ -0,0 +1,102 @@
|
|
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
|
+
describe Readingtime::Calculator do
|
7
|
+
let(:two_hundred_words) { ("Lorem " * 200) }
|
8
|
+
let(:three_hundred_words) { ("Lorem " * 300) }
|
9
|
+
let(:ten_words) { ("Lorem " * 10) }
|
10
|
+
|
11
|
+
describe '.new' do
|
12
|
+
it 'sets a default reading_speed' do
|
13
|
+
subject.reading_speed.should eq(200)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'allows a custom reading speed' do
|
17
|
+
subject = described_class.new(:reading_speed => 250)
|
18
|
+
subject.reading_speed.should eq(250)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#reading_time' do
|
23
|
+
it "should calculate the reading time of some text" do
|
24
|
+
subject.reading_time(two_hundred_words).should == "01:00"
|
25
|
+
subject.reading_time(three_hundred_words).should == "01:33"
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should accept an options hash to format the output" do
|
29
|
+
subject.reading_time(ten_words, :format => :long).should == "3 seconds"
|
30
|
+
subject.reading_time(two_hundred_words, :format => :basic).should == "01:00"
|
31
|
+
subject.reading_time(two_hundred_words, :format => :long).should == "1 minutes and 0 seconds"
|
32
|
+
subject.reading_time(two_hundred_words, :format => :approx).should == "1 minutes"
|
33
|
+
subject.reading_time(three_hundred_words, :format => :long).should == "1 minutes and 33 seconds"
|
34
|
+
subject.reading_time(three_hundred_words,:format => :approx).should == "2 minutes"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return a time in seconds when using approx with time less than 1 minute" do
|
38
|
+
subject.reading_time(ten_words, :format => :approx).should == "3 seconds"
|
39
|
+
subject.reading_time(three_hundred_words, :format => :full).should == '1 min 33 secs'
|
40
|
+
subject.reading_time(two_hundred_words, :format => :raw).should == [0, 1, 0]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#calculate_size' do
|
45
|
+
it "should calculate the length of the string input" do
|
46
|
+
subject.calculate_size(two_hundred_words).should == 200
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#minutes_in_seconds' do
|
51
|
+
it "should calculate the number of minutes the reading should take" do
|
52
|
+
words = subject.calculate_size(two_hundred_words)
|
53
|
+
subject.minutes_in_seconds(words).should == 60
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#seconds' do
|
58
|
+
it "should calculate the remaining seconds the reading should take" do
|
59
|
+
words = subject.calculate_size(two_hundred_words)
|
60
|
+
subject.seconds(words).should == 0
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#format_seconds' do
|
65
|
+
it "should format the reading time in an MM:SS format" do
|
66
|
+
subject.format_seconds(3600).should == "60:00"
|
67
|
+
subject.format_seconds(60).should == "01:00"
|
68
|
+
subject.format_seconds(10).should == "00:10"
|
69
|
+
subject.format_seconds(1).should == "00:01"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '#format_full' do
|
74
|
+
it "should format the reading time in words" do
|
75
|
+
subject.format_full([1, 0, 0]).should == "1 hr"
|
76
|
+
subject.format_full([2, 0, 0]).should == "2 hrs"
|
77
|
+
subject.format_full([1, 1, 0]).should == "1 hr 1 min"
|
78
|
+
subject.format_full([2, 2, 0]).should == "2 hrs 2 mins"
|
79
|
+
subject.format_full([1, 1, 1]).should == "1 hr 1 min 1 sec"
|
80
|
+
subject.format_full([2, 2, 2]).should == "2 hrs 2 mins 2 secs"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#format_approx' do
|
85
|
+
it "should format the reading time to the nearest minute" do
|
86
|
+
subject.format_approx(3600).should == "60 minutes"
|
87
|
+
subject.format_approx(119).should == "2 minutes"
|
88
|
+
subject.format_approx(61).should == "1 minutes"
|
89
|
+
subject.format_approx(60).should == "1 minutes"
|
90
|
+
subject.format_approx(59).should == "59 seconds"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe '#hms' do
|
95
|
+
it "should format the reading time in an array of hours, minutes, seconds" do
|
96
|
+
subject.hms(3600).should == [1, 0, 0]
|
97
|
+
subject.hms(60).should == [0, 1, 0]
|
98
|
+
subject.hms(10).should == [0, 0, 10]
|
99
|
+
subject.hms(1).should == [0, 0, 1]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
metadata
CHANGED
@@ -1,32 +1,36 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: readingtime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Gareth Rees
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2016-09-13 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: rake
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
|
-
- - '>='
|
19
|
+
- - ! '>='
|
18
20
|
- !ruby/object:Gem::Version
|
19
21
|
version: '0'
|
20
22
|
type: :development
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
|
-
- - '>='
|
27
|
+
- - ! '>='
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '0'
|
27
30
|
- !ruby/object:Gem::Dependency
|
28
31
|
name: rspec
|
29
32
|
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
30
34
|
requirements:
|
31
35
|
- - ~>
|
32
36
|
- !ruby/object:Gem::Version
|
@@ -34,6 +38,7 @@ dependencies:
|
|
34
38
|
type: :development
|
35
39
|
prerelease: false
|
36
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
37
42
|
requirements:
|
38
43
|
- - ~>
|
39
44
|
- !ruby/object:Gem::Version
|
@@ -52,31 +57,34 @@ files:
|
|
52
57
|
- README.md
|
53
58
|
- Rakefile
|
54
59
|
- lib/readingtime.rb
|
60
|
+
- lib/readingtime/calculator.rb
|
55
61
|
- lib/readingtime/core_ext.rb
|
56
62
|
- lib/readingtime/version.rb
|
57
63
|
- readingtime.gemspec
|
64
|
+
- spec/readingtime/calculator_spec.rb
|
58
65
|
- spec/readingtime_spec.rb
|
59
66
|
homepage: http://github.com/garethrees/readingtime
|
60
67
|
licenses: []
|
61
|
-
metadata: {}
|
62
68
|
post_install_message:
|
63
69
|
rdoc_options: []
|
64
70
|
require_paths:
|
65
71
|
- lib
|
66
72
|
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
67
74
|
requirements:
|
68
|
-
- - '>='
|
75
|
+
- - ! '>='
|
69
76
|
- !ruby/object:Gem::Version
|
70
77
|
version: '0'
|
71
78
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
72
80
|
requirements:
|
73
|
-
- - '>='
|
81
|
+
- - ! '>='
|
74
82
|
- !ruby/object:Gem::Version
|
75
83
|
version: '0'
|
76
84
|
requirements: []
|
77
85
|
rubyforge_project: readingtime
|
78
|
-
rubygems_version:
|
86
|
+
rubygems_version: 1.8.23
|
79
87
|
signing_key:
|
80
|
-
specification_version:
|
88
|
+
specification_version: 3
|
81
89
|
summary: Estimates reading time
|
82
90
|
test_files: []
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 55e652795450d840f9f5418723cb5300bf354650
|
4
|
-
data.tar.gz: 1a50232ab4d76f943c88df5e236839c6872807ee
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: cb518c3509e13c6d2c6129c1dab95b32d36c58f8aa128ab629a8ad79ccf6ada92e49b1dc498627e6134278b409e350d8973503947f56295c199847e1b9179497
|
7
|
-
data.tar.gz: 420dfceb1c1dd449cd5ce7d69b3401cffceb2c4e69bfeebab085ed1a9f083ce4afbdefb4292f5ff2a7b12d720d32fd475a8863f85d7e2636f229d58a9a7e64bf
|