readingtime 0.0.3 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +33 -7
- data/lib/readingtime.rb +4 -3
- data/lib/readingtime/core_ext.rb +1 -1
- data/lib/readingtime/version.rb +1 -1
- data/spec/readingtime_spec.rb +19 -12
- metadata +36 -43
data/README.md
CHANGED
@@ -5,14 +5,40 @@
|
|
5
5
|
|
6
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
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
8
|
## How to Use
|
11
9
|
|
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
|
10
|
+
[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.
|
11
|
+
|
12
|
+
### Install
|
13
|
+
|
14
|
+
gem install readingtime
|
15
|
+
|
16
|
+
### In irb
|
17
|
+
|
18
|
+
$ irb
|
19
|
+
> require 'readingtime'
|
20
|
+
=> true
|
21
|
+
> @words = "Lorem ipsum dolor sit amet"
|
22
|
+
> @words.reading_time
|
23
|
+
=> "0:01"
|
24
|
+
|
25
|
+
### In Your App
|
26
|
+
|
27
|
+
<article>
|
28
|
+
|
29
|
+
<header>
|
30
|
+
<h1><%= @article.title %></h1>
|
31
|
+
<span class="readingtime">Estimated reading time – <%= @article.body.reading_time %></span>
|
32
|
+
</header>
|
33
|
+
|
34
|
+
<%= @article.body %>
|
35
|
+
|
36
|
+
</article>
|
37
|
+
|
38
|
+
And voila!
|
39
|
+
|
40
|
+
![Screenshot of readingtime in use](https://github-screenshots.s3.amazonaws.com/readingtime-view.png "Screenshot of readingtime in use")
|
13
41
|
|
14
|
-
|
42
|
+
## Thanks
|
15
43
|
|
16
|
-
|
17
|
-
@words.reading_time
|
18
|
-
=> "0:01"
|
44
|
+
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.
|
data/lib/readingtime.rb
CHANGED
@@ -3,16 +3,17 @@ require "readingtime/core_ext"
|
|
3
3
|
|
4
4
|
module Readingtime
|
5
5
|
|
6
|
-
def self.
|
7
|
-
(words / 200).floor
|
6
|
+
def self.minutes_in_seconds(words)
|
7
|
+
(words / 200).floor * 60
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.seconds(words)
|
11
11
|
(words % 200 / (200 / 60)).floor
|
12
12
|
end
|
13
13
|
|
14
|
+
# TODO: Account for HH:MM:00
|
14
15
|
def self.format_seconds(seconds)
|
15
|
-
'%
|
16
|
+
'%02d:%02d' % seconds.divmod(60)
|
16
17
|
end
|
17
18
|
|
18
19
|
end
|
data/lib/readingtime/core_ext.rb
CHANGED
@@ -2,7 +2,7 @@ String.class_eval do
|
|
2
2
|
def reading_time
|
3
3
|
word_size = self.calculate_size
|
4
4
|
|
5
|
-
minutes = Readingtime.
|
5
|
+
minutes = Readingtime.minutes_in_seconds(word_size)
|
6
6
|
seconds = Readingtime.seconds(word_size)
|
7
7
|
|
8
8
|
return Readingtime.format_seconds((minutes + seconds))
|
data/lib/readingtime/version.rb
CHANGED
data/spec/readingtime_spec.rb
CHANGED
@@ -5,30 +5,37 @@ require "readingtime"
|
|
5
5
|
|
6
6
|
|
7
7
|
describe Readingtime do
|
8
|
-
let(:
|
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) }
|
8
|
+
let(:two_hundred_words) { ("Lorem " * 200) }
|
10
9
|
|
10
|
+
it "should calculate the reading time of some text" do
|
11
|
+
two_hundred_words.reading_time.should == "01:00"
|
12
|
+
end
|
11
13
|
|
12
14
|
it "should calculate the length of the string input" do
|
13
|
-
|
15
|
+
two_hundred_words.calculate_size.should == 200
|
14
16
|
end
|
15
17
|
|
16
|
-
|
17
18
|
it "should calculate the number of minutes the reading should take" do
|
18
|
-
words =
|
19
|
-
Readingtime.
|
19
|
+
words = two_hundred_words.calculate_size
|
20
|
+
Readingtime.minutes_in_seconds(words).should == 60
|
20
21
|
end
|
21
22
|
|
22
23
|
it "should calculate the remaining seconds the reading should take" do
|
23
|
-
words =
|
24
|
-
Readingtime.seconds(words).should ==
|
24
|
+
words = two_hundred_words.calculate_size
|
25
|
+
Readingtime.seconds(words).should == 0
|
25
26
|
end
|
26
27
|
|
27
|
-
it "should format the reading time in an
|
28
|
+
it "should format the reading time in an MM:SS format" do
|
28
29
|
Readingtime.format_seconds(3600).should == "60:00"
|
29
|
-
Readingtime.format_seconds(60).should == "
|
30
|
-
Readingtime.format_seconds(10).should == "
|
31
|
-
Readingtime.format_seconds(1).should == "
|
30
|
+
Readingtime.format_seconds(60).should == "01:00"
|
31
|
+
Readingtime.format_seconds(10).should == "00:10"
|
32
|
+
Readingtime.format_seconds(1).should == "00:01"
|
32
33
|
end
|
33
34
|
|
35
|
+
# it "should accept an options hash to format the output" do
|
36
|
+
# short_text.reading_time(:format => :basic).should == "0:23"
|
37
|
+
# short_text.reading_time(:format => :short).should == "23 seconds"
|
38
|
+
# short_text.reading_time(:format => :long).should == "0 minutes and 23 seconds"
|
39
|
+
# end
|
40
|
+
|
34
41
|
end
|
metadata
CHANGED
@@ -1,49 +1,45 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: readingtime
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.3
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Gareth Rees
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-09-30 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: rake
|
17
|
-
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &4992650 !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version:
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
24
22
|
type: :development
|
25
|
-
version_requirements: *id001
|
26
|
-
- !ruby/object:Gem::Dependency
|
27
|
-
name: rspec
|
28
23
|
prerelease: false
|
29
|
-
|
24
|
+
version_requirements: *4992650
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &4992330 !ruby/object:Gem::Requirement
|
30
28
|
none: false
|
31
|
-
requirements:
|
29
|
+
requirements:
|
32
30
|
- - ~>
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version:
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.6'
|
35
33
|
type: :development
|
36
|
-
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *4992330
|
37
36
|
description: Estimates reading time of a Ruby String object
|
38
|
-
email:
|
37
|
+
email:
|
39
38
|
- gareth@garethrees.co.uk
|
40
39
|
executables: []
|
41
|
-
|
42
40
|
extensions: []
|
43
|
-
|
44
41
|
extra_rdoc_files: []
|
45
|
-
|
46
|
-
files:
|
42
|
+
files:
|
47
43
|
- .gitignore
|
48
44
|
- Gemfile
|
49
45
|
- README.md
|
@@ -55,30 +51,27 @@ files:
|
|
55
51
|
- spec/readingtime_spec.rb
|
56
52
|
homepage: http://github.com/garethrees/readingtime
|
57
53
|
licenses: []
|
58
|
-
|
59
54
|
post_install_message:
|
60
55
|
rdoc_options: []
|
61
|
-
|
62
|
-
require_paths:
|
56
|
+
require_paths:
|
63
57
|
- lib
|
64
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
59
|
none: false
|
66
|
-
requirements:
|
67
|
-
- -
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
version:
|
70
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
65
|
none: false
|
72
|
-
requirements:
|
73
|
-
- -
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version:
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
76
70
|
requirements: []
|
77
|
-
|
78
71
|
rubyforge_project: readingtime
|
79
|
-
rubygems_version: 1.8.
|
72
|
+
rubygems_version: 1.8.5
|
80
73
|
signing_key:
|
81
74
|
specification_version: 3
|
82
75
|
summary: Estimates reading time
|
83
|
-
test_files:
|
76
|
+
test_files:
|
84
77
|
- spec/readingtime_spec.rb
|