1 package Catalyst::View::Component::SubInclude::HTTP;
4 use namespace::clean -except => 'meta';
5 use Moose::Util::TypeConstraints;
7 use List::MoreUtils 'firstval';
10 our $VERSION = '0.01';
11 $VERSION = eval $VERSION;
14 isa => 'Str', is => 'ro', default => 'GET',
18 isa => 'Int', is => 'ro', default => 10,
22 isa => 'Str', is => 'ro', required => 0,
26 isa => 'HashRef', is => 'ro', required => 0,
30 isa => duck_type([qw/get post/]), is => 'ro',
31 lazy => 1, builder => '_build_user_agent',
34 sub _build_user_agent {
36 return LWP::UserAgent->new(
38 timeout => $self->ua_timeout,
42 sub generate_subinclude {
43 my ($self, $c, $path, $args) = @_;
44 my $error_msg_prefix = "SubInclude for $path failed: ";
45 my $base_url = $self->base_url || $c->req->base;
46 my $uri_map = $self->uri_map || { q{/} => $base_url };
47 $base_url = $uri_map->{ firstval { $path =~ s/^$_// } keys %$uri_map };
49 my $uri = URI->new(join(q{/}, $base_url, $path));
50 my $req_method = q{_} . lc $self->http_method . '_request';
53 if ( $self->can($req_method) ) {
54 $response = $self->$req_method($uri, $args);
57 confess $self->http_method . ' not supported';
59 if ($response->is_success) {
60 return $response->content;
63 $c->log->info($error_msg_prefix . $response->status_line);
69 my ( $self, $uri, $args) = @_;
70 $uri->query_form($args);
71 return $self->user_agent->get($uri);
75 my ( $self, $uri, $args ) = @_;
76 return $self->user_agent->post($uri, $args);
79 __PACKAGE__->meta->make_immutable;
87 Catalyst::View::Component::SubInclude::HTTP - HTTP plugin for C::V::Component::SubInclude
93 package MyApp::View::TT;
96 extends 'Catalyst::View::TT';
97 with 'Catalyst::View::Component::SubInclude';
100 subinclude_plugin => 'HTTP::GET',
104 http_method => 'GET',
107 '/my/' => 'http://localhost:5000/',
112 http_method => 'POST',
115 '/foo/' => 'http://www.foo.com/',
121 Then, somewhere in your templates:
123 [% subinclude('/my/widget') %]
125 [% subinclude_using('HTTP::POST', '/foo/path', { foo => 1 }) %]
129 C<Catalyst::View::Component::SubInclude::HTTP> does HTTP requests (currently
130 using L<LWP::UserAgent>) and uses the responses to render subinclude contents.
134 The configuration is passed in the C<subinclude> key based on your plugin name
135 which can be arbitrary.
141 Required just in case your plugin name differs from C<HTTP>.
145 Accepts C<GET> and C<POST> as values. The default one is C<GET>.
149 This lazily builds a L<LWP::UserAgent> obj, however you can pass a different
150 user agent obj that implements the required API.
154 User Agent's timeout config param. Defaults to 10 seconds.
158 This expects a HashRef in order to map paths to different URLs.
162 Used only if C<uri_map> is C<undef> and defaults to C<< $c->request->base >>.
168 =head2 C<generate_subinclude( $c, $path, $args )>
170 Note that C<$path> should be the relative path.
174 L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>
178 Wallace Reis C<< <wreis@cpan.org> >>
182 Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
184 =head1 COPYRIGHT & LICENSE
186 Copyright (c) 2010 Wallace Reis.
188 This program is free software; you can redistribute it and/or modify it
189 under the same terms as Perl itself.