Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

added compress options for gzip #7730

Merged
merged 1 commit into from

5 participants

beyond Steve Klabnik Xavier Noria Andrew White Francesco Rodríguez
beyond

Zlib::GzipWriter can take two optional arguments, compression level and strategy. This fix is to pass these arguments.

http://www.ensta-paristech.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/zlib/rdoc/classes/Zlib/GzipWriter.html

Steve Klabnik
Collaborator

Do you think there's some way that a test could be written for this?

beyond

I have no good idea for testing wrapper object. If I could have a testing method, I write a code.

beyond

I wrote some test codes for gzip options.

Francesco Rodríguez frodsan commented on the diff
activesupport/lib/active_support/gzip.rb
@@ -25,9 +25,9 @@ def self.decompress(source)
end
# Compresses a string using gzip.
- def self.compress(source)
Francesco Rodríguez
frodsan added a note

Please, can you add docs about the new options? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Steve Klabnik
Collaborator

/cc @pixeltrix @fxn, what do you guys think about this?

Xavier Noria
Owner
fxn commented

Looks good to me, the signature of the method shown by RDoc will be enough documentation for those parameters I believe, since they match gzip jargon and are self-explanatory.

We would need a CHANGELOG entry though.

Andrew White
Owner

Ditto to what @fxn said - also squash the commits please.

Xavier Noria
Owner
fxn commented

Hi @beyond a CHANGELOG entry and commits squash is all we need here, if you find a minute we'll apply.

beyond

Hi @fxn I wrote a CHANGELOG and squashed commits.

Xavier Noria
Owner
fxn commented

Hi @beyond, could you please revise this pull request? I tried pulling the branch to do the merge myself, but some changes related to engines come down for some reason.

beyond beyond added compress options for gzip
added test for compress options of gzip

update changelog
d59a877
beyond

Hi @fxn This branch rebased the master branch and fixed conflicts.

Xavier Noria
Owner

Looking good, thanks very much!

Xavier Noria fxn merged commit 95dd699 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 10, 2013
  1. beyond

    added compress options for gzip

    beyond authored
    added test for compress options of gzip
    
    update changelog
This page is out of date. Refresh to see the latest.
5 activesupport/CHANGELOG.md
View
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* ActiveSupport::Gzip.compress allows two optional arguments for compression
+ level and strategy.
+
+ *Beyond*
+
* Modify `TimeWithZone#as_json` to include 3 decimal places of sub-second accuracy
by default, which is optional as per the ISO8601 spec, but extremely useful. Also
the default behaviour of Date#toJSON() in recent versions of Chrome, Safari and
4 activesupport/lib/active_support/gzip.rb
View
@@ -25,9 +25,9 @@ def self.decompress(source)
end
# Compresses a string using gzip.
- def self.compress(source)
Francesco Rodríguez
frodsan added a note

Please, can you add docs about the new options? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ def self.compress(source, level=Zlib::DEFAULT_COMPRESSION, strategy=Zlib::DEFAULT_STRATEGY)
output = Stream.new
- gz = Zlib::GzipWriter.new(output)
+ gz = Zlib::GzipWriter.new(output, level, strategy)
gz.write(source)
gz.close
output.string
18 activesupport/test/gzip_test.rb
View
@@ -4,6 +4,12 @@
class GzipTest < ActiveSupport::TestCase
def test_compress_should_decompress_to_the_same_value
assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World"))
+ assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", Zlib::NO_COMPRESSION))
+ assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", Zlib::BEST_SPEED))
+ assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", Zlib::BEST_COMPRESSION))
+ assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", nil, Zlib::FILTERED))
+ assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", nil, Zlib::HUFFMAN_ONLY))
+ assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World", nil, nil))
end
def test_compress_should_return_a_binary_string
@@ -12,4 +18,16 @@ def test_compress_should_return_a_binary_string
assert_equal Encoding.find('binary'), compressed.encoding
assert !compressed.blank?, "a compressed blank string should not be blank"
end
+
+ def test_compress_should_return_gzipped_string_by_compression_level
+ source_string = "Hello World"*100
+
+ gzipped_by_speed = ActiveSupport::Gzip.compress(source_string, Zlib::BEST_SPEED)
+ assert_equal 1, Zlib::GzipReader.new(StringIO.new(gzipped_by_speed)).level
+
+ gzipped_by_best_compression = ActiveSupport::Gzip.compress(source_string, Zlib::BEST_COMPRESSION)
+ assert_equal 9, Zlib::GzipReader.new(StringIO.new(gzipped_by_best_compression)).level
+
+ assert_equal true, (gzipped_by_best_compression.bytesize < gzipped_by_speed.bytesize)
+ end
end
Something went wrong with that request. Please try again.