Permalink
Browse files

created new GzipRule and added it to simple_config.rb

  • Loading branch information...
1 parent bc06237 commit e8748ca0b279fb128ce0031e7229931c30a9bcc4 @chriskottom committed Sep 27, 2011
Showing with 37 additions and 0 deletions.
  1. +4 −0 examples/simple_config.rb
  2. +33 −0 lib/rslow/rules/gzip_rule.rb
View
4 examples/simple_config.rb
@@ -8,5 +8,9 @@
css: { maximum_allowed: 2, deduction: 4 },
css_image: { maximum_allowed: 6, deduction: 3 }
}
+ rule :Gzip, # creates another Rule
+ title: "Compress components with GZip",
+ weight: 8,
+ deduction: 11
end
end
View
33 lib/rslow/rules/gzip_rule.rb
@@ -0,0 +1,33 @@
+module RSlow
+ module Rules
+ class GzipRule < RSlow::Rule
+ GZIP_ENCODING = "gzip"
+ MAX_UNCOMPRESSED_SIZE = 500
+
+ def evaluate(resource)
+ score = compute_score(resource)
+
+ {
+ title: self[:title],
+ score: score,
+ grade: RSlow::Grading.for_score(score)
+ }
+ end
+
+ def compute_score(root_resource)
+ tested_resources = []
+ tested_resources << root_resource
+ tested_resources += root_resource.scripts
+ tested_resources += root_resource.stylesheets
+
+ uncompressed = tested_resources.select do |resource|
+ GZIP_ENCODING == resource.content_encoding ||
+ (resource.content_length &&
+ resource.content_length <= MAX_UNCOMPRESSED_SIZE)
+ end
+
+ RSlow::Rule::MAX_SCORE - (uncompressed.count * self[:deduction])
+ end
+ end
+ end
+end

0 comments on commit e8748ca

Please sign in to comment.