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