Class: SQT::Sqt

Inherits:
Object
  • Object
show all
Defined in:
lib/sqt.rb

Class Method Summary collapse

Class Method Details

.buildResult(uri, file) ⇒ Object

Construit l’objet qui sera affiché, en fonction de son uri et de son contenu



18
19
20
21
22
23
24
25
# File 'lib/sqt.rb', line 18

def self.buildResult(uri, file)
  fileProperties = {}
  fileProperties[:uri] = uri
  fileProperties[:totalLength] = file.bytesize
  fileProperties[:jsAndCssLength] = getJsAndCssLength file
  fileProperties[:sqi] = sarbotteQuality(fileProperties[:jsAndCssLength], fileProperties[:totalLength])
  fileProperties
end

.getJsAndCssLength(file) ⇒ Object

Récupère le nombre de caractères dans les balises script et style d’un fichier



28
29
30
31
32
# File 'lib/sqt.rb', line 28

def self.getJsAndCssLength(file)
  xmlFile = Nokogiri::HTML(file)
  jsLength = xmlFile.search('//script').reduce(0) { |total, script| total + script.text.bytesize }
  xmlFile.search('//style').reduce(jsLength) { |total, style| total + style.text.bytesize }
end

.sarbotteCurlWithDepth(url, depth, result) ⇒ Object

Curl with depth



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sqt.rb', line 40

def self.sarbotteCurlWithDepth(url, depth, result)
  urlUrl = URI(url)
  http = Curl.get(url)
  file = http.body_str
  xmlFile = Nokogiri::HTML(file)
  allowedScheme = ['http', 'https']
  if !(result.any? {|sqr| URI(sqr[:uri]).path.gsub(/\/$/, '') == urlUrl.path.gsub(/\/$/, '') })
    result.push Sqt.buildResult(url, file)
  end
  if depth > 0
    xmlFile.search('//a').each do |foundLink|
      begin
        if foundLink['href'] =~ /^\//
          toVisit = url.gsub(/\/$/, '') + foundLink['href']
        else
          toVisit = foundLink['href']
        end
        puts toVisit
        if !(toVisit =~ /^\s*$/)
          toVisitUrl = URI(toVisit)
          result.any? do |sqr| 
            URI(sqr[:uri]).path.gsub(/\/$/, '') == toVisitUrl.path.gsub(/\/$/, '')
          end
          if urlUrl.host == toVisitUrl.host && allowedScheme.include?(toVisitUrl.scheme) && !result.any? { |sqr| URI(sqr[:uri]).path.gsub(/\/$/, '') == toVisitUrl.path.gsub(/\/$/, '') }
            d = sarbotteCurlWithDepth(toVisit, depth - 1, result)
            result = d if !d.nil?
          end
        end
      rescue
        puts "exception"
      end
    end
  end

  result
  
end

.sarbotteQuality(jsAndCss, total) ⇒ Object

Définit le Sarbotte Quality Index d’un fichier



35
36
37
# File 'lib/sqt.rb', line 35

def self.sarbotteQuality(jsAndCss, total)
  (1 - jsAndCss.to_f/total.to_f) * 100
end