Skip to content

Commit

Permalink
accessibility check tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
benbalter committed Jun 13, 2015
1 parent 0b21cd4 commit 8f7af2b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ Gemfile.lock
/tmp

mkmf.log
node_modules
120 changes: 76 additions & 44 deletions lib/site-inspector/checks/accessibility.rb
Expand Up @@ -5,58 +5,90 @@
class SiteInspector
class Endpoint
class Accessibility < Check

def section508
pa11y(:section508)

STANDARDS = {
section508: 'Section508', # Default standard
wcag2a: 'WCAG2A',
wcag2aa: 'WCAG2AA',
wcag2aaa: 'WCAG2AAA'
}

DEFAULT_LEVEL = "error"

def level
@level ||= DEFAULT_LEVEL
end

def wcag2a
pa11y(:wcag2a)

def level=(level)
raise ArgumentError, "Invalid level '#{level}'" unless %w[error, warning, notice].include?(level)
@level = level
end
def wcag2aa
pa11y(:wcag2aa)

def standard?(standard)
STANDARDS.keys.include?(standard)
end
def wcag2aaa
pa11y(:wcag2aaa)

def standard
@standard ||= STANDARDS.keys.first
end

private

def pa11y_installed?
!`which pa11y`.empty?

def standard=(standard)
raise ArgumentError, "Unknown standard '#{standard}'" unless standard?(standard)
@standard = standard
end

def pa11y(standard)
if pa11y_installed?
standards = {
section508: 'Section508',
wcag2a: 'WCAG2A',
wcag2aa: 'WCAG2AA',
wcag2aaa: 'WCAG2AAA'
}
standard = standards[standard]

cmd = "pa11y #{endpoint.uri} -s #{standard} -r json"
response = ""
error = nil

Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
response = stdout.read
error = stderr.read
end

if error && !error.empty?
raise error
end

JSON.parse(response)

def valid?
pa11y(standard)[:status] == :valid
end

def pa11y_version
output, status = Open3.capture2e("pa11y", "--version")
output.strip if status == 0
end

def pa11y?
!pa11y_version.nil?
end

def method_missing(method_sym, *arguments, &block)
if standard?(method_sym)
pa11y(method_sym)
else
raise "pa11y not found. To install: [sudo] npm install -g pa11y"
super
end
end


def respond_to?(method_sym, include_private = false)
if standard?(method_sym)
true
else
super
end
end

private

def pa11y(standard)
raise "pa11y not found. To install: [sudo] npm install -g pa11y" unless pa11y?
raise ArgumentError, "Unknown standard '#{standard}'" unless standard?(standard)

args = [
"--standard", STANDARDS[standard],
"--reporter", "json",
"--level", level,
endpoint.uri.to_s
]
output, status = Open3.capture2e("pa11y", *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 "Command `pa11y #{args.join(" ")}` failed: #{output}" if status == 1

{
status: status == 0 ? :valid : :invalid,
results: JSON.parse(output)
}
end
end
end
end
23 changes: 23 additions & 0 deletions package.json
@@ -0,0 +1,23 @@
{
"name": "site-inspector",
"version": "2.0.0",
"description": "Returns information about a domain's technology and capabilities",
"main": "site-inspector",
"dependencies": {
"pa11y": "^2.1.0"
},
"devDependencies": {},
"scripts": {
"test": "script/cibuild"
},
"repository": {
"type": "git",
"url": "git+https://github.com/benbalter/site-inspector.git"
},
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/benbalter/site-inspector/issues"
},
"homepage": "https://github.com/benbalter/site-inspector#readme"
}

0 comments on commit 8f7af2b

Please sign in to comment.