Class: HTML::Proofer::Checkable

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/html/proofer/checkable.rb

Overview

Represents the superclass from which all checks derive.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

create_nokogiri, swap

Constructor Details

#initialize(obj, check) ⇒ Checkable

Returns a new instance of Checkable.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/html/proofer/checkable.rb', line 11

def initialize(obj, check)
  obj.attributes.each_pair do |attribute, value|
    instance_variable_set("@#{attribute.tr('-', '_')}".to_sym, value.value)
  end

  @content = obj.content
  @check = check
  @checked_paths = {}
  @type = self.class.name
  @line = obj.line

  if @href && @check.options[:href_swap]
    @href = swap(@href, @check.options[:href_swap])
  end

  # fix up missing protocols
  @href.insert 0, 'http:' if @href =~ %r{^//}
  @src.insert 0, 'http:' if @src =~ %r{^//}
end

Instance Attribute Details

#lineObject (readonly)

Returns the value of attribute line


9
10
11
# File 'lib/html/proofer/checkable.rb', line 9

def line
  @line
end

Instance Method Details

#absolute_pathObject


119
120
121
122
# File 'lib/html/proofer/checkable.rb', line 119

def absolute_path
  path = file_path || @check.path
  File.expand_path path, Dir.pwd
end

#exists?Boolean

checks if a file exists relative to the current pwd

Returns:

  • (Boolean)

114
115
116
117
# File 'lib/html/proofer/checkable.rb', line 114

def exists?
  return @checked_paths[absolute_path] if @checked_paths.key? absolute_path
  @checked_paths[absolute_path] = File.exist? absolute_path
end

#external?Boolean

path is external to the file

Returns:

  • (Boolean)

81
82
83
# File 'lib/html/proofer/checkable.rb', line 81

def external?
  !internal?
end

#file_pathObject


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/html/proofer/checkable.rb', line 90

def file_path
  return if path.nil?

  if path =~ /^\// # path relative to root
    base = File.directory?(@check.src) ? @check.src : File.dirname(@check.src)
  elsif File.exist?(File.expand_path path, @check.src) # relative links, path is a file
    base = File.dirname @check.path
  elsif File.exist?(File.join(File.dirname(@check.path), path)) # relative links in nested dir, path is a file
    base = File.dirname @check.path
  else # relative link, path is a directory
    base = @check.path
  end

  file = File.join base, path

  # implicit index support
  if File.directory?(file) && !unslashed_directory?(file)
    file = File.join file, @check.options[:directory_index_file]
  end

  file
end

#follow_location?Boolean

Returns:

  • (Boolean)

140
141
142
# File 'lib/html/proofer/checkable.rb', line 140

def follow_location?
  @check.options[:typhoeus] && @check.options[:typhoeus][:followlocation]
end

#hashObject


49
50
51
# File 'lib/html/proofer/checkable.rb', line 49

def hash
  parts.fragment unless parts.nil?
end

#ignore?Boolean

Returns:

  • (Boolean)

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

def ignore?
  return true if @data_proofer_ignore

  case @type
  when 'FaviconCheckable'
    return true if url.match(/^data:image/)
  when 'LinkCheckable'
    return true if ignores_pattern_check(@check.href_ignores)
  when 'ImageCheckable'
    return true if url.match(/^data:image/)
    return true if ignores_pattern_check(@check.alt_ignores)
  end
end

#ignores_pattern_check(links) ⇒ Object


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/html/proofer/checkable.rb', line 124

def ignores_pattern_check(links)
  links.each do |ignore|
    if ignore.is_a? String
      return true if ignore == url
    elsif ignore.is_a? Regexp
      return true if ignore =~ url
    end
  end

  false
end

#internal?Boolean

path is an anchor or a query

Returns:

  • (Boolean)

86
87
88
# File 'lib/html/proofer/checkable.rb', line 86

def internal?
  url.start_with? '#', '?'
end

#non_http_remote?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/html/proofer/checkable.rb', line 62

def non_http_remote?
  !scheme.nil? && !remote?
end

#partsObject


39
40
41
42
43
# File 'lib/html/proofer/checkable.rb', line 39

def parts
  @parts ||= Addressable::URI.parse url
rescue URI::Error
  @parts = nil
end

#pathObject


45
46
47
# File 'lib/html/proofer/checkable.rb', line 45

def path
  CGI.unescape parts.path unless parts.nil?
end

#remote?Boolean

path is to an external server

Returns:

  • (Boolean)

58
59
60
# File 'lib/html/proofer/checkable.rb', line 58

def remote?
  %w( http https ).include? scheme
end

#schemeObject


53
54
55
# File 'lib/html/proofer/checkable.rb', line 53

def scheme
  parts.scheme unless parts.nil?
end

#unslashed_directory?(file) ⇒ Boolean

Returns:

  • (Boolean)

136
137
138
# File 'lib/html/proofer/checkable.rb', line 136

def unslashed_directory?(file)
  File.directory?(file) && !file.end_with?(File::SEPARATOR) && !follow_location?
end

#urlObject


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

def url
  @src || @srcset || @href || ''
end

#valid?Boolean

Returns:

  • (Boolean)

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

def valid?
  !parts.nil?
end