Class: Scrubyt::ResultIndexer

Inherits:
Object
  • Object
show all
Defined in:
lib/scrubyt/core/scraping/result_indexer.rb

Overview

Selecting results based on indices

If the results is list-like (as opposed to a ‘hard’ result, like a price or a title), probably with a variable count of results (like tags, authors etc.), you may need just specific elements - like the last one, every third one, or at specific indices. In this case you should use the select_indices syntax.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ResultIndexer

Returns a new instance of ResultIndexer.



12
13
14
# File 'lib/scrubyt/core/scraping/result_indexer.rb', line 12

def initialize(*args)
  select_indices(*args)
end

Instance Attribute Details

#indices_to_extractObject (readonly)

Returns the value of attribute indices_to_extract.



10
11
12
# File 'lib/scrubyt/core/scraping/result_indexer.rb', line 10

def indices_to_extract
  @indices_to_extract
end

Instance Method Details

#select_indices_to_extract(ary) ⇒ Object

Perform selection of the desires result instances, based on their indices



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/scrubyt/core/scraping/result_indexer.rb', line 18

def select_indices_to_extract(ary)
  return ary if @indices_to_extract == nil
  to_keep = []
  @indices_to_extract.each {|e|
    if e.is_a? Symbol
      case e
      when :first
        to_keep << 0
      when :last
        to_keep << ary.size-1
      when :all_but_last
       (0..ary.size-2).each {|i| to_keep << i} 
      when :all_but_first
       (1..ary.size-1).each {|i| to_keep << i} 
      when :every_even
       (0..ary.size).each {|i| to_keep << i if (i % 2 == 1)}
      when :every_odd
       (0..ary.size).each {|i| to_keep << i if (i % 2 == 0)}
      when :every_second
       (0..ary.size).each {|i| to_keep << i if (i % 2 == 0)}
      when :every_third
       (0..ary.size).each {|i| to_keep << i if (i % 3 == 0)}
      when :every_fourth
       (0..ary.size).each {|i| to_keep << i if (i % 4 == 0)}
      end
    end
  }
  @indices_to_extract.each {|i| to_keep << i if !i.is_a? Symbol}
  to_keep.sort!
  ary.reject! {|e| !to_keep.include? ary.index(e)}
  ary
end