Class: Plumnailer::Chooser
- Inherits:
-
Object
- Object
- Plumnailer::Chooser
- Defined in:
- lib/plumnailer/chooser.rb
Overview
Find the most representative image on a page.
Instance Attribute Summary collapse
-
#doc_parser ⇒ Object
Returns the value of attribute doc_parser.
-
#fetcher ⇒ Object
Returns the value of attribute fetcher.
-
#img_comparator ⇒ Object
Returns the value of attribute img_comparator.
-
#img_parser ⇒ Object
Returns the value of attribute img_parser.
-
#img_url_filters ⇒ Object
Returns the value of attribute img_url_filters.
Instance Method Summary collapse
-
#choose(url, options = {}) ⇒ Object
Find the most representative image on a page.
-
#initialize ⇒ Chooser
constructor
A new instance of Chooser.
Constructor Details
#initialize ⇒ Chooser
Returns a new instance of Chooser.
6 7 8 9 10 11 12 |
# File 'lib/plumnailer/chooser.rb', line 6 def initialize @fetcher = Plumnailer::Fetcher.new @doc_parser = Plumnailer::DocParser.new @img_url_filters = [Plumnailer::ImgUrlFilter.new] @img_parser = Plumnailer::ImgParser.new(fetcher) @img_comparator = Plumnailer::ImgComparator end |
Instance Attribute Details
#doc_parser ⇒ Object
Returns the value of attribute doc_parser.
44 45 46 |
# File 'lib/plumnailer/chooser.rb', line 44 def doc_parser @doc_parser end |
#fetcher ⇒ Object
Returns the value of attribute fetcher.
43 44 45 |
# File 'lib/plumnailer/chooser.rb', line 43 def fetcher @fetcher end |
#img_comparator ⇒ Object
Returns the value of attribute img_comparator.
47 48 49 |
# File 'lib/plumnailer/chooser.rb', line 47 def img_comparator @img_comparator end |
#img_parser ⇒ Object
Returns the value of attribute img_parser.
46 47 48 |
# File 'lib/plumnailer/chooser.rb', line 46 def img_parser @img_parser end |
#img_url_filters ⇒ Object
Returns the value of attribute img_url_filters.
45 46 47 |
# File 'lib/plumnailer/chooser.rb', line 45 def img_url_filters @img_url_filters end |
Instance Method Details
#choose(url, options = {}) ⇒ Object
Find the most representative image on a page.
Return the best image or nil if no suitable images are found.
If options is passed in return up to the top n images. If this option is used the the method will always return of list of size 0 to n.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/plumnailer/chooser.rb', line 20 def choose(url, ={}) doc_string = fetcher.fetch(url) doc = doc_parser.parse(doc_string, url) img_abs_urls = doc.img_abs_urls.dup img_url_filters.each do |filter| img_abs_urls.delete_if { |i| filter.reject?(i) } end imgs = img_parser.parse(img_abs_urls) imgs.each do |img| # set source document on image so it can be used in comparator img.doc = doc img.extend @img_comparator end imgs.sort! [:top] ? imgs.first([:top]) : imgs.first end |