Skip to content

Instantly share code, notes, and snippets.

/gz.diff Secret

Created September 10, 2014 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/05ab0470345759ee386e to your computer and use it in GitHub Desktop.
Save anonymous/05ab0470345759ee386e to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/ByteStream.pm b/lib/Mojo/ByteStream.pm
index c2995e8..53e13e4 100644
--- a/lib/Mojo/ByteStream.pm
+++ b/lib/Mojo/ByteStream.pm
@@ -3,6 +3,8 @@ use Mojo::Base -strict;
use overload '""' => sub { ${$_[0]} }, fallback => 1;
use Exporter 'import';
+use IO::Compress::Gzip;
+use IO::Uncompress::Gunzip;
use Mojo::Collection;
use Mojo::Util;
@@ -28,8 +30,11 @@ sub b { __PACKAGE__->new(@_) }
sub clone { $_[0]->new(${$_[0]}) }
-sub decode { shift->_delegate(\&Mojo::Util::decode, @_) }
-sub encode { shift->_delegate(\&Mojo::Util::encode, @_) }
+sub decode { shift->_charset(\&Mojo::Util::decode, @_) }
+sub encode { shift->_charset(\&Mojo::Util::encode, @_) }
+
+sub gunzip { shift->_gz(\&IO::Uncompress::Gunzip::gunzip) }
+sub gzip { shift->_gz(\&IO::Compress::Gzip::gzip, -Level => 9) }
sub new {
my $class = shift;
@@ -54,12 +59,19 @@ sub tap { shift->Mojo::Base::tap(@_) }
sub to_string { ${$_[0]} }
-sub _delegate {
+sub _charset {
my ($self, $sub) = (shift, shift);
$$self = $sub->(shift || 'UTF-8', $$self);
return $self;
}
+sub _gz {
+ my ($self, $sub, @args) = @_;
+ $sub->(\(my $input = $$self), \my $output, @args);
+ $$self = $output;
+ return $self;
+}
+
1;
=encoding utf8
@@ -160,6 +172,18 @@ Encode bytestream with L<Mojo::Util/"encode">, defaults to
$stream->trim->quote->encode->say;
+=head2 gunzip
+
+ $stream = $stream->gunzip;
+
+Uncompress bytestream with gzip.
+
+=head2 gzip
+
+ $stream = $stream->gzip;
+
+Compress bytestream with gzip.
+
=head2 hmac_sha1_sum
$stream = $stream->hmac_sha1_sum('passw0rd');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment