Class: Plumnailer::Chooser

Inherits:
Object
  • Object
show all
Defined in:
lib/plumnailer/chooser.rb

Overview

Find the most representative image on a page.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChooser

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_parserObject

Returns the value of attribute doc_parser.



44
45
46
# File 'lib/plumnailer/chooser.rb', line 44

def doc_parser
  @doc_parser
end

#fetcherObject

Returns the value of attribute fetcher.



43
44
45
# File 'lib/plumnailer/chooser.rb', line 43

def fetcher
  @fetcher
end

#img_comparatorObject

Returns the value of attribute img_comparator.



47
48
49
# File 'lib/plumnailer/chooser.rb', line 47

def img_comparator
  @img_comparator
end

#img_parserObject

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_filtersObject

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, 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