blib/lib/Gerrit/REST.pm

filter: Statementstmt 30.2% 29 / 96 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 56.2% 9 / 16 Podpod 100% 4 / 4 totaltotal 24.8% 42 / 169 CC 27 SCAR 40.2
CC
27
Cov
20%
new
132.0
_error
72.0
_content
42.0
CRAP 55.6
↑ Summary
1 package Gerrit::REST;
2 {
3 1 $Gerrit::REST::VERSION = '0.011';
4 }
5 # ABSTRACT: Thin wrapper around Gerrit's REST API
6  
7 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Util qw/:check/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use REST::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 0 ▶sub new {
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
19 0 my ($class, $URL, $username, $password, $rest_client_config) = @_;
20  
21 0 ▶ $URL = URI->new($URL) if is_string($URL);
Branch: 1 branch
BranchTrueFalseTotal
if is_string($URL) 0 0 0
22 0 ▶ is_instance($URL, 'URI')
Branch: 1 branch
BranchTrueFalseTotal
is_instance($URL, "URI") or croak("Gerrit::REST::new: URL argument must be a string or a URI object.\n") 0 0 0
23 or croak __PACKAGE__ . "::new: URL argument must be a string or a URI object.\n";
24  
25 # If no password is set we try to lookup the credentials in the .netrc file
26 0 ▶ if (! defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password) 0 0 0
27 0 ▶ eval {require Net::Netrc}
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Net::Netrc } } or croak("Can't require Net::Netrc module. Please, specify the USERNAME and PASSWORD.\n") 0 0 0
28 or croak "Can't require Net::Netrc module. Please, specify the USERNAME and PASSWORD.\n";
29 0 ▶ if (my $machine = Net::Netrc->lookup($URL->host, $username)) { # $username may be undef
Branch: 1 branch
BranchTrueFalseTotal
if (my $machine = 'Net::Netrc'->lookup($URL->host, $username)) { } 0 0 0
30 0 $username = $machine->login;
31 0 $password = $machine->password;
32 } else {
33 0 croak "No credentials found in the .netrc file.\n";
34 }
35 }
36  
37 0 ▶ is_string($username)
Branch: 1 branch
BranchTrueFalseTotal
is_string($username) or croak("Gerrit::REST::new: USERNAME argument must be a string.\n") 0 0 0
38 or croak __PACKAGE__ . "::new: USERNAME argument must be a string.\n";
39  
40 0 ▶ is_string($password)
Branch: 1 branch
BranchTrueFalseTotal
is_string($password) or croak("Gerrit::REST::new: PASSWORD argument must be a string.\n") 0 0 0
41 or croak __PACKAGE__ . "::new: PASSWORD argument must be a string.\n";
42  
43 0 ▶ $rest_client_config = {} unless defined $rest_client_config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rest_client_config 0 0 0
44 0 ▶ is_hash_ref($rest_client_config)
Branch: 1 branch
BranchTrueFalseTotal
is_hash_ref($rest_client_config) or croak("Gerrit::REST::new: REST_CLIENT_CONFIG argument must be a hash-ref.\n") 0 0 0
45 or croak __PACKAGE__ . "::new: REST_CLIENT_CONFIG argument must be a hash-ref.\n";
46  
47 0 my $rest = REST::Client->new($rest_client_config);
48  
49 # Set default base URL
50 0 $rest->setHost($URL);
51  
52 # Follow redirects/authentication by default
53 0 $rest->setFollow(1);
54  
55 # Request compact JSON by default
56 0 $rest->addHeader('Accept' => 'application/json');
57  
58 # Configure UserAgent name and password authentication
59 0 for my $ua ($rest->getUseragent) {
60 0 $ua->agent(__PACKAGE__);
61 0 $ua->credentials($URL->host_port, 'Gerrit Code Review', $username, $password);
62 }
63  
64 0 return bless {
65 rest => $rest,
66 json => JSON->new->utf8->allow_nonref,
67 } => $class;
68 }
69  
70 0 ▶sub _error {
Subroutine _error: not called CC=8 SCAR=42.8
71 0 my ($self, $content, $type, $code) = @_;
72  
73 0 ▶ $type = 'text/plain' unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 0 0
74 0 ▶ $code = 500 unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 0 0
75  
76 0 my $msg = __PACKAGE__ . " Error[$code";
77  
78 0 ▶ if (eval {require HTTP::Status}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require HTTP::Status } }) 0 0 0
79 0 ▶ if (my $status = HTTP::Status::status_message($code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $status = &HTTP::Status::status_message($code)) 0 0 0
80 0 $msg .= " - $status";
81 }
82 }
83  
84 0 $msg .= "]:\n";
85  
86 0 ▶ if ($type =~ m:text/plain:i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ m[text/plain]i) { } 0 0 0
elsif ($type =~ m[text/html]i and eval { do { require HTML::TreeBuilder } }) { } 0 0 0
87 0 $msg .= $content;
88 0 ▶ } elsif ($type =~ m:text/html:i && eval {require HTML::TreeBuilder}) {
Condition0%0/3
$type =~ m[text/html]i and eval { do { require HTML::TreeBuilder } }
!ll&&!rl&&r
000
MC/DC0%0/2
$type =~ m[text/html]i and eval { do { require HTML::TreeBuilder } }
$type =~ m[text/html]i eval { do { require HTML::TreeBuilder } }
Truth table0%0/3
A and B
A= $type =~ m[text/html]iB= eval { do { require HTML::TreeBuilder } }
ABresult
0X0
100
111
89 0 $msg .= HTML::TreeBuilder->new_from_content($content)->as_text;
90 } else {
91 0 $msg .= "<unconvertable Content-Type: '$type'>";
92 };
93 0 $msg =~ s/\n*$/\n/s; # end message with a single newline
94 0 return $msg;
95 }
96  
97 0 ▶sub _content {
Subroutine _content: not called CC=6 SCAR=37.4
98 0 my ($self) = @_;
99  
100 0 my $rest = $self->{rest};
101 0 my $code = $rest->responseCode();
102 0 my $type = $rest->responseHeader('Content-Type');
103 0 my $content = $rest->responseContent();
104  
105 0 ▶ $code =~ /^2/
Branch: 1 branch
BranchTrueFalseTotal
$code =~ /^2/ or croak($self->_error($content, $type, $code)) 0 0 0
106 or croak $self->_error($content, $type, $code);
107  
108 0 ▶ if (! defined $type) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $type) { } 0 0 0
elsif ($type =~ m[^application/json]i) { } 0 0 0
elsif ($type =~ m[^text/plain]i) { } 0 0 0
109 0 return;
110 } elsif ($type =~ m:^application/json:i) {
111 0 ▶ if (substr($content, 0, 4) eq ")]}'") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($content, 0, 4) eq q<)]}'>) { } 0 0 0
112 0 return $self->{json}->decode(substr($content, 5));
113 } else {
114 0 croak $self->_error("Missing \")]}'\" prefix for JSON content:\n\n$content");
115 }
116 } elsif ($type =~ m:^text/plain:i) {
117 0 return $content;
118 } else {
119 0 croak $self->_error("I don't understand content with Content-Type '$type'");
120 }
121 }
122  
123 0 ▶sub GET {
Subroutine GET: not called CC=2 SCAR=17.9
Pod documented
124 0 my ($self, $resource) = @_;
125  
126 0 ▶ eval { $self->{rest}->GET("/a$resource") }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"rest"}->GET("/a$resource") } } or croak($self->_error("Error in GET(/a$resource): $@")) 0 0 0
127 or croak $self->_error("Error in GET(/a$resource): $@");
128  
129 0 return $self->_content();
130 }
131  
132 0 ▶sub DELETE {
Subroutine DELETE: not called CC=2 SCAR=17.9
133 0 my ($self, $resource) = @_;
134  
135 0 ▶ eval { $self->{rest}->DELETE("/a$resource") }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"rest"}->DELETE("/a$resource") } } or croak($self->_error("Error in DELETE(/a$resource): $@")) 0 0 0
136 or croak $self->_error("Error in DELETE(/a$resource): $@");
137  
138 0 return $self->_content();
139 }
140  
141 0 ▶sub PUT {
Subroutine PUT: not called CC=2 SCAR=17.9
Pod documented
142 0 my ($self, $resource, $value) = @_;
143  
144 0 ▶ eval { $self->{rest}->PUT(
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"rest"}->PUT("/a$resource", $self->{"json"}->encode($value), {"Content-Type", "application/json;charset=UTF-8"}) } } or croak($self->_error("Error in PUT(/a$resource, ...): $@")) 0 0 0
145 "/a$resource",
146 0 $self->{json}->encode($value),
147 {'Content-Type' => 'application/json;charset=UTF-8'},
148 ) }
149 or croak $self->_error("Error in PUT(/a$resource, ...): $@");
150  
151 0 return $self->_content();
152 }
153  
154 0 ▶sub POST {
Subroutine POST: not called CC=2 SCAR=17.9
Pod documented
155 0 my ($self, $resource, $value) = @_;
156  
157 0 ▶ eval { $self->{rest}->POST(
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{"rest"}->POST("/a$resource", $self->{"json"}->encode($value), {"Content-Type", "application/json;charset=UTF-8"}) } } or croak($self->_error("Error in POST(/a$resource, ...): $@")) 0 0 0
158 "/a$resource",
159 0 $self->{json}->encode($value),
160 {'Content-Type' => 'application/json;charset=UTF-8'},
161 ) }
162 or croak $self->_error("Error in POST(/a$resource, ...): $@");
163  
164 0 return $self->_content();
165 }
166  
167 1 1;
168  
169 __END__
↑ Summary