Class: Goldencobra::LinkChecker
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Goldencobra::LinkChecker
- Defined in:
- app/models/goldencobra/link_checker.rb
Class Method Summary collapse
-
.set_link_checker(article) ⇒ Object
get all links of a page and make a check for response status and time.
Class Method Details
.set_link_checker(article) ⇒ Object
get all links of a page and make a check for response status and time
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/models/goldencobra/link_checker.rb', line 9 def self.set_link_checker(article) @article = article links_to_check = [] status_for_links = {} #Sammle Links auf der Seite doc = Nokogiri::HTML(open(article.absolute_public_url)) #find all links and stylesheets doc.css('a,link').each do |link| if self.add_link_to_checklist(link, "href", article).present? links_to_check << {"link" => add_link_to_checklist(link, "href", article), "pos" => link.path} end end #find all images and javascripts doc.css('img,script').each do |link| if self.add_link_to_checklist(link,"src", article).present? links_to_check << {"link" => add_link_to_checklist(link,"src", article), "pos" => link.path} end end links_to_check = links_to_check.compact.delete_if{|a| a.blank?} #generate status_for_links links_to_check.each_with_index do |linkpos| status_for_links[linkpos["link"]] = {"position" => linkpos["pos"]} begin start = Time.now response = open(linkpos["link"]) status_for_links[linkpos["link"]]["response_code"] = response.status[0] status_for_links[linkpos["link"]]["response_time"] = Time.now - start rescue Exception => e status_for_links[linkpos["link"]]["response_code"] = "404" status_for_links[linkpos["link"]]["response_error"] = e.to_s end end #save status_for_links to DB status_for_links.each do |link_name, value| article.link_checks.destroy_all Goldencobra::LinkChecker.create(article_id: article.id, target_link: link_name, position: value["position"], response_code: value["response_code"], response_time: value["response_time"], response_error: value["response_error"] ) end return status_for_links end |