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(path, html, opts = {}) ⇒ Check

Returns a new instance of Check.


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

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

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

Instance Attribute Details

#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

Class Method Details

.subclassesObject


84
85
86
87
88
89
90
91
92
93
# File 'lib/html/proofer/check.rb', line 84

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


29
30
31
# File 'lib/html/proofer/check.rb', line 29

def add_issue(desc)
  @issues << desc
end

#external_href?(href) ⇒ Boolean

Returns:

  • (Boolean)

37
38
39
40
41
42
43
44
# File 'lib/html/proofer/check.rb', line 37

def external_href?(href)
  uri = URI.parse(href)
  %w( http https ).include?(uri.scheme)
rescue URI::BadURIError
  false
rescue URI::InvalidURIError
  false
end

#ignore_href?(href) ⇒ Boolean

Returns:

  • (Boolean)

46
47
48
49
50
51
52
53
# File 'lib/html/proofer/check.rb', line 46

def ignore_href?(href)
  uri = URI.parse(href)
  %w( mailto ).include?(uri.scheme) || @additional_href_ignores.include?(href)
rescue URI::BadURIError
  false
rescue URI::InvalidURIError
  false
end

#output_filenamesObject


33
34
35
# File 'lib/html/proofer/check.rb', line 33

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

#request_url(url) ⇒ Object


73
74
75
76
77
78
79
80
81
82
# File 'lib/html/proofer/check.rb', line 73

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)

25
26
27
# File 'lib/html/proofer/check.rb', line 25

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

#validate_url(href, issue_text) ⇒ Object


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/html/proofer/check.rb', line 55

def validate_url(href, issue_text)
  request = Typhoeus::Request.new(href, {:followlocation => true})
  request.on_complete do |response|
    if response.success?
      # no op
    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}")
    else
      # Received a non-successful http response.
      self.add_issue(issue_text + " HTTP request failed: " + response.code.to_s) unless response.code.to_s == "420"
    end
  end
  hydra.queue(request)
end