Skip to content

Commit

Permalink
functionalize feedcheck_checks error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bmos committed Mar 6, 2024
1 parent a39057d commit 1e3e3b7
Showing 1 changed file with 38 additions and 42 deletions.
80 changes: 38 additions & 42 deletions tests/feedcheck_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,57 @@ class Status
PASSED = false
end

def check_avatar(avatar, av_dir, faraday)
return ['_ ', Status::PASSED] unless avatar
def check_status_and_location(response, url, error_message)
error = "#{error_message}Non successful status code #{response.status} when trying to access '#{url}' "
if response.status.to_i.between?(300, 399) && response.headers.key?('location')
return ["#{error}. Try using '#{response.headers['location']}' instead", Status::FAILED]
end

return check_url(avatar, faraday) if avatar.include? '//'
return ["✗ Avatar not found: #{av_dir}/#{avatar} ", Status::FAILED] unless File.file?("#{av_dir}/#{avatar}")
return [error, Status::FAILED] unless response.status.to_i == 200

['✓ ', Status::PASSED]
end

def check_url(url, faraday)
def request_data(connection, url, error_message)
connection.get(URI(url))
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError => e
["#{error_message}#{e.class} when trying to access '#{url}' ", Status::FAILED]
end

def parse_xml(feed, faraday)
error_message = '✗ '
response = request_data(faraday, feed, error_message)
return response if response.is_a? Array

begin
res = faraday.get(URI(url))
rescue Faraday::ConnectionFailed
return ["#{error_message}Connection Failure when trying to access '#{url}' ", Status::FAILED]
rescue Faraday::TimeoutError
return ["#{error_message}Connection Timeout waiting for '#{url}' ", Status::FAILED]
rescue Faraday::SSLError
return ["#{error_message}SSL Error when trying to access '#{url}' ", Status::FAILED]
end
xml_err = Nokogiri::XML(response.body).errors
return ["#{error_message}Unusable XML syntax: #{feed} #{xml_err} ", Status::FAILED] unless xml_err.empty?

error = "#{error_message}Non successful status code #{res.status} when trying to access '#{url}' "
if res.status.to_i.between?(300, 399) && res.headers.key?('location')
return ["#{error}. Try using '#{res.headers['location']}' instead", Status::FAILED]
end
['✓ ', Status::PASSED]
end

return [error, Status::FAILED] unless res.status.to_i == 200
def check_single_url(url, faraday)
error_message = '✗ '
res = request_data(faraday, url, error_message)
return res if res.is_a? Array

['✓ ', Status::PASSED]
check_status_and_location(res, url, error_message)
end

def check_urls(url_arr, faraday)
results = url_arr.map { |url| check_url(url, faraday) }
results = url_arr.map { |url| check_single_url(url, faraday) }

[results.map(&:first).join, results.any?(&:last)]
end

def parse_xml(feed, faraday)
result = ['✗ ', Status::FAILED]

begin
xml = faraday.get(URI(feed))
rescue Faraday::ConnectionFailed
return ["#{result.first}Connection Failure when trying to read XML from '#{feed}' ", Status::FAILED]
rescue Faraday::SSLError
return ["#{result.first}SSL Error when trying to read XML from '#{feed}' ", Status::FAILED]
end
def check_avatar(avatar, av_dir, faraday)
return ['_ ', Status::PASSED] unless avatar

xml_err = Nokogiri::XML(xml.body).errors
return ["#{result.first}Unusable XML syntax: #{feed}\n#{xml_err} ", Status::FAILED] unless xml_err.empty?
return check_url(avatar, faraday) if avatar.include? '//'
return ["✗ Avatar not found: #{av_dir}/#{avatar} ", Status::FAILED] unless File.file?("#{av_dir}/#{avatar}")

['✓ ', Status::PASSED]
end

def check_unused_files(av_dir, avatars)
hackergotchis = Dir.foreach(av_dir).select { |f| File.file?("#{av_dir}/#{f}") }
diff = (hackergotchis - avatars)

return ["There are unused files in #{av_dir}: #{diff.sort.join(', ')}", Status::FAILED] unless diff.empty?

[nil, Status::PASSED]
end

def accumulate_results(result, did_fail, new_result)
result << new_result.first

Expand All @@ -94,3 +81,12 @@ def check_source(key, section, faraday)

[[result.compact.join, did_fail], avatar]
end

def check_unused_files(av_dir, avatars)
hackergotchis = Dir.foreach(av_dir).select { |f| File.file?("#{av_dir}/#{f}") }
diff = (hackergotchis - avatars)

return ["There are unused files in #{av_dir}: #{diff.sort.join(', ')}", Status::FAILED] unless diff.empty?

[nil, Status::PASSED]
end

0 comments on commit 1e3e3b7

Please sign in to comment.