You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

  1. #!/usr/bin/env ruby
  2. require 'httparty'
  3. require 'ruby-progressbar'
  4. require 'uri'
  5. allowed_codes = [200, 302, 403, 429]
  6. allowed_links = ["https://www.yelp.com/developers/documentation/v3"]
  7. args = ARGV
  8. filename = args[0]
  9. contents = File.open(filename, 'rb') { |f| f.read }
  10. raw_links = URI.extract(contents, ['http', 'https'])
  11. # Remove trailing ')' from entry URLs
  12. links = []
  13. raw_links.each do |link|
  14. if link.end_with?(')')
  15. links.push(link[0...-1])
  16. else
  17. links.push(link)
  18. end
  19. end
  20. fails = []
  21. # Fail on any duplicate elements
  22. dup = links.select{|element| links.count(element) > 1}
  23. if dup.uniq.length > 0
  24. dup.uniq.each do |e|
  25. fails.push("(DUP): #{e}")
  26. end
  27. end
  28. # Remove any duplicates from array
  29. links = links.uniq
  30. count = 0
  31. total = links.length
  32. progressbar = ProgressBar.create(:total => total)
  33. # GET each link and check for valid response code from allowed_codes
  34. links.each do |link|
  35. begin
  36. count += 1
  37. if allowed_links.include?(link)
  38. next
  39. end
  40. res = HTTParty.get(link, timeout: 10)
  41. if res.code.nil?
  42. fails.push("(NIL): #{link}")
  43. next
  44. end
  45. if !allowed_codes.include?(res.code)
  46. fails.push("(#{res.code}): #{link}")
  47. end
  48. rescue Net::ReadTimeout
  49. fails.push("(TMO): #{link}")
  50. rescue OpenSSL::SSL::SSLError
  51. fails.push("(SSL): #{link}")
  52. rescue SocketError
  53. fails.push("(SOK): #{link}")
  54. rescue Errno::ECONNREFUSED
  55. fails.push("(CON): #{link}")
  56. end
  57. progressbar.increment
  58. end
  59. puts "#{count}/#{total} links checked"
  60. if fails.length <= 0
  61. puts "all links valid"
  62. exit(0)
  63. else
  64. puts "-- RESULTS --"
  65. fails.sort!
  66. fails.each do |e|
  67. puts e
  68. end
  69. exit(1)
  70. end