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