Add HTTP support
[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
12has ua_timeout => (
13 isa => 'Int', is => 'ro', default => 60,
14);
15
16has http_method => (
17 isa => 'Str', is => 'ro', required => 1,
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(
98 subinclude_plugin => 'HTTP',
99 subinclude => {
100 HTTP => {
101 base_uri => 'http://www.foo.com/bar',
102 },
103 },
104 );
105
106Then, somewhere in your templates:
107
108 [% subinclude('/my/widget') %]
109
110=head1 DESCRIPTION
111
112=head1 METHODS
113
114=head2 C<generate_subinclude( $c, $path, @params )>
115
116=head1 SEE ALSO
117
118L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude>
119
120=head1 AUTHOR
121
122Wallace Reis C<< <wreis@cpan.org> >>
123
124=head1 SPONSORSHIP
125
126Development sponsored by Ionzero LLC L<http://www.ionzero.com/>.
127
128=head1 COPYRIGHT & LICENSE
129
130Copyright (c) 2010 Wallace Reis.
131
132This program is free software; you can redistribute it and/or modify it
133under the same terms as Perl itself.
134
135=cut