Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

75 рядки
1.8 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. if links.length <= 0
  21. puts "no links to check"
  22. exit(0)
  23. end
  24. fails = []
  25. # Fail on any duplicate elements
  26. dup = links.select{|element| links.count(element) > 1}
  27. if dup.uniq.length > 0
  28. dup.uniq.each do |e|
  29. fails.push("(DUP): #{e}")
  30. end
  31. end
  32. # Remove any duplicates from array
  33. links = links.uniq
  34. count = 0
  35. total = links.length
  36. progressbar = ProgressBar.create(:total => total)
  37. # GET each link and check for valid response code from allowed_codes
  38. links.each do |link|
  39. begin
  40. count += 1
  41. if allowed_links.include?(link)
  42. next
  43. end
  44. res = HTTParty.get(link, timeout: 10)
  45. if res.code.nil?
  46. fails.push("(NIL): #{link}")
  47. next
  48. end
  49. if !allowed_codes.include?(res.code)
  50. fails.push("(#{res.code}): #{link}")
  51. end
  52. rescue Net::ReadTimeout
  53. fails.push("(TMO): #{link}")
  54. rescue OpenSSL::SSL::SSLError
  55. fails.push("(SSL): #{link}")
  56. rescue SocketError
  57. fails.push("(SOK): #{link}")
  58. rescue Errno::ECONNREFUSED
  59. fails.push("(CON): #{link}")
  60. end
  61. progressbar.increment
  62. end
  63. puts "#{count}/#{total} links checked"
  64. if fails.length <= 0
  65. puts "all links valid"
  66. exit(0)
  67. else
  68. puts "-- RESULTS --"
  69. fails.sort!
  70. fails.each do |e|
  71. puts e
  72. end
  73. exit(1)
  74. end