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
- .count_images(doc, options) ⇒ Object
- .count_text(doc, wpm) ⇒ Object
- .default_image_options(options) ⇒ Object
- .estimate_html(html, options = {}) ⇒ Object
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, ) images_count = doc.css('img').length images_count.times.reduce(0) do |total, i| total + if [:max] - i * [:step] > [:min] [:max] - i * [:step] else [: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.() { max: [:max] || IMAGE_MAX, min: [:min] || IMAGE_MIN, step: [: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, = {}) wpm = [:wpm] || WORDS_PER_MINUTE = ([:images] || {}) doc = Nokogiri::HTML(html) count_text(doc, wpm) + count_images(doc, ) end |