Merge remote branch 'wreis/master'
Tomas Doran [Tue, 14 Dec 2010 14:31:22 +0000 (14:31 +0000)]
* wreis/master:
  s/base_uri/base_url/g | Add duck_type check
  Update main doc
  Add Vladimir to contributors and update README
  Add me as a contributor
  Add HTTP plugin's doc
  No need of a second View in tests
  Add HTTP support
  Let the plugin name be arbitrary
  make config less gross and work with ConfigLoader
  Not class methods
  Add more tests

12 files changed:
Changes
Makefile.PL
README
lib/Catalyst/View/Component/SubInclude.pm
lib/Catalyst/View/Component/SubInclude/HTTP.pm [new file with mode: 0644]
t/00-load.t
t/01-app.t [moved from t/app.t with 100% similarity]
t/author/http.t [new file with mode: 0644]
t/lib/ESITest/Controller/Root.pm
t/lib/ESITest/View/TT.pm
t/lib/ESITest/root/http_cpan.tt [new file with mode: 0644]
t/lib/ESITest/root/http_github.tt [new file with mode: 0644]

diff --git a/Changes b/Changes
index eed5818..53ee257 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for Catalyst-View-Component-SubInclude
 
+0.10
+      - Add HTTP support (Wallace Reis)
+
 0.09  Thu 10 June 21:24:00 2010
       - Add SSI support (Vladimir Timofeev)
       - Additional documentation.
index fa12b05..0a3b6d9 100644 (file)
@@ -14,6 +14,9 @@ requires 'Moose::Role';
 requires 'MooseX::Types';
 requires 'Carp';
 requires 'namespace::clean';
+requires 'LWP::UserAgent';
+requires 'List::MoreUtils';
+requires 'URI';
 
 test_requires 'Test::More' => '0.88';
 test_requires 'Catalyst::View::TT';
diff --git a/README b/README
index 759fe36..3b66a9e 100644 (file)
--- a/README
+++ b/README
@@ -30,11 +30,7 @@ DESCRIPTION
 
     It's built in an extensible way so that you're free to use sub-requests,
     Varnish ESI (<http://www.catalystframework.org/calendar/2008/17>) or any
-    other sub-include plugin you might want to implement. An LWP plugin
-    seems useful and might be developed in the future. If you need to
-    address a resource by it's public path (i.e. the path part trailing
-    "http://example.com/myapp" then you will need to use
-    Catalyst::Plugin::SubRequest directly, and not this component.
+    other sub-include plugin you might want to implement.
 
 STASH FUNCTIONS
     This component does its magic by exporting a "subinclude" coderef entry
@@ -102,6 +98,10 @@ AUTHOR
 CONTRIBUTORS
     Tomas Doran (t0m) "<bobtfish@bobtfish.net".
 
+    Vladimir Timofeev, "<vovkasm at gmail.com>".
+
+    Wallace Reis (wreis) "<wreis@cpan.org>".
+
 SPONSORSHIP
     Development sponsored by Ionzero LLC <http://www.ionzero.com/>.
 
index 5d12012..8c23b02 100644 (file)
@@ -19,7 +19,7 @@ Version 0.09
 
 =cut
 
-our $VERSION = '0.09';
+our $VERSION = '0.10';
 $VERSION = eval $VERSION;
 
 =head1 SYNOPSIS
@@ -49,11 +49,7 @@ in your page.
 
 It's built in an extensible way so that you're free to use sub-requests,
 Varnish ESI (L<http://www.catalystframework.org/calendar/2008/17>) or any other
-sub-include plugin you might want to implement. An LWP plugin seems useful and
-might be developed in the future. If you need to address a resource by it's
-public path (i.e. the path part trailing C<http://example.com/myapp> then you
-will need to use L<Catalyst::Plugin::SubRequest> directly, and not this
-component.
+sub-include plugin you might want to implement.
 
 =head1 STASH FUNCTIONS
 
@@ -168,8 +164,6 @@ has _subinclude_plugin_class_instance_cache => (
 sub _subinclude_plugin_class_instance {
     my ($self, $plugin) = @_;
 
-    my $class = $plugin =~ /::/ ? $plugin : __PACKAGE__ . '::' . $plugin;
-
     my $cache = $self->_subinclude_plugin_class_instance_cache;
     return $cache->{$plugin} if exists $cache->{$plugin};
 
@@ -177,10 +171,16 @@ sub _subinclude_plugin_class_instance {
         $self->subinclude->{ALL}||{},
         $self->subinclude->{$plugin}||{}
     );
+    my $short_class = $plugin_config->{'class'} ?
+        delete $plugin_config->{'class'}
+        : $plugin;
+    my $class = $short_class =~ /::/ ?
+        $short_class
+        : __PACKAGE__ . '::' . $short_class;
 
     Class::MOP::load_class($class);
 
-    return $cache->{$plugin} = $class->new($plugin_config);
+    return $cache->{$class} = $class->new($plugin_config);
 }
 
 =head1 SEE ALSO
@@ -205,6 +205,10 @@ Nilson Santos Figueiredo Junior, C<< <nilsonsfj at cpan.org> >>
 
 Tomas Doran (t0m) C<< <bobtfish@bobtfish.net >>.
 
+Vladimir Timofeev, C<< <vovkasm at gmail.com> >>.
+
+Wallace Reis (wreis) C<< <wreis@cpan.org> >>.
+
 =head1 SPONSORSHIP
 
 Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
diff --git a/lib/Catalyst/View/Component/SubInclude/HTTP.pm b/lib/Catalyst/View/Component/SubInclude/HTTP.pm
new file mode 100644 (file)
index 0000000..33ac3a7
--- /dev/null
@@ -0,0 +1,191 @@
+package Catalyst::View::Component::SubInclude::HTTP;
+
+use Moose;
+use namespace::clean -except => 'meta';
+use Moose::Util::TypeConstraints;
+use LWP::UserAgent;
+use List::MoreUtils 'firstval';
+use URI;
+
+our $VERSION = '0.01';
+$VERSION = eval $VERSION;
+
+has http_method => (
+    isa => 'Str', is => 'ro', default => 'GET',
+);
+
+has ua_timeout => (
+    isa => 'Int', is => 'ro', default => 10,
+);
+
+has base_url => (
+    isa => 'Str', is => 'ro', required => 0,
+);
+
+has uri_map => (
+    isa => 'HashRef', is => 'ro', required => 0,
+);
+
+has user_agent => (
+    isa => duck_type([qw/get post/]), is => 'ro',
+    lazy => 1, builder => '_build_user_agent',
+);
+
+sub _build_user_agent {
+    my $self = shift;
+    return LWP::UserAgent->new(
+        agent => ref($self),
+        timeout => $self->ua_timeout,
+    );
+}
+
+sub generate_subinclude {
+    my ($self, $c, $path, $args) = @_;
+    my $error_msg_prefix = "SubInclude for $path failed: ";
+    my $base_url = $self->base_url || $c->req->base;
+    my $uri_map = $self->uri_map || { q{/} => $base_url };
+    $base_url = $uri_map->{ firstval { $path =~ s/^$_// } keys %$uri_map };
+    $base_url =~ s{/$}{};
+    my $uri = URI->new(join(q{/}, $base_url, $path));
+    my $req_method = q{_} . lc $self->http_method . '_request';
+
+    my $response;
+    if ( $self->can($req_method) ) {
+        $response = $self->$req_method($uri, $args);
+    }
+    else {
+        confess $self->http_method . ' not supported';
+    }
+    if ($response->is_success) {
+        return $response->content;
+    }
+    else {
+        $c->log->info($error_msg_prefix . $response->status_line);
+        return undef;
+    }
+}
+
+sub _get_request {
+    my ( $self, $uri, $args) = @_;
+    $uri->query_form($args);
+    return $self->user_agent->get($uri);
+}
+
+sub _post_request {
+    my ( $self, $uri, $args ) = @_;
+    return $self->user_agent->post($uri, $args);
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
+
+__END__
+
+=head1 NAME
+
+Catalyst::View::Component::SubInclude::HTTP - HTTP plugin for C::V::Component::SubInclude
+
+=head1 SYNOPSIS
+
+In your view class:
+
+    package MyApp::View::TT;
+    use Moose;
+
+    extends 'Catalyst::View::TT';
+    with 'Catalyst::View::Component::SubInclude';
+
+    __PACKAGE__->config(
+        subinclude_plugin => 'HTTP::GET',
+        subinclude => {
+            'HTTP::GET' => {
+                class => 'HTTP',
+                http_method => 'GET',
+                ua_timeout => '10',
+                uri_map => {
+                    '/my/' => 'http://localhost:5000/',
+                },
+            },
+            'HTTP::POST' => {
+                class => 'HTTP',
+                http_method => 'POST',
+                ua_timeout => '10',
+                uri_map => {
+                    '/foo/' => 'http://www.foo.com/',
+                },
+            },
+        },
+    );
+
+Then, somewhere in your templates:
+
+    [% subinclude('/my/widget') %]
+    ...
+    [% subinclude_using('HTTP::POST', '/foo/path', { foo => 1 }) %]
+
+=head1 DESCRIPTION
+
+C<Catalyst::View::Component::SubInclude::HTTP> does HTTP requests (currently
+using L<LWP::UserAgent>) and uses the responses to render subinclude contents.
+
+=head1 CONFIGURATION
+
+The configuration is passed in the C<subinclude> key based on your plugin name
+which can be arbitrary.
+
+=over
+
+=item class
+
+Required just in case your plugin name differs from C<HTTP>.
+
+=item http_method
+
+Accepts C<GET> and C<POST> as values. The default one is C<GET>.
+
+=item user_agent
+
+This lazily builds a L<LWP::UserAgent> obj, however you can pass a different
+user agent obj that implements the required API.
+
+=item ua_timeout
+
+User Agent's timeout config param. Defaults to 10 seconds.
+
+=item uri_map
+
+This expects a HashRef in order to map paths to different URLs.
+
+=item base_url
+
+Used only if C<uri_map> is C<undef> and defaults to C<< $c->request->base >>.
+
+=back
+
+=head1 METHODS
+
+=head2 C<generate_subinclude( $c, $path, $args )>
+
+Note that C<$path> should be the relative path.
+
+=head1 SEE ALSO
+
+L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>
+
+=head1 AUTHOR
+
+Wallace Reis C<< <wreis@cpan.org> >>
+
+=head1 SPONSORSHIP
+
+Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright (c) 2010 Wallace Reis.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
index d795774..759892a 100644 (file)
@@ -1,12 +1,16 @@
 #!perl\r
 \r
-use Test::More tests => 4;\r
+use Test::More;\r
 \r
 BEGIN {\r
        use_ok( 'Catalyst::View::Component::SubInclude' );\r
     use_ok( 'Catalyst::View::Component::SubInclude::SubRequest' );\r
     use_ok( 'Catalyst::View::Component::SubInclude::ESI' );\r
+    use_ok( 'Catalyst::View::Component::SubInclude::SSI' );\r
     use_ok( 'Catalyst::View::Component::SubInclude::Visit' );\r
+    use_ok( 'Catalyst::View::Component::SubInclude::HTTP' );\r
 }\r
 \r
 diag( "Testing Catalyst::View::Component::SubInclude $Catalyst::View::Component::SubInclude::VERSION, Perl $], $^X" );\r
+\r
+done_testing;\r
similarity index 100%
rename from t/app.t
rename to t/01-app.t
diff --git a/t/author/http.t b/t/author/http.t
new file mode 100644 (file)
index 0000000..4fd8c65
--- /dev/null
@@ -0,0 +1,16 @@
+use strict;
+use warnings;
+use FindBin qw/$Bin/;
+use lib "$Bin/../lib";
+use Test::More;
+use Catalyst::Test 'ESITest';
+
+my $res_content = get('/http_cpan');
+like $res_content, qr{CPAN Directory};
+like $res_content, qr{WREIS};
+
+$res_content = get('/http_github');
+like $res_content, qr{GitHub};
+like $res_content, qr{Wallace Reis};
+
+done_testing;
index 966a992..79a20c3 100644 (file)
@@ -6,13 +6,9 @@ use base 'Catalyst::Controller';
 
 __PACKAGE__->config->{namespace} = '';
 
-sub index :Path Args(0) {
-    my ( $self, $c ) = @_;
-}
+sub index :Path Args(0) {}
 
-sub base : Chained('/') PathPart('') CaptureArgs(0) {
-    my ( $self, $c ) = @_;
-}
+sub base : Chained('/') PathPart('') CaptureArgs(0) {}
 
 sub time_include : Chained('base') PathPart('time') Args(0) {
     my ( $self, $c ) = @_;
@@ -106,6 +102,10 @@ sub time_args_no_chained : Path('time_args_no_chained') Args {
     $c->stash->{template} = 'time_include.tt';
 }
 
+sub http_cpan : Chained('base') Args(0) {}
+
+sub http_github : Chained('base') Args(0) {}
+
 sub end : ActionClass('RenderView') {}
 
 1;
index 4f17bd6..bf9d33c 100644 (file)
@@ -7,6 +7,16 @@ with 'Catalyst::View::Component::SubInclude';
 __PACKAGE__->config(
     TEMPLATE_EXTENSION => '.tt',
     subinclude_plugin => 'Visit',
+    subinclude => {
+        'HTTP::GET' => {
+            class => 'HTTP',
+            http_method => 'GET',
+            uri_map => {
+                '/cpan/' => 'http://search.cpan.org/~',
+                '/github/' => 'http://github.com/',
+            },
+        },
+    },
 );
 
 1;
diff --git a/t/lib/ESITest/root/http_cpan.tt b/t/lib/ESITest/root/http_cpan.tt
new file mode 100644 (file)
index 0000000..4ec56ed
--- /dev/null
@@ -0,0 +1 @@
+[% subinclude_using('HTTP::GET', '/cpan/wreis') %]
diff --git a/t/lib/ESITest/root/http_github.tt b/t/lib/ESITest/root/http_github.tt
new file mode 100644 (file)
index 0000000..1627c51
--- /dev/null
@@ -0,0 +1 @@
+[% subinclude_using('HTTP::GET', '/github/wreis') %]