Class: Readingtime::Calculator

Inherits:
Object
  • Object
show all
Defined in:
lib/readingtime/calculator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Calculator

Returns a new instance of Calculator.



6
7
8
# File 'lib/readingtime/calculator.rb', line 6

def initialize(opts = {})
  @reading_speed = opts[:reading_speed] || 200
end

Instance Attribute Details

#reading_speedObject (readonly)

Returns the value of attribute reading_speed.



4
5
6
# File 'lib/readingtime/calculator.rb', line 4

def reading_speed
  @reading_speed
end

Instance Method Details

#calculate_size(words) ⇒ Object



42
43
44
# File 'lib/readingtime/calculator.rb', line 42

def calculate_size(words)
  words.scan(/(\w|-)+/).size
end

#format_approx(seconds) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/readingtime/calculator.rb', line 70

def format_approx(seconds)
  if seconds > 59
    '%d minutes' % (seconds.to_f/60).round
  else
    '%d seconds' % seconds
  end
end

#format_full(hms) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/readingtime/calculator.rb', line 78

def format_full(hms)
  r, h, m, s = [], hms[0], hms[1], hms[2]
  r << "#{h} #{h == 1 ? 'hr' : 'hrs'}" if h > 0
  r << "#{m} #{m == 1 ? 'min' : 'mins'}" if m > 0
  r << "#{s} #{s == 1 ? 'sec' : 'secs'}" if s > 0
  r.join(" ")
end

#format_seconds(seconds) ⇒ Object

TODO: Account for HH:MM:00



58
59
60
# File 'lib/readingtime/calculator.rb', line 58

def format_seconds(seconds)
  '%02d:%02d' % seconds.divmod(60)
end

#format_words(seconds) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/readingtime/calculator.rb', line 62

def format_words(seconds)
  if seconds >= 60
    '%d minutes and %d seconds' % seconds.divmod(60)
  else
    "#{ seconds } seconds"
  end
end

#hms(secs) ⇒ Object

Formatting



48
49
50
51
52
53
54
55
# File 'lib/readingtime/calculator.rb', line 48

def hms(secs)
  h, m, s = 0, 0, 0
  h = secs / 3600
  secs -= h * 3600
  m = secs / 60
  secs -= m * 60
  [h, m, secs]
end

#minutes_in_seconds(words) ⇒ Object

Calculations



34
35
36
# File 'lib/readingtime/calculator.rb', line 34

def minutes_in_seconds(words)
  (words / reading_speed).floor * 60
end

#reading_time(words, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/readingtime/calculator.rb', line 10

def reading_time(words, opts = {})
  format_options = opts[:format] || :basic

  word_size = calculate_size(words)
  minutes = Readingtime.minutes_in_seconds(word_size)
  seconds = Readingtime.seconds(word_size)

  case format_options
  when :basic
    format_seconds((minutes + seconds))
  when :long
    format_words((minutes + seconds))
  when :approx
    format_approx((minutes + seconds))
  when :full
    hms = hms(minutes + seconds)
    format_full(hms)
  when :raw
    hms(minutes + seconds)
  end
end

#seconds(words) ⇒ Object



38
39
40
# File 'lib/readingtime/calculator.rb', line 38

def seconds(words)
  (words % reading_speed / (reading_speed / 60)).floor
end