Class: HTML::Proofer::Checks::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/html/proofer/check.rb

Direct Known Subclasses

Images, Links

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, path, html, opts = {}) ⇒ Check

Returns a new instance of Check.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/html/proofer/check.rb', line 15

def initialize(src, path, html, opts={})
  @src    = src
  @path   = path
  @html   = html
  @options = opts
  @issues = []
  @checked_urls = {}

  @hydra = Typhoeus::Hydra.hydra
  @additional_href_ignores = @options[:href_ignore] || []
end

Instance Attribute Details

#additional_href_ignoresObject (readonly)

Returns the value of attribute additional_href_ignores.



13
14
15
# File 'lib/html/proofer/check.rb', line 13

def additional_href_ignores
  @additional_href_ignores
end

#hydraObject (readonly)

Returns the value of attribute hydra.



13
14
15
# File 'lib/html/proofer/check.rb', line 13

def hydra
  @hydra
end

#issuesObject (readonly)

Returns the value of attribute issues.



13
14
15
# File 'lib/html/proofer/check.rb', line 13

def issues
  @issues
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/html/proofer/check.rb', line 13

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



13
14
15
# File 'lib/html/proofer/check.rb', line 13

def path
  @path
end

#srcObject (readonly)

Returns the value of attribute src.



13
14
15
# File 'lib/html/proofer/check.rb', line 13

def src
  @src
end

Class Method Details

.subclassesObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/html/proofer/check.rb', line 79

def self.subclasses
  classes = []

  ObjectSpace.each_object(Class) do |c|
    next unless c.superclass == self
    classes << c
  end

  classes
end

Instance Method Details

#add_issue(desc) ⇒ Object



31
32
33
# File 'lib/html/proofer/check.rb', line 31

def add_issue(desc)
  @issues << "#{@path.blue}: #{desc}"
end

#output_filenamesObject



35
36
37
# File 'lib/html/proofer/check.rb', line 35

def output_filenames
  Dir[@site.config[:output_dir] + '/**/*'].select{ |f| File.file?(f) }
end

#request_url(url) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/html/proofer/check.rb', line 68

def request_url(url)
  path = (url.path.nil? || url.path.empty? ? '/' : url.path)
  req = Net::HTTP::Head.new(path)
  http = Net::HTTP.new(url.host, url.port)
  if url.instance_of? URI::HTTPS
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  res = http.request(req)
end

#runObject

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/html/proofer/check.rb', line 27

def run
  raise NotImplementedError.new("HTML::Proofer::Check subclasses must implement #run")
end

#validate_url(href, issue_text) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/html/proofer/check.rb', line 39

def validate_url(href, issue_text)
  return @checked_urls[href] if @checked_urls.has_key? href
  request = Typhoeus::Request.new(href, {:followlocation => true})
  request.on_complete do |response|
    if response.success?
      @checked_urls[href] = true
    elsif response.timed_out?
      self.add_issue(issue_text + " got a time out")
    elsif response.code == 0
      # Could not get an http response, something's wrong.
      self.add_issue(issue_text + ". #{response.return_message}!")
    else
      response_code = response.code.to_s
      if %w(420 503).include?(response_code)
        # 420s usually come from rate limiting; let's ignore the query and try just the path
        uri = URI(href)
        response = Typhoeus.get(uri.scheme + "://" + uri.host + uri.path, {:followlocation => true})
        self.add_issue("#{issue_text} Originally, this was a #{response_code}. Now, the HTTP request failed again: #{response.code.to_s}") unless response.success?
      else
        # Received a non-successful http response.
        self.add_issue("#{issue_text} HTTP request failed: #{response_code}")
      end
    end

    @checked_urls[href] = false unless response.success?
  end
  hydra.queue(request)
end