Client/Protocol/V1.pm

filter: Statementstmt 29.9% 29 / 97 Branchbran 0.0% 0 / 20 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 36.3% 8 / 22 Podpod 0.0% 0 / 5 totaltotal 25.1% 39 / 155 CC 15 SCAR 24.7
CC
15
Cov
25%
call
42.0
_get
20.0
_ua
12.0
CRAP 11.8
↑ Summary Client.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4 # vim: set ts=3 sw=3 tw=0:
5 # vim: set expandtab:
6  
7 package Rex::IO::Client::Protocol::V1;
8  
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 our $VERSION = '0.6'; # VERSION
13  
14 1 ▶use attributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Mojo::JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.6
Pod undocumented
22 1 my $that = shift;
23 1 ▶ my $proto = ref($that) || $that;
Condition33%1/3
ref $that || $that
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table33%1/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
24 1 my $self = {@_};
25  
26 1 bless( $self, $proto );
27  
28 1 ▶ $self->{endpoint} ||= "http://127.0.0.1:3000";
Condition50%1/2
$self->{"endpoint"} ||= "http://127.0.0.1:3000"
l!l
01
MC/DC0%0/1
$self->{"endpoint"} ||= "http://127.0.0.1:3000"
$self->{"endpoint"}
Truth table50%1/2
A
A= $self->{"endpoint"}
Aresult
11
00
29  
30 1 return $self;
31 }
32  
33 0 ▶sub endpoint : lvalue {
Subroutine endpoint: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my ($self) = @_;
35 0 $self->{endpoint};
36 }
37  
38 0 ▶sub get_plugins {
Subroutine get_plugins: not called CC=1 SCAR=6.9
Pod undocumented
39 0 my ($self) = @_;
40 0 $self->_get("/plugins")->res->json;
41 }
42  
43 0 ▶sub auth {
Subroutine auth: not called CC=2 SCAR=17.9
Pod undocumented
44 0 my ( $self, $user, $pass ) = @_;
45 my ( $proto, $endpoint ) =
46 0 ( $self->{endpoint} =~ m/^(https?:\/\/).*\@(.*)$/ );
47  
48 0 my $ref = $self->_ua->post( "$proto$user:$pass\@$endpoint/1.0/user/login",
49 json => {} )->res->json;
50  
51 0 ▶ if ( $ref->{ok} == Mojo::JSON->true ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"ok"} == "Mojo::JSON"->true) 0 0 0
52 0 return $ref->{data};
53 }
54  
55 0 return 0;
56 }
57  
58 0 ▶sub _ua {
Subroutine _ua: not called CC=3 SCAR=24.8
59 0 my ($self) = @_;
60 0 ▶ if ( $self->{ua} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ua"}) 0 0 0
61 0 return $self->{ua};
62 }
63  
64 0 my $ua = Mojo::UserAgent->new;
65  
66 0 ▶ if ( $self->{ssl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ssl"}) 0 0 0
67 0 $ua->ca( $self->{ssl}->{ca} );
68 0 $ua->cert( $self->{ssl}->{cert} );
69 0 $ua->key( $self->{ssl}->{key} );
70 }
71  
72 0 $self->{ua} = $ua;
73 0 return $self->{ua};
74 }
75  
76 0 ▶sub _get {
Subroutine _get: not called CC=4 SCAR=30.0
77 0 my ( $self, $url, $qry_string_ref ) = @_;
78  
79 0 ▶ if ( ref $qry_string_ref ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $qry_string_ref) { } 0 0 0
elsif ($qry_string_ref) { } 0 0 0
80 0 $url .= "?";
81 0 for my $key ( keys %{$qry_string_ref} ) {
82 0 $url .= "\&$key=$qry_string_ref->{$key}";
83 }
84 }
85 elsif ($qry_string_ref) {
86 0 $url .= "?$qry_string_ref";
87 }
88  
89 0 $self->_ua->get( $self->endpoint . $url );
90 }
91  
92 0 ▶sub _post {
Subroutine _post: not called CC=1 SCAR=6.9
93 0 my ( $self, $url, $post ) = @_;
94 0 $self->_ua->post( $self->endpoint . $url, json => $post );
95 }
96  
97 0 ▶sub _put {
Subroutine _put: not called CC=1 SCAR=6.9
98 0 my ( $self, $url, $put ) = @_;
99 0 $self->_ua->put( $self->endpoint . $url, $self->_json->encode($put) );
100 }
101  
102 0 ▶sub _list {
Subroutine _list: not called CC=1 SCAR=6.9
103 0 my ( $self, $url ) = @_;
104 0 my $tx = $self->_ua->build_tx( LIST => $self->endpoint . $url );
105 0 $self->_ua->start($tx);
106 }
107  
108 0 ▶sub _info {
Subroutine _info: not called CC=1 SCAR=6.9
109 0 my ( $self, $url ) = @_;
110 0 my $tx = $self->_ua->build_tx( INFO => $self->endpoint . $url );
111 0 $self->_ua->start($tx);
112 }
113  
114 0 ▶sub _run {
Subroutine _run: not called CC=2 SCAR=17.9
115 0 my ( $self, $url, $obj ) = @_;
116 0 ▶ $obj ||= {};
Condition0%0/2
$obj ||= {}
l!l
00
MC/DC0%0/1
$obj ||= {}
$obj
Truth table0%0/2
A
A= $obj
Aresult
11
00
117  
118 0 my $tx =
119 $self->_ua->build_tx( RUN => $self->endpoint . $url, json => $obj );
120 0 $self->_ua->start($tx);
121 }
122  
123 0 ▶sub _delete {
Subroutine _delete: not called CC=1 SCAR=6.9
124 0 my ( $self, $url ) = @_;
125 0 my $tx = $self->_ua->build_tx( DELETE => $self->endpoint . $url );
126 0 $self->_ua->start($tx);
127 }
128  
129 0 ▶sub _count {
Subroutine _count: not called CC=1 SCAR=6.9
130 0 my ( $self, $url ) = @_;
131 0 my $tx = $self->_ua->build_tx( COUNT => $self->endpoint . $url );
132 0 $self->_ua->start($tx);
133 }
134  
135 0 ▶sub _json {
Subroutine _json: not called CC=1 SCAR=6.9
136 0 my ($self) = @_;
137 0 return Mojo::JSON->new;
138 }
139  
140 ## new urls
141 # $VERB /1.0/plugin/resource/subres/id
142 # GET /1.0/hardware/server/5 -> get hardware id 5
143 # GET /1.0/hardware/server -> get hardware list
144  
145 0 ▶sub call {
Subroutine call: not called CC=6 SCAR=37.4
Pod undocumented
146 0 my ( $self, $verb, $version, $plugin, @param ) = @_;
147  
148 0 my $url = "/$version/$plugin";
149 0 my $ref;
150  
151 #for my $key (@param) {
152 0 ▶ while ( my $key = shift @param ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = shift @param) 0 0 0
153 0 my $value = shift @param;
154 0 ▶ if ( $key eq "ref" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "ref") 0 0 0
155 0 $ref = $value;
156 0 next;
157 }
158  
159 0 $url .= "/$key";
160  
161 0 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
162 0 $url .= "/$value";
163 }
164 }
165  
166 0 my $meth = "_\L$verb";
167  
168 0 my $ret;
169  
170 0 ▶ if ( ref $ref ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref) { } 0 0 0
elsif ($ref) { } 0 0 0
171 0 $ret = $self->$meth( $url, $ref );
172 }
173 elsif ($ref) {
174 0 $ret = $self->$meth( $url, $ref );
175 }
176 else {
177 0 $ret = $self->$meth($url);
178 }
179  
180 0 return decode_json( $ret->res->body );
181 }
182  
183 1 1;
↑ Summary Client.pm »