blib/lib/Net/NodeTransformator.pm

filter: Statementstmt 29.0% 32 / 110 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 37.0% 10 / 27 Podpod 100% 10 / 10 totaltotal 24.8% 52 / 209 CC 23 SCAR 30.0
CC
23
Cov
20%
new
42.0
standalone
42.0
transform
42.0
CRAP 20.1
↑ Summary
1 package Net::NodeTransformator;
2 # ABSTRACT: interface to node transformator
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use AnyEvent::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use AnyEvent::Proc 0.101;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Env::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use POSIX qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '0.103'; # VERSION
16  
17  
18 sub new {
19 0 ▶ my ($class, $hostport) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
20 0 ▶ if ($hostport !~ m{:}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hostport =~ /:/) 0 0 0
21 0 ▶ if ($hostport =~ m{^\d+$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostport =~ /^\d+$/) { } 0 0 0
22 0 $hostport = "localhost:$hostport";
23 } else {
24 0 $hostport = "unix/:$hostport";
25 }
26 }
27 0 my ($host, $port) = parse_hostport($hostport);
28 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
29 0 $port = getcwd.'/'.$port;
30 }
31 bless {
32 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
33 port => $port,
34 } => ref $class || $class;
35 }
36  
37  
38 sub standalone {
39 0 ▶ my ($class, $hostport) = @_;
Subroutine standalone: not called CC=6 SCAR=37.4
Pod documented
40  
41 0 my $bin = 'transformator';
42 0 my ($path) = Env::Path->PATH->Whence($bin);
43  
44 0 ▶ unless ($hostport) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hostport) 0 0 0
45 0 my $tmpdir = tempdir(CLEANUP => 1);
46 0 $hostport = "$tmpdir/~sock";
47 }
48  
49 0 my $errstr;
50  
51 0 my $ok = 0;
52  
53 my $server = AnyEvent::Proc->new(
54 bin => $path,
55 args => [ $hostport ],
56 rtimeout => 10,
57 errstr => \$errstr,
58 0 ▶ on_rtimeout => sub { shift->kill; $ok = 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 );
60  
61 0 ▶ while (local $_ = $server->readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (local $_ = $server->readline) 0 0 0
62 0 AE::log debug => $_;
63 0 ▶ if (/server bound/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/server bound/) 0 0 0
64 0 $ok = 1;
65 0 last;
66 }
67 }
68  
69 0 ▶ AE::log error => $errstr if $errstr;
Branch: 1 branch
BranchTrueFalseTotal
if $errstr 0 0 0
70  
71 0 $server->stop_rtimeout;
72  
73 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 0 0
74 0 $server->fire_and_kill(10);
75 0 AE::log fatal => 'standalone service not started';
76 0 return undef;
77 } else {
78 0 my $client = $class->new($hostport);
79 0 $client->{_server} = $server;
80 0 return $client;
81 }
82 }
83  
84  
85 sub cleanup {
86 0 ▶ my ($self) = @_;
Subroutine cleanup: not called CC=2 SCAR=17.9
Pod documented
87 0 ▶ if (exists $self->{_server}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_server'}) { } 0 0 0
88 0 my $server = delete $self->{_server};
89 0 $server->fire_and_kill(10);
90 } else {
91 0 AE::log note => "$self->cleanup called when no standalone server active";
92 }
93  
94 }
95  
96  
97 sub transform_cv($%) {
98 0 ▶ my ($self, %options) = @_;
Subroutine transform_cv: not called CC=1 SCAR=6.9
Pod documented
99  
100 0 my $cv = AE::cv;
101  
102 my $err = sub {
103 0 ▶ $options{on_error}->(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 $cv->send(undef);
105 0 };
106  
107 0 my $host = $self->{host};
108 0 my $port = $self->{port};
109  
110 tcp_connect ($host, $port, sub {
111 0 ▶ return $err->("Connect to $host:$port failed: $!") unless @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
112 0 my ($fh) = @_;
113 0 my $AEH;
114 $AEH = AnyEvent::Handle->new(
115 fh => $fh,
116 on_error => sub {
117 0 ▶ my ($handle, $fatal, $message) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 $handle->destroy;
119 0 $err->("Socket error: $message");
120 },
121 on_eof => sub {
122 0 ▶ $AEH->destroy;
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 },
124 0 );
125 $AEH->push_read(cbor => sub {
126 0 ▶ my $answer = $_[1];
Subroutine __ANON__: not called CC=5 SCAR=34.0
127 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
128 0 ▶ if (exists $answer->{error}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $answer->{'error'}) { } 0 0 0
elsif (exists $answer->{'result'}) { } 0 0 0
129 0 $err->("Service error: ".$answer->{error});
130 } elsif (exists $answer->{result}) {
131 0 $cv->send($answer->{result});
132 } else {
133 0 $err->("Something is wrong: no result and no error");
134 }
135 } else {
136 0 $err->("No answer");
137 }
138 0 });
139 0 ▶ $AEH->push_write(cbor => [ $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
140 0 });
141 0 $cv;
142 }
143  
144  
145 sub transform($$$;$) {
146 0 ▶ my ($self, $engine, $input, $data) = @_;
Subroutine transform: not called CC=6 SCAR=37.4
Pod documented
147 0 my $error;
148 my $result = $self->transform_cv(
149 0 ▶ on_error => sub { $error = shift },
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 engine => $engine,
151 input => $input,
152 data => $data,
153 )->recv;
154 0 ▶ return $result if defined $result and not defined $error and not ref $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result and not defined $error and not ref $result 0 0 0
Condition0%0/6
defined $result and not defined $error and not ref $result
!ll&&!rl&&r
000
defined $result and not defined $error
!ll&&!rl&&r
000
MC/DC0%0/3
defined $result and not defined $error and not ref $result
defined $result not defined $error not ref $result
Truth table0%0/4
A and not(B) and not(C)
A= defined $resultB= not defined $errorC= not ref $result
ABCresult
0XX0
10X0
1100
1111
155 0 ▶ if (not defined $result and defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $result and defined $error) { } 0 0 0
Condition0%0/3
not defined $result and defined $error
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $result and defined $error
not defined $result defined $error
Truth table0%0/3
not(A) and B
A= not defined $resultB= defined $error
ABresult
0X0
100
111
156 0 AE::log error => $error;
157 } else {
158 0 AE::log error => "Something is wrong: $@";
159 }
160 }
161  
162  
163 0 ▶sub jade ($$;$) { shift->transform(jade => @_) }
Subroutine jade: not called CC=1 SCAR=6.9
Pod documented
164  
165  
166 0 ▶sub coffeescript ($$;$) { shift->transform(coffeescript => @_) }
Subroutine coffeescript: not called CC=1 SCAR=6.9
Pod documented
167  
168  
169 0 ▶sub minify_html ($$;$) { shift->transform(minify_html => @_) }
Subroutine minify_html: not called CC=1 SCAR=6.9
Pod documented
170  
171  
172 0 ▶sub minify_css ($$;$) { shift->transform(minify_css => @_) }
Subroutine minify_css: not called CC=1 SCAR=6.9
Pod documented
173  
174  
175 0 ▶sub minify_js ($$;$) { shift->transform(minify_js => @_) }
Subroutine minify_js: not called CC=1 SCAR=6.9
Pod documented
176  
177 2 1;
178  
179 __END__
↑ Summary