module ArticleReadingtime

Constants

VERSION

Public Class Methods

count_images(doc, options) click to toggle source
# File lib/article_readingtime.rb, line 32
def self.count_images(doc, options)
  images_count = doc.css('img').length

  images_count.times.reduce(0) do |total, i|
    total + if options[:max] - i * options[:step] > options[:min]
              options[:max] - i * options[:step]
            else
              options[:min]
            end
  end
end
count_text(doc, wpm) click to toggle source
# File lib/article_readingtime.rb, line 27
def self.count_text(doc, wpm)
  words = doc.inner_text.split(' ')
  (words.length / wpm.to_f * 60).round
end
default_image_options(options) click to toggle source
# File lib/article_readingtime.rb, line 19
def self.default_image_options(options)
  {
    max: options[:max] || IMAGE_MAX,
    min: options[:min] || IMAGE_MIN,
    step: options[:step] || IMAGE_STEP
  }
end
estimate_html(html, options = {}) click to toggle source
# File lib/article_readingtime.rb, line 11
def self.estimate_html(html, options = {})
  wpm = options[:wpm] || WORDS_PER_MINUTE
  images_options = default_image_options(options[:images] || {})

  doc = Nokogiri::HTML(html)
  count_text(doc, wpm) + count_images(doc, images_options)
end