class SiteInspector::Endpoint::Accessibility

Constants

DEFAULT_LEVEL
REQUIRED_PA11Y_VERSION
STANDARDS

Public Class Methods

enabled?() click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 36
def enabled?
  @@enabled && pa11y?
end
pa11y() click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 40
def pa11y
  @pa11y ||= begin
    node_bin = File.expand_path('../../../node_modules/pa11y/bin', File.dirname(__FILE__))
    path = ['*', node_bin].join(File::PATH_SEPARATOR)
    Cliver::Dependency.new('pa11y.js', REQUIRED_PA11Y_VERSION, path: path)
  end
end
pa11y?() click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 30
def pa11y?
  return @pa11y_detected if defined? @pa11y_detected

  @pa11y_detected = !!pa11y.detect
end
pa11y_version() click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 23
def pa11y_version
  @pa11y_version ||= begin
    output, status = run_command('--version')
    output.strip if status.exitstatus.zero?
  end
end
run_command(args) click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 48
def run_command(args)
  Open3.capture2e(pa11y.detect, *args)
end

Public Instance Methods

check() click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 85
def check
  @check ||= run_pa11y(standard)
rescue Pa11yError
  nil
end
Also aliased as: to_h
errors() click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 81
def errors
  check[:results].count { |r| r['type'] == 'error' } if check
end
level() click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 53
def level
  @level ||= DEFAULT_LEVEL
end
level=(level) click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 57
def level=(level)
  raise ArgumentError, "Invalid level '#{level}'" unless %i[error warning notice].include?(level)

  @level = level
end
method_missing(method_sym, *arguments, &block) click to toggle source
Calls superclass method
# File lib/site-inspector/checks/accessibility.rb, line 92
def method_missing(method_sym, *arguments, &block)
  if standard?(method_sym)
    run_pa11y(method_sym)
  else
    super
  end
end
respond_to_missing?(method_sym, include_private = false) click to toggle source
Calls superclass method
# File lib/site-inspector/checks/accessibility.rb, line 100
def respond_to_missing?(method_sym, include_private = false)
  if standard?(method_sym)
    true
  else
    super
  end
end
standard() click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 67
def standard
  @standard ||= STANDARDS.keys.first
end
standard=(standard) click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 71
def standard=(standard)
  raise ArgumentError, "Unknown standard '#{standard}'" unless standard?(standard)

  @standard = standard
end
standard?(standard) click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 63
def standard?(standard)
  STANDARDS.key?(standard)
end
to_h()
Alias for: check
valid?() click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 77
def valid?
  check[:valid] if check
end

Private Instance Methods

run_pa11y(standard) click to toggle source
# File lib/site-inspector/checks/accessibility.rb, line 110
def run_pa11y(standard)
  self.class.pa11y.detect! unless ENV['SKIP_PA11Y_CHECK']
  raise ArgumentError, "Unknown standard '#{standard}'" unless standard?(standard)

  args = [
    '--standard', STANDARDS[standard],
    '--reporter', 'json',
    '--level',    level.to_s,
    endpoint.uri.to_s
  ]
  output, status = self.class.run_command(args)

  # Pa11y exit codes: https://github.com/nature/pa11y#exit-codes
  # 0: No errors, 1: Technical error within pa11y, 2: accessibility error (configurable via --level)
  raise Pa11yError if status.exitstatus == 1

  {
    valid: status.exitstatus.zero?,
    results: JSON.parse(output)
  }
rescue Pa11yError, JSON::ParserError
  raise Pa11yError, "Command `pa11y #{args.join(' ')}` failed: #{output}"
end