Module: ArticleReadingtime

Defined in:
lib/article_readingtime.rb,
lib/article_readingtime/version.rb

Constant Summary collapse

VERSION =
'1.0.0'.freeze

Class Method Summary collapse

Class Method Details

.count_images(doc, options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



27
28
29
30
# 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) ⇒ Object



19
20
21
22
23
24
25
# 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 = {}) ⇒ Object



11
12
13
14
15
16
17
# 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