[Bps-public-commit] plient branch, master, updated. 07f04bf24ac53422ac4c91426e91dff1c7a9baff

? sunnavy sunnavy at bestpractical.com
Mon Apr 26 17:57:18 EDT 2010


The branch, master has been updated
       via  07f04bf24ac53422ac4c91426e91dff1c7a9baff (commit)
       via  0aa095afdbeb4bf66d11531a6124d4c58f2c0aa5 (commit)
       via  c1a15f27c56df7b78bc57ec9c9cc8502fc41ecf1 (commit)
       via  080b2762ee60d0083c0dff69913f519591d80ac4 (commit)
       via  630444a824d3d954e99edae6eb8585cdd826306e (commit)
       via  7c3b6cfac55d217a5d1b1b94f7e48e94b69a3d36 (commit)
       via  e2a9cef4cc4bce9ab1c341b1e29ef914e636820a (commit)
       via  f8486d8c0c4ef457d67fc4aaeaf7638aa236aa7a (commit)
       via  ea73f33b272cc3787154b3f595fdf916735492f5 (commit)
      from  a5f72dce8d697634f11a3999678c4f8a20ad808d (commit)

Summary of changes:
 bin/plient            |   19 ++++++++++---------
 lib/Plient.pm         |   34 +++++++++++++++++++++++++++-------
 lib/Plient/Handler.pm |    6 +++++-
 t/01.plient.t         |   32 +++++++++++++++++++++++++++++++-
 4 files changed, 73 insertions(+), 18 deletions(-)

- Log -----------------------------------------------------------------
commit ea73f33b272cc3787154b3f595fdf916735492f5
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 27 01:06:15 2010 +0800

    shouldn't include the require failed handlers

diff --git a/lib/Plient.pm b/lib/Plient.pm
index 7ed0e1b..7f1c1f9 100644
--- a/lib/Plient.pm
+++ b/lib/Plient.pm
@@ -146,9 +146,8 @@ sub find_handlers {
             }
         }
     }
-    for my $hd (@hd) {
-        eval "require $hd" or warn "failed to require $hd";
-    }
+
+    @hd = grep { eval "require $hd" or warn "failed to require $hd" and 0 } @hd;
 
     @hd;
 }

commit f8486d8c0c4ef457d67fc4aaeaf7638aa236aa7a
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 27 01:29:14 2010 +0800

    use $found_handlers flag

diff --git a/lib/Plient.pm b/lib/Plient.pm
index 7f1c1f9..08c5a97 100644
--- a/lib/Plient.pm
+++ b/lib/Plient.pm
@@ -101,9 +101,10 @@ sub dispatch {
 }
 
 my @all_handlers;
+my $found_handlers;
 sub all_handlers {
-    return @all_handlers if @all_handlers;
-    @all_handlers = find_handlers();
+    return @all_handlers if $found_handlers;
+    @all_handlers = @all_handlers, find_handlers();
 }
 
 sub handlers {
@@ -133,6 +134,7 @@ sub handlers {
 }
 
 sub find_handlers {
+    $found_handlers = 1;
     my @hd;
     for my $inc (@INC) {
         my $handler_dir = catdir( $inc, 'Plient', 'Handler' );
@@ -147,7 +149,7 @@ sub find_handlers {
         }
     }
 
-    @hd = grep { eval "require $hd" or warn "failed to require $hd" and 0 } @hd;
+    @hd = grep { eval "require $_" or warn "failed to require $_" and 0 } @hd;
 
     @hd;
 }

commit e2a9cef4cc4bce9ab1c341b1e29ef914e636820a
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 27 01:31:26 2010 +0800

    return uniq handlers

diff --git a/lib/Plient.pm b/lib/Plient.pm
index 08c5a97..c37351d 100644
--- a/lib/Plient.pm
+++ b/lib/Plient.pm
@@ -105,6 +105,8 @@ my $found_handlers;
 sub all_handlers {
     return @all_handlers if $found_handlers;
     @all_handlers = @all_handlers, find_handlers();
+    my %hash = map { $_ => undef } @all_handlers;
+    @all_handlers = keys %hash;
 }
 
 sub handlers {

commit 7c3b6cfac55d217a5d1b1b94f7e48e94b69a3d36
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 27 01:34:49 2010 +0800

    comment update

diff --git a/lib/Plient/Handler.pm b/lib/Plient/Handler.pm
index cb9b8f3..6ee35a6 100644
--- a/lib/Plient/Handler.pm
+++ b/lib/Plient/Handler.pm
@@ -13,7 +13,7 @@ sub may_support_protocol {
     exists $class->all_protocol->{$protocol};
 }
 
-# you must call this when you init the handle, or nothing will return
+# call this after init(), or maybe nothing will return
 sub support_protocol {
     my $class    = shift;
     my $protocol = shift;

commit 630444a824d3d954e99edae6eb8585cdd826306e
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 27 01:42:31 2010 +0800

    add _add_handlers

diff --git a/lib/Plient.pm b/lib/Plient.pm
index c37351d..3a8c31e 100644
--- a/lib/Plient.pm
+++ b/lib/Plient.pm
@@ -109,6 +109,15 @@ sub all_handlers {
     @all_handlers = keys %hash;
 }
 
+# to include handlers not in @INC.
+sub _add_handlers {
+    shift if $_[0] && $_[0] eq __PACKAGE__;
+    push @all_handlers,
+      grep { $_->can('support_protocol') && $_->can('support_method') } @_;
+    my %hash = map { $_ => undef } @all_handlers;
+    @all_handlers = keys %hash;
+}
+
 sub handlers {
     shift if $_[0] && $_[0] eq __PACKAGE__;
     if ( my $protocol = lc shift ) {
diff --git a/t/01.plient.t b/t/01.plient.t
index fce273f..6b360ac 100644
--- a/t/01.plient.t
+++ b/t/01.plient.t
@@ -1,9 +1,38 @@
 use strict;
 use warnings;
 
-use Test::More tests => 2;
+use Test::More tests => 5;
 
 use_ok( 'Plient' );
 
 ok( Plient->available('File','GET'), 'supports File GET' );
 # ok( Plient->available('HTTP','GET'), 'supports HTTP GET' );
+
+
+{
+    # Fake1 doesn't look like a Plient Handler
+    package Plient::Handler::Fake1;
+    Plient->_add_handlers('Plient::Handler::Fake1');
+}
+
+ok( ! grep( { /Fake1/ } Plient->all_handlers ), 'not found Fake1 handler' );
+
+{
+    # make Fake2 looks like a Plient Handler
+    package Plient::Handler::Fake2;
+    sub support_protocol {};
+    sub support_method {};
+    Plient->_add_handlers('Plient::Handler::Fake2');
+}
+
+ok( grep( { /Fake2/ } Plient->all_handlers ), 'found Fake2 handler' );
+
+{
+    # Fake3 is based on 'Plient::Handler'
+    package Plient::Handler::Fake3;
+    use base 'Plient::Handler';
+    Plient->_add_handlers('Plient::Handler::Fake3');
+}
+
+ok( grep( { /Fake3/ } Plient->all_handlers ), 'found Fake3 handler' );
+

commit 080b2762ee60d0083c0dff69913f519591d80ac4
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 27 01:45:12 2010 +0800

    warn if the _add_handlers args are not handlers

diff --git a/lib/Plient.pm b/lib/Plient.pm
index 3a8c31e..86100b4 100644
--- a/lib/Plient.pm
+++ b/lib/Plient.pm
@@ -112,8 +112,18 @@ sub all_handlers {
 # to include handlers not in @INC.
 sub _add_handlers {
     shift if $_[0] && $_[0] eq __PACKAGE__;
-    push @all_handlers,
-      grep { $_->can('support_protocol') && $_->can('support_method') } @_;
+    for my $handler (@_) {
+        next unless $handler;
+        if ( $handler->can('support_protocol')
+            && $handler->can('support_method') )
+        {
+            push @all_handlers, $handler;
+        }
+        else {
+            warn "$handler doesn't look like a Plient handler";
+        }
+    }
+
     my %hash = map { $_ => undef } @all_handlers;
     @all_handlers = keys %hash;
 }

commit c1a15f27c56df7b78bc57ec9c9cc8502fc41ecf1
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 27 01:55:48 2010 +0800

    change all_handlers to a hash to erase the duplication

diff --git a/lib/Plient.pm b/lib/Plient.pm
index 86100b4..9a17042 100644
--- a/lib/Plient.pm
+++ b/lib/Plient.pm
@@ -100,13 +100,12 @@ sub dispatch {
     }
 }
 
-my @all_handlers;
+my %all_handlers;
 my $found_handlers;
 sub all_handlers {
-    return @all_handlers if $found_handlers;
-    @all_handlers = @all_handlers, find_handlers();
-    my %hash = map { $_ => undef } @all_handlers;
-    @all_handlers = keys %hash;
+    return keys %all_handlers if $found_handlers;
+    @all_handlers{keys %all_handlers, find_handlers()} = ();
+    keys %all_handlers;
 }
 
 # to include handlers not in @INC.
@@ -117,15 +116,14 @@ sub _add_handlers {
         if ( $handler->can('support_protocol')
             && $handler->can('support_method') )
         {
-            push @all_handlers, $handler;
+            $all_handlers{$handler} = ();
         }
         else {
             warn "$handler doesn't look like a Plient handler";
         }
     }
 
-    my %hash = map { $_ => undef } @all_handlers;
-    @all_handlers = keys %hash;
+    return keys %all_handlers;
 }
 
 sub handlers {
@@ -150,7 +148,7 @@ sub handlers {
     }
     else {
         # fallback to return all the handlers
-        return @all_handlers;
+        return keys %all_handlers;
     }
 }
 

commit 0aa095afdbeb4bf66d11531a6124d4c58f2c0aa5
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 27 01:56:36 2010 +0800

    add Plient::Handler->_add_to_plient

diff --git a/lib/Plient/Handler.pm b/lib/Plient/Handler.pm
index 6ee35a6..1fbf627 100644
--- a/lib/Plient/Handler.pm
+++ b/lib/Plient/Handler.pm
@@ -29,6 +29,10 @@ sub support_method {
     $class->method->{ $method };
 }
 
+sub _add_to_plient {
+    Plient->_add_handlers(shift);
+}
+
 
 1;
 
diff --git a/t/01.plient.t b/t/01.plient.t
index 6b360ac..0acad47 100644
--- a/t/01.plient.t
+++ b/t/01.plient.t
@@ -28,10 +28,11 @@ ok( ! grep( { /Fake1/ } Plient->all_handlers ), 'not found Fake1 handler' );
 ok( grep( { /Fake2/ } Plient->all_handlers ), 'found Fake2 handler' );
 
 {
+
     # Fake3 is based on 'Plient::Handler'
     package Plient::Handler::Fake3;
     use base 'Plient::Handler';
-    Plient->_add_handlers('Plient::Handler::Fake3');
+    __PACKAGE__->_add_to_plient();
 }
 
 ok( grep( { /Fake3/ } Plient->all_handlers ), 'found Fake3 handler' );

commit 07f04bf24ac53422ac4c91426e91dff1c7a9baff
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 27 05:56:46 2010 +0800

    show usage info

diff --git a/bin/plient b/bin/plient
index c3015ea..f0a0b62 100755
--- a/bin/plient
+++ b/bin/plient
@@ -6,25 +6,26 @@ use Getopt::Long;
 
 my %args;
 
-GetOptions( \%args, ) or die 'unknown option';
+GetOptions( \%args, 'help|h' ) or die 'unknown option';
 
 my $USAGE =<<EOF;
-Usage:
-    # fetch a page:
-    plient http://cpan.org/
-    plient get http://cpan.org/
-
-Arguments:
-
+USAGE: plient [METHOD] URL [ ... ]
+EXAMPLES:
+    plient http://cpan.org/                         # fetch http://cpan.org
+    plient get http://cpan.org/                     # ditto
 EOF
 
+if ( $args{help} || !$ARGV[0] ) {
+    print $USAGE;
+    exit;
+}
+
 my ( $method, @uri ) = @ARGV;
 if ( $method && $method =~ m{://} ) {
     push @uri, $method;
     $method = 'get';
 }
 
-use Plient;
 for my $uri ( @uri ) {
     print plient( $method, $uri );
 }

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list