File Coverage

blib/lib/POE/Component/Client/HTTPDeferred.pm
Criterion Covered Total %
statement 27 28 96.4
branch 2 4 50.0
condition n/a
subroutine 9 9 100.0
pod 6 6 100.0
total 44 47 93.6


line stmt bran cond sub pod time code
1             package POE::Component::Client::HTTPDeferred;
2 2     2   2594 use Any::Moose;
  2         75373  
  2         12  
3              
4             our $VERSION = '0.02';
5              
6 2         16 use POE qw/
7             Component::Client::HTTP
8             Component::Client::HTTPDeferred::Deferred
9 2     2   3426 /;
  2         80436  
10              
11             has client_alias => (
12             is => 'rw',
13             isa => 'Str',
14             default => sub { 'ua' },
15             );
16              
17             has session => (
18             is => 'rw',
19             isa => 'POE::Session',
20             );
21              
22 2     2   360 no Any::Moose;
  2         3  
  2         9  
23              
24             =head1 NAME
25              
26             POE::Component::Client::HTTPDeferred - Yet another poco http client with twist like deferred interface.
27              
28             =head1 SYNOPSIS
29              
30             use POE qw/Component::Client::HTTPDeferred/;
31             use HTTP::Request::Common;
32            
33             POE::Session->create(
34             inline_states => {
35             _start => sub {
36             my $ua = POE::Component::Client::HTTPDeferred->new;
37             my $d = $ua->request( GET 'http://example.com/' );
38            
39             $d->addBoth(sub {
40             my $res = shift;
41            
42             if ($res->is_success) {
43             print $res->as_string;
44             }
45             else {
46             warn $res->status_line;
47             }
48            
49             $ua->shutdown;
50             });
51             },
52             },
53             );
54             POE::Kernel->run;
55              
56             =head1 DESCRIPTION
57              
58             POE::Component::Client::HTTPDeferred is a wrapper module to add twist (or MochiKit) like callback interface to POE::Component::Client::HTTP.
59              
60             To use this module, you can use code reference as response callback. So you don't have to create POE state for handling response.
61              
62             =head1 SEE ALSO
63              
64             L
65              
66             =head1 METHODS
67              
68             =head2 new
69              
70             Create POE::Component::Client::HTTPDeferred instance.
71              
72             my $ua = POE::Component::Client::HTTPDeferred->new;
73              
74             Once you call this, POE::Component::Client::HTTPDeferred will start POE::Session for own use.
75             So you need to call ->shutdown method to stop the session.
76              
77             =cut
78              
79             sub BUILD {
80 1     1 1 2 my $self = shift;
81              
82 3         25 $self->{session} = POE::Session->create(
83             object_states => [
84             $self => {
85 1         3 map { $_ => "poe_$_" } qw/_start request response/
86             },
87             ],
88             );
89             }
90              
91             =head2 request
92              
93             Send HTTP request and return Deferred object (L).
94              
95             my $d = $ua->request($request);
96              
97             This $request argument should be HTTP::Request object.
98              
99             =cut
100              
101             sub request {
102 1     1 1 4 my ($self, $req) = @_;
103              
104 1         28 my $d = $req->{_deferred} = POE::Component::Client::HTTPDeferred::Deferred->new(
105             request => $req,
106             client_alias => $self->client_alias,
107             );
108              
109 1         116 $poe_kernel->post( $self->session->ID => request => $req );
110              
111 1         137 $d;
112             }
113              
114             =head2 shutdown
115              
116             Shutdown POE::Component::Client::HTTPDeferred session.
117              
118             =cut
119              
120             sub shutdown {
121 1     1 1 9 my $self = shift;
122 1         10 $poe_kernel->post( $self->client_alias => 'shutdown' );
123             }
124              
125             =head1 POE METHODS
126              
127             Internal POE methods.
128              
129             =head2 poe__start
130              
131             =cut
132              
133             sub poe__start {
134 1     1 1 177 my ($self, $kernel) = @_[OBJECT, KERNEL];
135 1         11 POE::Component::Client::HTTP->spawn( Alias => $self->client_alias );
136             }
137              
138             =head2 poe_request
139              
140             =cut
141              
142             sub poe_request {
143 1     1 1 502 my ($self, $kernel, $req) = @_[OBJECT, KERNEL, ARG0];
144              
145 1         6 $kernel->post( $self->client_alias, 'request', 'response', $req );
146             }
147              
148             =head2 poe_response
149              
150             =cut
151              
152             sub poe_response {
153 1     1 1 228255 my ($self, $kernel) = @_[OBJECT, KERNEL];
154 1         7 my ($req, $res) = ($_[ARG0]->[0], $_[ARG1]->[0]);
155              
156 1 50       14 my $d = delete $req->{_deferred} or Carp::confess 'deferred object not found';
157              
158 1 50       14 if ($res->is_success) {
159 0         0 $d->callback($res);
160             }
161             else {
162 1         55 $d->errback($res);
163             }
164             }
165              
166             =head1 AUTHOR
167              
168             Daisuke Murase
169              
170             =head1 COPYRIGHT AND LICENSE
171              
172             Copyright (c) 2008-2009 by KAYAC Inc.
173              
174             This program is free software; you can redistribute
175             it and/or modify it under the same terms as Perl itself.
176              
177             The full text of the license can be found in the
178             LICENSE file included with this module.
179              
180             =cut
181              
182             __PACKAGE__->meta->make_immutable;