readingtime 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01c44cb3bfde84a62cb226ff37d727a9842cc28f
4
+ data.tar.gz: ed919fb05b30d745a486d0f7a685248b038c1a2f
5
+ SHA512:
6
+ metadata.gz: 1a77483f0233efda41152c352cb7979399d3c9f97a2c6d7a93dac307e092750dde8055a70a1509eaefc129b2ce2be769edcd3256b8a079fbc7ceb448a8e1cbf1
7
+ data.tar.gz: 12a9e3694359349b3b7247adff6e3c04605279907973ac3898ee8cff3dc9ebe40e73666be6b78291a4c700000c40a085e346cb1d3d347de2e4cb44f9065694b3
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - 1.9.3
6
+ - 1.8.7
data/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # Estimate Reading Time
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/garethrees/readingtime.png)](http://travis-ci.org/garethrees/readingtime)
4
-
3
+ [![Build Status](https://secure.travis-ci.org/garethrees/readingtime.png)](http://travis-ci.org/garethrees/readingtime) [![Code Climate](https://codeclimate.com/github/garethrees/coderwall_ruby.png)](https://codeclimate.com/github/garethrees/coderwall_ruby)
5
4
 
6
5
  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
6
 
@@ -11,29 +10,29 @@ I use [iA Writer](http://iawriter.com "iA Writer") and find the *estimated readi
11
10
 
12
11
  ### Install
13
12
 
14
- gem install readingtime
13
+ gem install readingtime
15
14
 
16
15
  ### In irb
17
16
 
18
- $ irb
19
- > require 'readingtime'
20
- => true
21
- > @words = "Lorem ipsum dolor sit amet"
22
- > @words.reading_time
23
- => "00:01"
17
+ $ irb
18
+ > require 'readingtime'
19
+ => true
20
+ > @words = "Lorem ipsum dolor sit amet"
21
+ > @words.reading_time
22
+ => "00:01"
24
23
 
25
24
  ### In Your App
26
25
 
27
- <article>
26
+ <article>
28
27
 
29
- <header>
30
- <h1><%= @article.title %></h1>
31
- <span class="readingtime">Estimated reading time – <%= @article.body.reading_time %></span>
32
- </header>
28
+ <header>
29
+ <h1><%= @article.title %></h1>
30
+ <span class="readingtime">Estimated reading time – <%= @article.body.reading_time %></span>
31
+ </header>
33
32
 
34
- <%= @article.body %>
33
+ <%= @article.body %>
35
34
 
36
- </article>
35
+ </article>
37
36
 
38
37
  And voila!
39
38
 
@@ -43,14 +42,22 @@ And voila!
43
42
 
44
43
  You can also send in options to modify the formatting.
45
44
 
46
- # Default output
47
- @article.body.reading_time :format => :basic
48
- => "03:36"
49
-
50
- # Longer text output
51
- @article.body.reading_time :format => :long
52
- => "3 minutes and 36 seconds"
45
+ # Default output
46
+ @article.body.reading_time :format => :basic
47
+ => "03:36"
48
+
49
+ # Longer text output
50
+ @article.body.reading_time :format => :long
51
+ => "3 minutes and 36 seconds"
52
+
53
+ # Full text output
54
+ @article.body.reading_time :format => :full
55
+ => "1 hr 3 mins 36 secs"
56
+
57
+ # Raw output [hours, minutes, seconds]
58
+ @article.body.reading_time :format => :raw
59
+ => [1, 12, 23]
53
60
 
54
61
  ## Thanks
55
62
 
56
- 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.
63
+ 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
@@ -1,8 +1,18 @@
1
- require "readingtime/version"
2
- require "readingtime/core_ext"
1
+ Dir[File.dirname(__FILE__) + '/readingtime/*.rb'].each do |file|
2
+ require file
3
+ end
3
4
 
4
5
  module Readingtime
5
6
 
7
+ def self.hms(secs)
8
+ h, m, s = 0, 0, 0
9
+ h = secs / 3600
10
+ secs -= h * 3600
11
+ m = secs / 60
12
+ secs -= m * 60
13
+ [h, m, secs]
14
+ end
15
+
6
16
  def self.minutes_in_seconds(words)
7
17
  (words / 200).floor * 60
8
18
  end
@@ -28,4 +38,12 @@ module Readingtime
28
38
  end
29
39
  end
30
40
 
41
+ def self.format_full(hms)
42
+ r, h, m, s = [], hms[0], hms[1], hms[2]
43
+ r << "#{h} #{h == 1 ? 'hr' : 'hrs'}" if h > 0
44
+ r << "#{m} #{m == 1 ? 'min' : 'mins'}" if m > 0
45
+ r << "#{s} #{s == 1 ? 'sec' : 'secs'}" if s > 0
46
+ r.join(" ")
47
+ end
48
+
31
49
  end
@@ -5,7 +5,7 @@ String.class_eval do
5
5
  word_size = self.calculate_size
6
6
  minutes = Readingtime.minutes_in_seconds(word_size)
7
7
  seconds = Readingtime.seconds(word_size)
8
-
8
+
9
9
  case format_options
10
10
  when :basic
11
11
  Readingtime.format_seconds((minutes + seconds))
@@ -13,12 +13,17 @@ String.class_eval do
13
13
  Readingtime.format_words((minutes + seconds))
14
14
  when :approx
15
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)
16
21
  end
17
-
22
+
18
23
  end
19
24
 
20
25
  def calculate_size
21
26
  self.scan(/(\w|-)+/).size
22
27
  end
23
28
 
24
- end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Readingtime
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -35,6 +35,22 @@ describe Readingtime do
35
35
  Readingtime.format_seconds(1).should == "00:01"
36
36
  end
37
37
 
38
+ it "should format the reading time in words" do
39
+ Readingtime.format_full([1, 0, 0]).should == "1 hr"
40
+ Readingtime.format_full([2, 0, 0]).should == "2 hrs"
41
+ Readingtime.format_full([1, 1, 0]).should == "1 hr 1 min"
42
+ Readingtime.format_full([2, 2, 0]).should == "2 hrs 2 mins"
43
+ Readingtime.format_full([1, 1, 1]).should == "1 hr 1 min 1 sec"
44
+ Readingtime.format_full([2, 2, 2]).should == "2 hrs 2 mins 2 secs"
45
+ end
46
+
47
+ it "should format the reading time in an array of hours, minutes, seconds" do
48
+ Readingtime.hms(3600).should == [1, 0, 0]
49
+ Readingtime.hms(60).should == [0, 1, 0]
50
+ Readingtime.hms(10).should == [0, 0, 10]
51
+ Readingtime.hms(1).should == [0, 0, 1]
52
+ end
53
+
38
54
  it "should accept an options hash to format the output" do
39
55
  two_hundred_words.reading_time(:format => :basic).should == "01:00"
40
56
  two_hundred_words.reading_time(:format => :long).should == "1 minutes and 0 seconds"
@@ -45,6 +61,8 @@ describe Readingtime do
45
61
 
46
62
  it "should return a time in seconds when using approx with time less than 1 minute" do
47
63
  ten_words.reading_time(:format => :approx).should == "3 seconds"
64
+ three_hundred_words.reading_time(:format => :full).should == '1 min 33 secs'
65
+ two_hundred_words.reading_time(:format => :raw).should == [0, 1, 0]
48
66
  end
49
67
 
50
68
  end
metadata CHANGED
@@ -1,38 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: readingtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Gareth Rees
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-02 00:00:00.000000000 Z
11
+ date: 2014-02-22 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
- requirement: &70347145149640 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
- version_requirements: *70347145149640
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: rspec
27
- requirement: &70347145149140 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ~>
31
+ - - "~>"
31
32
  - !ruby/object:Gem::Version
32
33
  version: '2.6'
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *70347145149140
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.6'
36
41
  description: Estimates reading time of a Ruby String object
37
42
  email:
38
43
  - gareth@garethrees.co.uk
@@ -40,7 +45,8 @@ executables: []
40
45
  extensions: []
41
46
  extra_rdoc_files: []
42
47
  files:
43
- - .gitignore
48
+ - ".gitignore"
49
+ - ".travis.yml"
44
50
  - Gemfile
45
51
  - README.md
46
52
  - Rakefile
@@ -51,27 +57,26 @@ files:
51
57
  - spec/readingtime_spec.rb
52
58
  homepage: http://github.com/garethrees/readingtime
53
59
  licenses: []
60
+ metadata: {}
54
61
  post_install_message:
55
62
  rdoc_options: []
56
63
  require_paths:
57
64
  - lib
58
65
  required_ruby_version: !ruby/object:Gem::Requirement
59
- none: false
60
66
  requirements:
61
- - - ! '>='
67
+ - - ">="
62
68
  - !ruby/object:Gem::Version
63
69
  version: '0'
64
70
  required_rubygems_version: !ruby/object:Gem::Requirement
65
- none: false
66
71
  requirements:
67
- - - ! '>='
72
+ - - ">="
68
73
  - !ruby/object:Gem::Version
69
74
  version: '0'
70
75
  requirements: []
71
76
  rubyforge_project: readingtime
72
- rubygems_version: 1.8.15
77
+ rubygems_version: 2.2.0
73
78
  signing_key:
74
- specification_version: 3
79
+ specification_version: 4
75
80
  summary: Estimates reading time
76
81
  test_files:
77
82
  - spec/readingtime_spec.rb