Browse Source

clean up and pull out link checker / audit common code

Paul Hinze 12 years ago
parent
commit
93b63bd583
  1. 53
      lib/cask/audit.rb
  2. 51
      lib/cask/checkable.rb
  3. 54
      lib/cask/link_checker.rb

53
lib/cask/audit.rb

@ -1,12 +1,12 @@
require 'cask/checkable'
class Cask::Audit 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 @cask = cask
@errors = []
@warnings = []
@headers = {}
@fetcher = fetcher
end end
def run! def run!
@ -14,44 +14,8 @@ class Cask::Audit
return if errors? return if errors?
end end
def add_error(message) def summary_header
@errors << message "audit for #{cask}"
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 end
def _check_required_fields def _check_required_fields
@ -59,5 +23,4 @@ class Cask::Audit
add_error "version is required" unless cask.version add_error "version is required" unless cask.version
add_error "homepage is required" unless cask.homepage add_error "homepage is required" unless cask.homepage
end end
end end

51
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

54
lib/cask/link_checker.rb

@ -1,12 +1,18 @@
require 'cask/checkable'
class Cask::LinkChecker 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) def initialize(cask, fetcher=Cask::Fetcher)
@cask = cask @cask = cask
@errors = []
@warnings = []
@headers = {}
@fetcher = fetcher @fetcher = fetcher
@headers = {}
end
def summary_header
"link check result for #{cask}"
end end
def run def run
@ -15,46 +21,6 @@ class Cask::LinkChecker
_check_response_status _check_response_status
end 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_RESPONSES = [
'HTTP/1.0 200 OK', 'HTTP/1.0 200 OK',
'HTTP/1.1 200 OK' 'HTTP/1.1 200 OK'

Loading…
Cancel
Save