Commit | Line | Data |
7b1b8afd |
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 ua_timeout => ( |
13 | isa => 'Int', is => 'ro', default => 60, |
14 | ); |
15 | |
16 | has http_method => ( |
17 | isa => 'Str', is => 'ro', required => 1, |
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', |
99 | subinclude => { |
100 | HTTP => { |
101 | base_uri => 'http://www.foo.com/bar', |
102 | }, |
103 | }, |
104 | ); |
105 | |
106 | Then, 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 | |
118 | L<Catalyst::View::Component::SubInclude|Catalyst::View::Component::SubInclude> |
119 | |
120 | =head1 AUTHOR |
121 | |
122 | Wallace Reis C<< <wreis@cpan.org> >> |
123 | |
124 | =head1 SPONSORSHIP |
125 | |
126 | Development sponsored by Ionzero LLC L<http://www.ionzero.com/>. |
127 | |
128 | =head1 COPYRIGHT & LICENSE |
129 | |
130 | Copyright (c) 2010 Wallace Reis. |
131 | |
132 | This program is free software; you can redistribute it and/or modify it |
133 | under the same terms as Perl itself. |
134 | |
135 | =cut |