class BBC::A11y::CLI

Constants

HELP
NO_ERRORS_MESSAGE

Attributes

args[R]
stderr[R]
stdin[R]
stdout[R]

Public Class Methods

new(stdin, stdout, stderr, args) click to toggle source
# File lib/bbc/a11y/cli.rb, line 13
def initialize(stdin, stdout, stderr, args)
  @stdin, @stdout, @stderr, @args = stdin, stdout, stderr, args
end

Public Instance Methods

all_pages_tested(summary) click to toggle source
# File lib/bbc/a11y/cli.rb, line 46
def all_pages_tested(summary)
  messages = [
    summary_message(summary.pages, 'page', 'checked'),
    summary_message(summary.errors, 'error', 'found'),
    summary_message(summary.skips, 'standard', 'skipped')
  ]
  stdout.puts(messages.join(', '))
  @any_errors = summary.fail?
  unless @any_errors
    stdout.puts NO_ERRORS_MESSAGE
  end
end
call() click to toggle source
# File lib/bbc/a11y/cli.rb, line 17
def call
  Runner.new(settings, self).run
  exit 1 if @any_errors
rescue Configuration::ParseError => error
  exit_with_message error.message
rescue Configuration::MissingConfigurationFileError => error
  exit_with_message error.message
end
page_tested(page_settings, lint_result) click to toggle source
# File lib/bbc/a11y/cli.rb, line 26
def page_tested(page_settings, lint_result)
  if lint_result.passed?
    stdout.puts green("✓ #{page_settings.url}")
  else
    stdout.puts red("✗ #{page_settings.url}")
    current_section = nil
    current_name = nil
    lint_result.errors.each do |error|
      if error.section != current_section || error.name != current_name
        humanised_section = humanise(error.section)
        stdout.puts "  * #{humanised_section}: #{error.name}"
      end
      stdout.puts "    - #{error.message}"
      current_section = error.section
      current_name = error.name
    end
  end
  stdout.puts ""
end

Private Instance Methods

colourize_if_tty(message, colour) click to toggle source
# File lib/bbc/a11y/cli.rb, line 73
def colourize_if_tty(message, colour)
  if tty?
    message.send(colour)
  else
    message
  end
end
exit_with_message(*messages) click to toggle source
# File lib/bbc/a11y/cli.rb, line 95
def exit_with_message(*messages)
  messages.each { |message| stderr.puts message }
  exit 1
end
green(message) click to toggle source
# File lib/bbc/a11y/cli.rb, line 69
def green(message)
  colourize_if_tty(message, :green)
end
humanise(word) click to toggle source
# File lib/bbc/a11y/cli.rb, line 61
def humanise(word)
  word.gsub(/[A-Z]/) { |a| ' ' + a }.gsub(/^\w/) { |w| w.upcase }
end
red(message) click to toggle source
# File lib/bbc/a11y/cli.rb, line 65
def red(message)
  colourize_if_tty(message, :red)
end
settings() click to toggle source
# File lib/bbc/a11y/cli.rb, line 89
def settings
  return Configuration.for_urls(@args) if @args.any?
  configuration_file = File.expand_path("a11y.rb")
  Configuration.parse(configuration_file)
end
summary_message(count, noun, verb) click to toggle source
# File lib/bbc/a11y/cli.rb, line 85
def summary_message(count, noun, verb)
  "#{count} #{noun}#{count == 1 ? '' : 's'} #{verb}"
end
tty?() click to toggle source
# File lib/bbc/a11y/cli.rb, line 81
def tty?
  (stdout.tty? || ENV['TTY'] == 'true') && ENV['TTY'] != 'false'
end