readingtime 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -4,3 +4,5 @@ rvm:
4
4
  - 2.1.0
5
5
  - 1.9.3
6
6
  - 1.8.7
7
+ before_install:
8
+ - gem install bundler
@@ -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
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ module Readingtime
3
+ class Configuration
4
+ attr_accessor :reading_speed
5
+
6
+ def initialize
7
+ @reading_speed = 200
8
+ end
9
+ end
10
+ end
@@ -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
@@ -1,3 +1,4 @@
1
+ # encoding: utf-8
1
2
  module Readingtime
2
- VERSION = "0.3.1"
3
+ VERSION = "0.4.0"
3
4
  end
data/lib/readingtime.rb CHANGED
@@ -1,58 +1,58 @@
1
+ # encoding: utf-8
1
2
  Dir[File.dirname(__FILE__) + '/readingtime/*.rb'].each do |file|
2
3
  require file
3
4
  end
4
5
 
5
6
  module Readingtime
6
- #TODO: move this to a configuration object
7
+ class << self
8
+ attr_accessor :configuration
9
+ end
10
+
11
+ def self.configure
12
+ yield(configuration)
13
+ end
14
+
15
+ def self.configuration
16
+ @configuration ||= Configuration.new
17
+ end
18
+
7
19
  def self.reading_speed
8
- 200
20
+ configuration.reading_speed
9
21
  end
10
22
 
11
23
  def self.hms(secs)
12
- h, m, s = 0, 0, 0
13
- h = secs / 3600
14
- secs -= h * 3600
15
- m = secs / 60
16
- secs -= m * 60
17
- [h, m, secs]
24
+ calculator = Calculator.new(:reading_speed => self.reading_speed)
25
+ calculator.hms(secs)
18
26
  end
19
27
 
20
28
  def self.minutes_in_seconds(words)
21
- (words / self.reading_speed).floor * 60
29
+ calculator = Calculator.new(:reading_speed => self.reading_speed)
30
+ calculator.minutes_in_seconds(words)
22
31
  end
23
32
 
24
33
  def self.seconds(words)
25
- (words % self.reading_speed / (self.reading_speed / 60)).floor
34
+ calculator = Calculator.new(:reading_speed => self.reading_speed)
35
+ calculator.seconds(words)
26
36
  end
27
37
 
28
38
  # TODO: Account for HH:MM:00
29
39
  def self.format_seconds(seconds)
30
- '%02d:%02d' % seconds.divmod(60)
40
+ calculator = Calculator.new(:reading_speed => self.reading_speed)
41
+ calculator.format_seconds(seconds)
31
42
  end
32
43
 
33
44
  def self.format_words(seconds)
34
- if seconds >= 60
35
- '%d minutes and %d seconds' % seconds.divmod(60)
36
- else
37
- "#{ seconds } seconds"
38
- end
39
-
45
+ calculator = Calculator.new(:reading_speed => self.reading_speed)
46
+ calculator.format_words(seconds)
40
47
  end
41
48
 
42
49
  def self.format_approx(seconds)
43
- if seconds > 59
44
- '%d minutes' % (seconds.to_f/60).round
45
- else
46
- '%d seconds' % seconds
47
- end
50
+ calculator = Calculator.new(:reading_speed => self.reading_speed)
51
+ calculator.format_approx(seconds)
48
52
  end
49
53
 
50
54
  def self.format_full(hms)
51
- r, h, m, s = [], hms[0], hms[1], hms[2]
52
- r << "#{h} #{h == 1 ? 'hr' : 'hrs'}" if h > 0
53
- r << "#{m} #{m == 1 ? 'min' : 'mins'}" if m > 0
54
- r << "#{s} #{s == 1 ? 'sec' : 'secs'}" if s > 0
55
- r.join(" ")
55
+ calculator = Calculator.new(:reading_speed => self.reading_speed)
56
+ calculator.format_full(hms)
56
57
  end
57
-
58
58
  end
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
@@ -0,0 +1,27 @@
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::Configuration do
7
+
8
+ describe '.new' do
9
+ it 'sets the default reading_speed' do
10
+ subject.reading_speed.should == 200
11
+ end
12
+ end
13
+
14
+ describe '#reading_speed' do
15
+ it 'returns the reading_speed' do
16
+ subject.reading_speed.should == 200
17
+ end
18
+ end
19
+
20
+ describe '#reading_speed=' do
21
+ it 'sets the reading_speed' do
22
+ subject.reading_speed = 250
23
+ subject.reading_speed.should == 250
24
+ end
25
+ end
26
+
27
+ end
@@ -3,7 +3,6 @@ path = File.expand_path(File.dirname(__FILE__) + "/../lib/")
3
3
  $LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
4
4
  require "readingtime"
5
5
 
6
-
7
6
  describe Readingtime do
8
7
  let(:two_hundred_words) { ("Lorem " * 200) }
9
8
  let(:three_hundred_words) { ("Lorem " * 300) }
@@ -66,4 +65,20 @@ describe Readingtime do
66
65
  two_hundred_words.reading_time(:format => :raw).should == [0, 1, 0]
67
66
  end
68
67
 
68
+ it 'should use the default reading speed from configuration' do
69
+ Readingtime.reading_speed.should == 200
70
+ end
71
+
72
+ it "should use reading speed from configuration" do
73
+ Readingtime.configure do |config|
74
+ config.reading_speed = 250
75
+ end
76
+
77
+ Readingtime.reading_speed.should == 250
78
+
79
+ Readingtime.configure do |config|
80
+ config.reading_speed = 200
81
+ end
82
+ end
83
+
69
84
  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.1
4
+ version: 0.4.0
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: 2015-09-20 00:00:00.000000000 Z
12
+ date: 2016-09-24 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,36 @@ files:
52
57
  - README.md
53
58
  - Rakefile
54
59
  - lib/readingtime.rb
60
+ - lib/readingtime/calculator.rb
61
+ - lib/readingtime/configuration.rb
55
62
  - lib/readingtime/core_ext.rb
56
63
  - lib/readingtime/version.rb
57
64
  - readingtime.gemspec
65
+ - spec/readingtime/calculator_spec.rb
66
+ - spec/readingtime/configuration_spec.rb
58
67
  - spec/readingtime_spec.rb
59
68
  homepage: http://github.com/garethrees/readingtime
60
69
  licenses: []
61
- metadata: {}
62
70
  post_install_message:
63
71
  rdoc_options: []
64
72
  require_paths:
65
73
  - lib
66
74
  required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
67
76
  requirements:
68
- - - '>='
77
+ - - ! '>='
69
78
  - !ruby/object:Gem::Version
70
79
  version: '0'
71
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
72
82
  requirements:
73
- - - '>='
83
+ - - ! '>='
74
84
  - !ruby/object:Gem::Version
75
85
  version: '0'
76
86
  requirements: []
77
87
  rubyforge_project: readingtime
78
- rubygems_version: 2.0.14
88
+ rubygems_version: 1.8.23
79
89
  signing_key:
80
- specification_version: 4
90
+ specification_version: 3
81
91
  summary: Estimates reading time
82
92
  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