module ReadTimeEstimator::String

Public Instance Methods

hours_in_words(hours) click to toggle source
# File lib/read_time_estimator.rb, line 36
def hours_in_words(hours)
  hours == 1 ? "#{hours} hour" : "#{hours} hours"
end
minutes_in_words(minutes) click to toggle source
# File lib/read_time_estimator.rb, line 45
def minutes_in_words(minutes)
  minutes == 1 ? "#{minutes} minute" : "#{minutes} minutes"
end
minutes_to_read() click to toggle source
# File lib/read_time_estimator.rb, line 5
def minutes_to_read
  self.split(' ').count/250.0
end
read_time_hours(time) click to toggle source
# File lib/read_time_estimator.rb, line 31
def read_time_hours(time)
  hours = (time/60).to_i
  hours >= 1 ? hours_in_words(hours) : nil
end
read_time_minutes(time) click to toggle source
# File lib/read_time_estimator.rb, line 40
def read_time_minutes(time)
  minutes = time.to_i
  minutes > 1 ? minutes_in_words(minutes) : nil
end
read_time_seconds(time) click to toggle source
# File lib/read_time_estimator.rb, line 49
def read_time_seconds(time)
  seconds = (time * 60).to_i
  seconds > 1 ? seconds_in_words(seconds) : nil
end
read_time_words() click to toggle source
# File lib/read_time_estimator.rb, line 9
def read_time_words
  time = minutes_to_read
  words = []
  if read_time_hours(time)
    words << read_time_hours(time)
    time = time % 60
  end
  if read_time_minutes(time)
    words << read_time_minutes(time)
    time = time % 1
  end
  if read_time_seconds(time)
    words << read_time_seconds(time)
  end
  words << "1 second" if words.empty?
  words = words.reverse
  words.insert(2, ", ") if words[2]
  words.insert(1, " and ") if words[1]
  words = words.reverse
  words.join + " to read"
end
seconds_in_words(seconds) click to toggle source
# File lib/read_time_estimator.rb, line 54
def seconds_in_words(seconds)
  seconds == 1 ? "#{seconds} second" : "#{seconds} seconds"
end