Class: Scrubyt::TextFilter

Inherits:
BaseFilter show all
Defined in:
lib/scrubyt/core/scraping/filters/text_filter.rb

Constant Summary

Constants inherited from BaseFilter

BaseFilter::EXAMPLE_TYPE_CHILDREN, BaseFilter::EXAMPLE_TYPE_COMPOUND, BaseFilter::EXAMPLE_TYPE_IMAGE, BaseFilter::EXAMPLE_TYPE_REGEXP, BaseFilter::EXAMPLE_TYPE_STRING, BaseFilter::EXAMPLE_TYPE_XPATH

Instance Attribute Summary

Attributes inherited from BaseFilter

#constraints, #example, #example_type, #final_result, #parent_pattern, #regexp, #temp_sink, #xpath

Instance Method Summary collapse

Methods inherited from BaseFilter

create, #method_missing, #throw_method_missing

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Scrubyt::BaseFilter

Instance Method Details

#evaluate(source) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/scrubyt/core/scraping/filters/text_filter.rb', line 4

def evaluate(source)
    return find_string(source) if @example =~ /^find\(/
    final_element_name = @example.scan(/^(.+?)\[/)[0][0]
    text = Regexp.escape(@example.scan(/\[(.+?)\]/)[0][0])

    index = @example.scan(/\]:(.+)/).flatten
    index = 0 if index.empty?
    index = index[0].to_i unless index[0] == "all"
    result = (index.is_a? Fixnum) ? (SharedUtils.traverse_for_match(source,/#{text}/)[index]) : (SharedUtils.traverse_for_match(source,/#{text}/))
    return "" unless result

    if index[0] == "all"
      result.inject([]) {|a,r| a << XPathUtils.traverse_up_until_name(r,final_element_name); a}
    else
      [XPathUtils.traverse_up_until_name(result,final_element_name)]
    end
end

#find_string(source) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/scrubyt/core/scraping/filters/text_filter.rb', line 22

def find_string(source)
  str = @example.scan(/find\((.+)\)/).flatten[0]
  strings_to_find = str.include?('|') ? str.split('|') : [str]
  strings_to_find.each do |s|
    result = SharedUtils.traverse_for_match(source,/#{s}/i)
    return [s] unless result.empty?
  end
  return []
end

#to_sexpObject



32
33
34
# File 'lib/scrubyt/core/scraping/filters/text_filter.rb', line 32

def to_sexp
  [:str, @example]
end