File Coverage

blib/lib/WWW/OpenResty.pm
Criterion Covered Total %
statement 64 80 80.0
branch 18 32 56.2
condition 5 16 31.2
subroutine 11 15 73.3
pod 5 8 62.5
total 103 151 68.2


line stmt bran cond sub pod time code
1             package WWW::OpenResty;
2              
3 1     1   8 use strict;
  1         2  
  1         41  
4 1     1   7 use warnings;
  1         2  
  1         39  
5              
6             #use Smart::Comments;
7 1     1   6 use Carp qw(croak);
  1         2  
  1         63  
8 1     1   1535 use Params::Util qw( _HASH0 );
  1         4952  
  1         78  
9 1     1   1762 use LWP::UserAgent;
  1         96511  
  1         36  
10 1     1   12 use Data::Dumper;
  1         2  
  1         82  
11 1     1   7 use Digest::MD5 qw(md5_hex);
  1         3  
  1         1063  
12              
13             our $VERSION = '0.09';
14              
15             sub new {
16             ### @_
17 5 50   5 0 11560 my $class = ref $_[0] ? ref shift : shift;
18 5 50       39 my $params = _HASH0(shift @_) or croak "Invalid params";
19             ### $params
20 5 50       28 my $server = delete $params->{server} or
21             croak "No server specified.";
22 5 50       45 if ($server !~ m{^\w+://}) {
23 0         0 $server = "http://$server";
24             }
25 5         18 my $ignore_dup_error = delete $params->{ignore_dup_error};
26 5         13 my $timer = delete $params->{timer};
27 5         10 my $retries = delete $params->{retries};
28 5         45 my $ua = LWP::UserAgent->new;
29 5         7173 $ua->cookie_jar({ file => "cookies.txt" });
30 5         19059 bless {
31             server => $server,
32             ua => $ua,
33             timer => $timer,
34             retries => $retries,
35             ignore_dup_error => $ignore_dup_error,
36             }, $class;
37             }
38              
39             sub content_type {
40 0     0 0 0 $_[0]->{content_type} = $_[1];
41             }
42              
43             sub login {
44 3     3 1 27 my ($self, $user, $password) = @_;
45 3 50       12 if ($password) {
46 0         0 $password = md5_hex($password);
47 0         0 return $self->get("/=/login/$user/$password?_use_cookie=1");
48             }
49 3         13 $self->{_user} = $user;
50             }
51              
52             sub get {
53 5     5 1 1009 my $self = shift;
54 5         26 $self->request(undef, 'GET', @_);
55             }
56              
57             sub post {
58 0     0 1 0 my $self = shift;
59 0         0 my $content = pop;
60 0         0 $self->request($content, 'POST', @_);
61             }
62              
63             sub put {
64 0     0 1 0 my $self = shift;
65 0         0 my $content = pop;
66 0         0 $self->request($content, 'PUT', @_);
67             }
68              
69             sub delete {
70 0     0 1 0 my $self = shift;
71 0         0 $self->request(undef, 'DELETE', @_);
72             }
73              
74             sub request {
75 5     5 0 11 my ($self, $content, $method, $url, $params) = @_;
76 5 50 33     25 !defined $params or _HASH0($params) or
77             croak "Params must be a hash: ", Dumper($params);
78 5 50       16 !ref $url or croak "URL is of the wrong type: ", Dumper($url);
79 5   50     23 $params ||= {};
80 5 100       40 if ($self->{_user}) {
81 3   33     21 $params->{_user} ||= $self->{_user};
82             }
83 5         9 my @params;
84 5         28 while (my ($key, $val) = each %$params) {
85 3         18 push @params, "$key=$val";
86             }
87 5 100       32 if ($url =~ /\?$/) {
    100          
88 1         3 $url .= join '&', @params;
89             } elsif ($url =~ /\?/) {
90 1         6 $url .= "&" . join '&', @params;
91             } else {
92 3         11 $url .= "?" . join '&', @params;
93             }
94 5         12 my $type = $self->{content_type};
95 5   50     24 $type ||= 'text/plain';
96 5 50       17 if ($url !~ /^http:\/\//) {
97 5         14 $url = $self->{server} . $url;
98             }
99 5         48 my $req = HTTP::Request->new($method);
100 5         305 $req->header('Content-Type' => $type);
101 5         297 $req->header('Accept', '*/*');
102 5         206 $req->url($url);
103 5 50 33     16333 if ($content) {
    50          
104 0 0 0     0 if ($method eq 'GET' or $method eq 'HEAD') {
105 0         0 croak "HTTP 1.0/1.1 $method request should not have content: $content";
106             }
107              
108 0         0 $req->content($content);
109             } elsif ($method eq 'POST' or $method eq 'PUT') {
110 0         0 $req->header('Content-Length' => 0);
111             }
112 5         13 my $timer = $self->{timer};
113 5         10 my $ua = $self->{ua};
114 5 50       18 $timer->start($method) if $timer;
115 5         26 my $res = $ua->request($req);
116 5 50       86393 $timer->stop($method) if $timer;
117 5         36 return $res;
118             }
119              
120              
121             1;
122             __END__