Add HTTP plugin's doc
[catagits/Catalyst-View-Component-SubInclude.git] / lib / Catalyst / View / Component / SubInclude / HTTP.pm
CommitLineData
7b1b8afd 1package Catalyst::View::Component::SubInclude::HTTP;
2
3use Moose;
4use namespace::clean -except => 'meta';
5use LWP::UserAgent;
6use List::MoreUtils 'firstval';
7use URI;
8
9our $VERSION = '0.01';
10$VERSION = eval $VERSION;
11
b454d9bb 12has http_method => (
13 isa => 'Str', is => 'ro', default => 'GET',
7b1b8afd 14);
15
b454d9bb 16has ua_timeout => (
17 isa => 'Int', is => 'ro', default => 10,
7b1b8afd 18);
19
20has base_uri => (
21 isa => 'Str', is => 'ro', required => 0,
22);
23
24has uri_map => (
25 isa => 'HashRef', is => 'ro', required => 0,
26);
27
28has user_agent => (
29 is => 'ro', lazy => 1, builder => '_build_user_agent',
30);
31
32sub _build_user_agent {
33 my $self = shift;
34 return LWP::UserAgent->new(
35 agent => ref($self),
36 timeout => $self->ua_timeout,
37 );
38}
39
40sub 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 };
46 $base_uri =~ s{/$}{};
47 my $uri = URI->new(join(q{/}, $base_uri, $path));
48 my $req_method = q{_} . lc $self->http_method . '_request';
49
50 my $response;
51 if ( $self->can($req_method) ) {
52 $response = $self->$req_method($uri, $args);
53 }
54 else {
55 confess $self->http_method . ' not supported';
56 }
57 if ($response->is_success) {
58 return $response->content;
59 }
60 else {
61 $c->log->info($error_msg_prefix . $response->status_line);
62 return undef;
63 }
64}
65
66sub _get_request {
67 my ( $self, $uri, $args) = @_;
68 $uri->query_form($args);
69 return $self->user_agent->get($uri);
70}
71
72sub _post_request {
73 my ( $self, $uri, $args ) = @_;
74 return $self->user_agent->post($uri, $args);
75}
76
77__PACKAGE__->meta->make_immutable;
78
791;
80
81__END__
82
83=head1 NAME
84
85Catalyst::View::Component::SubInclude::HTTP - HTTP plugin for C::V::Component::SubInclude
86
87=head1 SYNOPSIS
88
89In your view class:
90
91 package MyApp::View::TT;
92 use Moose;
93
94 extends 'Catalyst::View::TT';
95 with 'Catalyst::View::Component::SubInclude';
96
97 __PACKAGE__->config(
b454d9bb 98 subinclude_plugin => 'HTTP::GET',
7b1b8afd 99 subinclude => {
b454d9bb 100 'HTTP::GET' => {
101 class => 'HTTP',
102 http_method => 'GET',
103 ua_timeout => '10',
104 uri_map => {
105 '/my/' => 'http://localhost:5000/',
106 },
107 },
108 'HTTP::POST' => {
109 class => 'HTTP',
110 http_method => 'POST',
111 ua_timeout => '10',
112 uri_map => {
113 '/foo/' => 'http://www.foo.com/',
114 },
7b1b8afd 115 },
116 },
117 );
118
119Then, somewhere in your templates:
120
121 [% subinclude('/my/widget') %]
b454d9bb 122 ...
123 [% subinclude_using('HTTP::POST', '/foo/path', { foo => 1 }) %]
7b1b8afd 124
125=head1 DESCRIPTION
126
b454d9bb 127C<Catalyst::View::Component::SubInclude::HTTP> does HTTP requests (currently
128using L<LWP::UserAgent>) and uses the responses to render subinclude contents.
129
130=head1 CONFIGURATION
131
132The configuration is passed in the C<subinclude> key based on your plugin name
133which can be arbitrary.
134
135=over
136
137=item class
138
139Required just in case your plugin name differs from C<HTTP>.
140
141=item http_method
142
143Accepts C<GET> and C<POST> as values. The default one is C<GET>.
144
145=item user_agent
146
147This lazily builds a L<LWP::UserAgent> obj, however you can pass a different
148user agent obj that implements the required API.
149
150=item ua_timeout
151
152User Agent's timeout config param. Defaults to 10 seconds.
153
154=item uri_map
155
156This expects a HashRef in order to map paths to different URLs.
157
158=item base_uri
159
160Used only if C<uri_map> is C<undef> and defaults to C<< $c->request->base >>.
161
162=back
163
7b1b8afd 164=head1 METHODS
165
b454d9bb 166=head2 C<generate_subinclude( $c, $path, $args )>
167
168Note that C<$path> should be the relative path.
7b1b8afd 169
170=head1 SEE ALSO
171
172L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>
173
174=head1 AUTHOR
175
176Wallace Reis C<< <wreis@cpan.org> >>
177
178=head1 SPONSORSHIP
179
180Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
181
182=head1 COPYRIGHT & LICENSE
183
184Copyright (c) 2010 Wallace Reis.
185
186This program is free software; you can redistribute it and/or modify it
187under the same terms as Perl itself.
188
189=cut