Class: BuilderLinks::Analize

Inherits:
Object
  • Object
show all
Defined in:
lib/builder_links/analize.rb

Instance Method Summary collapse

Constructor Details

#initialize(text, opts = {}) ⇒ Analize

Returns a new instance of Analize.



5
6
7
8
9
10
# File 'lib/builder_links/analize.rb', line 5

def initialize(text, opts = {})
  @options = {black_uris: []}.merge(opts)
  @doc = Nokogiri::HTML(text)
  @analized_text = nil
  @total_links = 0
end

Instance Method Details

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/builder_links/analize.rb', line 12

def run
  return @analized_text unless @analized_text.blank?

  BuilderLinks.patterns.each do |pattern|
    break if max_links_generated?
    next if black_pattern?(pattern)
    next if !@doc.content.include?(pattern[:anchortext])

    links_per_pattern = 0
    @doc.search('p').children.each do |child|
      break if max_links_generated?(links_per_pattern)

      if analize_node(child, pattern)
        links_per_pattern += 1
        @total_links += 1
      end
    end
  end

  @analized_text = @doc.at('body').nil? ? '' : @doc.at('body').inner_html
end


34
35
36
# File 'lib/builder_links/analize.rb', line 34

def total_links
  @total_links
end