class Plumnailer::Chooser
Find the most representative image on a page.
Attributes
doc_parser[RW]
fetcher[RW]
img_comparator[RW]
img_parser[RW]
img_url_filters[RW]
Public Class Methods
new()
click to toggle source
# 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
Public Instance Methods
choose(url, options={})
click to toggle source
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.
# File lib/plumnailer/chooser.rb, line 20 def choose(url, options={}) 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! options[:top] ? imgs.first(options[:top]) : imgs.first end