blib/lib/Net/NodeTransformator.pm

filter: Statementstmt 33.6% 38 / 113 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 41.3% 12 / 29 Podpod 100% 10 / 10 totaltotal 25.9% 60 / 231 SCAR 60.7
CC
29
Cov
22%
standalone
182.0
new
42.0
__ANON__
30.0
CRAP 434.4
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::NodeTransformator;
5  
6 # ABSTRACT: interface to node transformator
7  
8 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use AnyEvent::Proc 0.101;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Env::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use POSIX qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use CBOR::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 our $VERSION = '0.104'; # VERSION
20  
21 sub new {
22 0 ▶ my ( $class, $hostport ) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
23 0 ▶ if ( $hostport !~ m{:} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hostport =~ /:/) 0 0 0
24 0 ▶ if ( $hostport =~ m{^\d+$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostport =~ /^\d+$/) { } 0 0 0
25 0 $hostport = "localhost:$hostport";
26 }
27 else {
28 0 $hostport = "unix/:$hostport";
29 }
30 }
31 0 my ( $host, $port ) = parse_hostport($hostport);
32 0 ▶ if ( $host eq 'unix/' and $port !~ m{^/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host eq "unix/" and not $port =~ m[^/]) 0 0 0
Condition0%0/3
$host eq "unix/" and not $port =~ m[^/]
!ll&&!rl&&r
000
MC/DC0%0/2
$host eq "unix/" and not $port =~ m[^/]
$host eq "unix/" not $port =~ m[^/]
Truth table0%0/3
A and not(B)
A= $host eq "unix/"B= not $port =~ m[^/]
ABresult
0X0
100
111
33 0 $port = getcwd . '/' . $port;
34 }
35 bless {
36 0 ▶ host => $host,
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
37 port => $port,
38 } => ref $class
39 || $class;
40 }
41  
42 sub standalone {
43 0 ▶ my $class = shift;
Subroutine standalone: not called CC=13 SCAR=52.0
Pod documented
44  
45 0 my %options;
46 0 ▶ if ( @_ > 1 and !( @_ % 2 ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ > 1 and not @_ % 2) { } 0 0 0
elsif (@_ == 1) { } 0 0 0
elsif (@_) { } 0 0 0
Condition0%0/3
@_ > 1 and not @_ % 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 1 and not @_ % 2
@_ > 1 not @_ % 2
Truth table0%0/3
A and not(B)
A= @_ > 1B= not @_ % 2
ABresult
0X0
100
111
47 0 %options = @_;
48 }
49 elsif ( @_ == 1 ) {
50 0 $options{connect} = shift;
51 }
52 elsif (@_) {
53 0 croak "wrong paramater given for $class->standalone";
54 }
55  
56 0 ▶ $options{bin} ||= 'transformator';
Condition0%0/2
$options{"bin"} ||= "transformator"
l!l
00
MC/DC0%0/1
$options{"bin"} ||= "transformator"
$options{"bin"}
Truth table0%0/2
A
A= $options{"bin"}
Aresult
11
00
57 my $path =
58 $options{bin} =~ m{/}
59 ? $options{bin}
60 0 ▶ : ( Env::Path->PATH->Whence( $options{bin} ) )[0];
Branch: 1 branch
BranchTrueFalseTotal
$options{'bin'} =~ m[/] ? : 0 0 0
61  
62 0 ▶ unless ($path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 0 0 0
63 0 croak "binary " . $options{bin} . " not found";
64 }
65  
66 0 ▶ unless ( $options{connect} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"connect"}) 0 0 0
67 0 my $tmpdir = tempdir( CLEANUP => 1 );
68 0 $options{connect} = "$tmpdir/~sock";
69 }
70  
71 0 my $errstr = '';
72  
73 0 ▶ my $cv = $options{cv} // AE::cv;
Condition0%0/3
$options{"cv"} // &AE::cv()
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"cv"} // &AE::cv()
$options{"cv"} &AE::cv()
Truth table0%0/3
A // B
A= $options{"cv"}B= &AE::cv()
ABresult
1X1
011
000
74  
75 my $server = AnyEvent::Proc->new(
76 bin => $path,
77 args => [ $options{connect} ],
78 rtimeout => 10,
79 errstr => \$errstr,
80 on_rtimeout => sub {
81 shift->fire_and_kill(
82 10,
83 sub {
84 0 ▶ $cv->croak( 'timeout (' . $errstr . ')' );
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 }
86 0 ▶ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 },
88 0 );
89  
90 $server->readlines_cb(
91 sub {
92 0 ▶ if ( shift =~ m{server bound} ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ((shift()) =~ /server bound/) 0 0 0
93 0 $server->stop_rtimeout;
94 0 my $client = $class->new( $options{connect} );
95 0 $client->{_server} = $server;
96 0 $cv->send($client);
97 }
98 }
99 0 );
100  
101 0 ▶ if ( $options{cb} or $options{cv} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'cb'} or $options{'cv'}) { } 0 0 0
Condition0%0/3
$options{"cb"} or $options{"cv"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"cb"} or $options{"cv"}
$options{"cb"} $options{"cv"}
Truth table0%0/3
A or B
A= $options{"cb"}B= $options{"cv"}
ABresult
1X1
011
000
102 0 ▶ $cv->cb( $options{cb} ) if $options{cb};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"cb"} 0 0 0
103 0 return $cv;
104 }
105 else {
106 0 return $cv->recv;
107 }
108 }
109  
110 sub cleanup {
111 0 ▶ my ($self) = @_;
Subroutine cleanup: not called CC=2 SCAR=17.9
Pod documented
112 0 ▶ if ( exists $self->{_server} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_server'}) { } 0 0 0
113 0 my $server = delete $self->{_server};
114 0 $server->fire_and_kill(10);
115 }
116 else {
117 0 AE::log note =>
118 "$self->cleanup called when no standalone server active";
119 }
120  
121 }
122  
123 sub transform_cv {
124 0 ▶ my ( $self, %options ) = @_;
Subroutine transform_cv: not called CC=4 SCAR=30.0
Pod documented
125  
126 0 ▶ if ( $options{on_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"on_error"}) 0 0 0
127 0 confess
128 "on_error option is deprecated. the returned condvar will now croak on receive if there is an error.";
129 }
130  
131 0 ▶ my $cv = $options{cv} || AE::cv;
Condition0%0/3
$options{'cv'} || &AE::cv()
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{'cv'} || &AE::cv()
$options{'cv'} &AE::cv()
Truth table0%0/3
A || B
A= $options{'cv'}B= &AE::cv()
ABresult
1X1
011
000
132  
133 0 my $host = $self->{host};
134 0 my $port = $self->{port};
135  
136 tcp_connect(
137 $host, $port,
138 sub {
139 0 ▶ return $cv->croak("Connect to $host:$port failed: $!") unless @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
140 0 my ($fh) = @_;
141 0 my $AEH;
142 $AEH = AnyEvent::Handle->new(
143 fh => $fh,
144 on_error => sub {
145 0 ▶ my ( $handle, $fatal, $message ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 $handle->destroy;
147 0 $cv->croak("Socket error: $message");
148 },
149 on_eof => sub {
150 0 ▶ $AEH->destroy;
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 },
152 0 );
153 $AEH->push_read(
154 cbor => sub {
155 0 ▶ my $answer = $_[1];
Subroutine __ANON__: not called CC=5 SCAR=34.0
156 0 ▶ if ( defined $answer and ref $answer eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $answer and ref $answer eq 'HASH') { } 0 0 0
Condition0%0/3
defined $answer and ref $answer eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $answer and ref $answer eq "HASH"
defined $answer ref $answer eq "HASH"
Truth table0%0/3
A and B
A= defined $answerB= ref $answer eq "HASH"
ABresult
0X0
100
111
157 0 ▶ if ( exists $answer->{error} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $answer->{'error'}) { } 0 0 0
elsif (exists $answer->{'result'}) { } 0 0 0
158 0 $cv->croak( "Service error: " . $answer->{error} );
159 }
160 elsif ( exists $answer->{result} ) {
161 0 $cv->send( $answer->{result} );
162 }
163 else {
164 0 $cv->croak(
165 "Something is wrong: no result and no error");
166 }
167 }
168 else {
169 0 $cv->croak("No answer");
170 }
171 }
172 0 );
173 $AEH->push_write( cbor =>
174 0 ▶ [ $options{engine}, $options{input}, $options{data} || {} ] );
Condition0%0/2
$options{'data'} || {}
l!l
00
MC/DC0%0/1
$options{'data'} || {}
$options{'data'}
Truth table0%0/2
A
A= $options{'data'}
Aresult
11
00
175 }
176 0 );
177  
178 0 ▶ $cv->cb( $options{cb} ) if $options{cb};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"cb"} 0 0 0
179  
180 0 $cv;
181 }
182  
183 sub transform {
184 0 ▶ my ( $self, $engine, $input, $data ) = @_;
Subroutine transform: not called CC=1 SCAR=6.9
Pod documented
185 0 $self->transform_cv(
186 engine => $engine,
187 input => $input,
188 data => $data,
189 )->recv;
190 }
191  
192 0 ▶sub jade { shift->transform( jade => @_ ) }
Subroutine jade: not called CC=1 SCAR=6.9
Pod documented
193  
194 0 ▶sub coffeescript { shift->transform( coffeescript => @_ ) }
Subroutine coffeescript: not called CC=1 SCAR=6.9
Pod documented
195  
196 0 ▶sub minify_html { shift->transform( minify_html => @_ ) }
Subroutine minify_html: not called CC=1 SCAR=6.9
Pod documented
197  
198 0 ▶sub minify_css { shift->transform( minify_css => @_ ) }
Subroutine minify_css: not called CC=1 SCAR=6.9
Pod documented
199  
200 0 ▶sub minify_js { shift->transform( minify_js => @_ ) }
Subroutine minify_js: not called CC=1 SCAR=6.9
Pod documented
201  
202 2 1;
203  
204 __END__
↑ Summary