Class: Blinkr::Extensions::Links

Inherits:
Object
  • Object
show all
Includes:
HttpUtils
Defined in:
lib/blinkr/extensions/links.rb

Instance Method Summary collapse

Methods included from HttpUtils

#retry?, #sanitize

Constructor Details

#initialize(config) ⇒ Links

Returns a new instance of Links.



8
9
10
11
# File 'lib/blinkr/extensions/links.rb', line 8

def initialize config
  @config = config
  @links = {}
end

Instance Method Details

#analyze(context, typhoeus) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/blinkr/extensions/links.rb', line 26

def analyze context, typhoeus
  puts "----------------------" if @config.verbose
  puts " #{@links.length} links to check " if @config.verbose
  puts "----------------------" if @config.verbose
  @links.each do |url, |
    typhoeus.process(url, @config.max_retrys) do |resp|
      puts "Loaded #{url} via typhoeus #{'(cached)' if resp.cached?}" if @config.verbose
      unless resp.success? || resp.code == 200
        .each do |src|
          code = resp.code.to_i unless resp.code.nil? || resp.code == 0
          if resp.status_message.nil?
            message = resp.return_message
          else
            message = resp.status_message
            detail = resp.return_message unless resp.return_message == "No error"
          end
          src[:page].errors << OpenStruct.new({ :severity => 'danger', :category => 'Resources missing', :type => '<a href=""> target cannot be loaded', :url => url, :title => "#{url} (line #{src[:line]})", :code => code, :message => message, :detail => detail, :snippet => src[:snippet], :icon => 'fa-bookmark-o' })
        end
      end
    end
  end
  typhoeus.hydra.run
end

#collect(page) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/blinkr/extensions/links.rb', line 13

def collect page
  page.body.css('a[href]').each do |a|
    attr = a.attribute('href')
    src = page.response.effective_url
    url = attr.value
    url = sanitize url, src
    unless url.nil? || @config.skipped?(url)
      @links[url] ||= []
      @links[url] << {:page => page, :line => attr.line, :snippet => attr.parent.to_s}
    end
  end
end