From 93b63bd583f2536e0868ca7c924ffe7924e40a3b Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Wed, 13 Mar 2013 13:03:12 -0500 Subject: [PATCH] clean up and pull out link checker / audit common code --- lib/cask/audit.rb | 53 ++++++--------------------------------- lib/cask/checkable.rb | 51 +++++++++++++++++++++++++++++++++++++ lib/cask/link_checker.rb | 54 ++++++++-------------------------------- 3 files changed, 69 insertions(+), 89 deletions(-) create mode 100644 lib/cask/checkable.rb diff --git a/lib/cask/audit.rb b/lib/cask/audit.rb index 2f5f96146..467ffc318 100644 --- a/lib/cask/audit.rb +++ b/lib/cask/audit.rb @@ -1,12 +1,12 @@ +require 'cask/checkable' + class Cask::Audit - attr_reader :cask, :errors, :warnings, :headers, :response_status + attr_reader :cask + + include Cask::Checkable - def initialize(cask, fetcher=Cask::Fetcher) + def initialize(cask) @cask = cask - @errors = [] - @warnings = [] - @headers = {} - @fetcher = fetcher end def run! @@ -14,44 +14,8 @@ class Cask::Audit return if errors? end - def add_error(message) - @errors << message - end - - def add_warning(message) - @warnings << message - end - - def errors? - !@errors.empty? - end - - def warnings? - !@warnings.empty? - end - - def result - if errors? - "#{Tty.red}failed#{Tty.reset}" - elsif warnings? - "#{Tty.yellow}warning#{Tty.reset}" - else - "#{Tty.green}passed#{Tty.reset}" - end - end - - def summary - summary = ["audit for #{cask}: #{result}"] - - @errors.each do |error| - summary << " #{Tty.red}-#{Tty.reset} #{error}" - end - - @warnings.each do |warning| - summary << " #{Tty.yellow}-#{Tty.reset} #{warning}" - end - - summary.join("\n") + def summary_header + "audit for #{cask}" end def _check_required_fields @@ -59,5 +23,4 @@ class Cask::Audit add_error "version is required" unless cask.version add_error "homepage is required" unless cask.homepage end - end diff --git a/lib/cask/checkable.rb b/lib/cask/checkable.rb new file mode 100644 index 000000000..a961fdba7 --- /dev/null +++ b/lib/cask/checkable.rb @@ -0,0 +1,51 @@ +module Cask::Checkable + def errors + Array(@errors) + end + + def warnings + Array(@warnings) + end + + def add_error(message) + @errors ||= [] + @errors << message + end + + def add_warning(message) + @warnings ||= [] + @warnings << message + end + + def errors? + Array(@errors).any? + end + + def warnings? + Array(@warnings).any? + end + + def result + if errors? + "#{Tty.red}failed#{Tty.reset}" + elsif warnings? + "#{Tty.yellow}warning#{Tty.reset}" + else + "#{Tty.green}passed#{Tty.reset}" + end + end + + def summary + summary = ["#{summary_header}: #{result}"] + + errors.each do |error| + summary << " #{Tty.red}-#{Tty.reset} #{error}" + end + + warnings.each do |warning| + summary << " #{Tty.yellow}-#{Tty.reset} #{warning}" + end + + summary.join("\n") + end +end diff --git a/lib/cask/link_checker.rb b/lib/cask/link_checker.rb index 99e5da658..d8b48022e 100644 --- a/lib/cask/link_checker.rb +++ b/lib/cask/link_checker.rb @@ -1,12 +1,18 @@ +require 'cask/checkable' + class Cask::LinkChecker - attr_accessor :cask, :errors, :response_status, :headers + attr_accessor :cask, :response_status, :headers + + include Cask::Checkable def initialize(cask, fetcher=Cask::Fetcher) @cask = cask - @errors = [] - @warnings = [] - @headers = {} @fetcher = fetcher + @headers = {} + end + + def summary_header + "link check result for #{cask}" end def run @@ -15,46 +21,6 @@ class Cask::LinkChecker _check_response_status end - def add_error(message) - @errors << message - end - - def add_warning(message) - @warnings << message - end - - def errors? - !@errors.empty? - end - - def warnings? - !@warnings.empty? - end - - def result - if errors? - "#{Tty.red}failed#{Tty.reset}" - elsif warnings? - "#{Tty.yellow}warning#{Tty.reset}" - else - "#{Tty.green}passed#{Tty.reset}" - end - end - - def summary - summary = ["audit for #{cask}: #{result}"] - - @errors.each do |error| - summary << " #{Tty.red}-#{Tty.reset} #{error}" - end - - @warnings.each do |warning| - summary << " #{Tty.yellow}-#{Tty.reset} #{warning}" - end - - summary.join("\n") - end - HTTP_RESPONSES = [ 'HTTP/1.0 200 OK', 'HTTP/1.1 200 OK'