File Coverage

blib/lib/WWW/REST.pm
Criterion Covered Total %
statement 71 75 94.6
branch 12 16 75.0
condition 2 3 66.6
subroutine 19 20 95.0
pod 3 3 100.0
total 107 117 91.4


line stmt bran cond sub pod time code
1 1     1   154334 use 5.006;
  1         3  
2             package WWW::REST;
3             # $WWW::REST::VERSION = '0.011';
4             $WWW::REST::VERSION = '0.022';
5 1     1   6 use strict;
  1         2  
  1         22  
6 1     1   5 use vars '$AUTOLOAD';
  1         2  
  1         57  
7              
8 1     1   498 use URI ();
  1         3851  
  1         20  
9 1     1   534 use LWP::UserAgent ();
  1         33916  
  1         22  
10 1     1   411 use HTTP::Request::Common ();
  1         1814  
  1         19  
11 1     1   416 use Class::Struct ();
  1         1523  
  1         24  
12              
13 1     1   7 use constant MEMBERS => qw(_uri _ua _res dispatch);
  1         2  
  1         59  
14 1     1   5 use constant METHODS => qw(options get head post put delete trace connect);
  1         1  
  1         443  
15              
16             Class::Struct::struct(map { $_ => '$' } +MEMBERS);
17              
18              
19             my $old_new = \&new;
20              
21             *new = sub {
22 6     6   1419 my $obj = shift;
23 6   66     18 my $class = ref($obj) || $obj;
24 6         8 my $uri = shift;
25 6         103 my $self = $old_new->($class);
26 6         176 $self->_uri( URI->new($uri) );
27 6 100       6221 $self->_uri( $self->_uri->abs($obj->_uri) ) if ref($obj);
28 6 100       1093 $self->_ua(
29             ref($obj) ? $obj->_ua : LWP::UserAgent->new( cookie_jar => {}, @_)
30             );
31 6 100       8765 $self->dispatch( $obj->dispatch ) if ref($obj);
32 6         54 return $self;
33             };
34              
35              
36             *url = \&new;
37              
38              
39             sub AUTOLOAD {
40 27     27   102 my $self = shift;
41 27         124 $AUTOLOAD =~ s/^.*:://;
42 27         76 foreach my $delegate (+MEMBERS) {
43 39         622 my $obj = $self->can($delegate)->($self);
44 39 100       397 my $code = UNIVERSAL::can($obj, $AUTOLOAD) or next;
45 27         53 unshift @_, $obj;
46 27         147 goto &$code;
47             }
48 0         0 die "No such method: $AUTOLOAD";
49             }
50              
51       0     sub DESTROY {}
52              
53             sub _request {
54 7     7   10 my $method = uc(+shift);
55             sub {
56 6     6   3781 my $self = shift;
57 6         39 $self->query_form(@_);
58 6         601 my $request = _simple_req( $method, $self->as_string );
59 6         112 my $res = $self->_ua->request($request);
60 6         1340806 $self->_res($res);
61 6 50       196 my $dispatch = $self->dispatch or return $self;
62 6         83 return $dispatch->($self);
63 7         203 };
64             }
65              
66             sub _simple_req
67             {
68 6     6   52 my($method, $url) = splice(@_, 0, 2);
69 6         40 my $req = HTTP::Request->new($method => $url);
70 6         1066 my($k, $v);
71 6         30 while (($k,$v) = splice(@_, 0, 2)) {
72 0 0       0 if (lc($k) eq 'content') {
73 0         0 $req->add_content($v);
74             } else {
75 0         0 $req->push_header($k, $v);
76             }
77             }
78 6         14 $req;
79             }
80              
81              
82             BEGIN {
83 1     1   3 foreach my $method (+METHODS) {
84 1     1   7 no strict 'refs';
  1         2  
  1         43  
85 8 100       21 *$method = _request($method) unless $method eq 'post';
86             }
87             }
88              
89             sub post {
90 1     1 1 599 my $self = shift;
91 1         5 my $request = HTTP::Request::Common::POST( $self->as_string, \@_ );
92 1         516 my $res = $self->_ua->request($request);
93 1         293722 $self->_res($res);
94 1 50       28 my $dispatch = $self->dispatch or return $self;
95 1         14 return $dispatch->($res);
96             }
97              
98              
99 1     1 1 399 sub parent { $_[0]->new('../') }
100              
101              
102             sub dir {
103 2     2 1 914 my @segs = $_[0]->path_segments;
104 2         109 pop @segs;
105 2         8 return $_[0]->new(join('/', @segs, ''));
106             }
107              
108              
109             1;
110              
111             __END__