1 package Catalyst::View::Component::SubInclude::HTTP;
4 use namespace::clean -except => 'meta';
6 use List::MoreUtils 'firstval';
10 $VERSION = eval $VERSION;
13 isa => 'Int', is => 'ro', default => 60,
17 isa => 'Str', is => 'ro', required => 1,
21 isa => 'Str', is => 'ro', required => 0,
25 isa => 'HashRef', is => 'ro', required => 0,
29 is => 'ro', lazy => 1, builder => '_build_user_agent',
32 sub _build_user_agent {
34 return LWP::UserAgent->new(
36 timeout => $self->ua_timeout,
40 sub generate_subinclude {
41 my ($self, $c, $path, $args) = @_;
42 my $error_msg_prefix = "SubInclude for $path failed: ";
43 my $base_uri = $self->base_uri || $c->req->base;
44 my $uri_map = $self->uri_map || { q{/} => $base_uri };
45 $base_uri = $uri_map->{ firstval { $path =~ s/^$_// } keys %$uri_map };
47 my $uri = URI->new(join(q{/}, $base_uri, $path));
48 my $req_method = q{_} . lc $self->http_method . '_request';
51 if ( $self->can($req_method) ) {
52 $response = $self->$req_method($uri, $args);
55 confess $self->http_method . ' not supported';
57 if ($response->is_success) {
58 return $response->content;
61 $c->log->info($error_msg_prefix . $response->status_line);
67 my ( $self, $uri, $args) = @_;
68 $uri->query_form($args);
69 return $self->user_agent->get($uri);
73 my ( $self, $uri, $args ) = @_;
74 return $self->user_agent->post($uri, $args);
77 __PACKAGE__->meta->make_immutable;
85 Catalyst::View::Component::SubInclude::HTTP - HTTP plugin for C::V::Component::SubInclude
91 package MyApp::View::TT;
94 extends 'Catalyst::View::TT';
95 with 'Catalyst::View::Component::SubInclude';
98 subinclude_plugin => 'HTTP',
101 base_uri => 'http://www.foo.com/bar',
106 Then, somewhere in your templates:
108 [% subinclude('/my/widget') %]
114 =head2 C<generate_subinclude( $c, $path, @params )>
118 L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>
122 Wallace Reis C<< <wreis@cpan.org> >>
126 Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
128 =head1 COPYRIGHT & LICENSE
130 Copyright (c) 2010 Wallace Reis.
132 This program is free software; you can redistribute it and/or modify it
133 under the same terms as Perl itself.