blib/lib/Pinto/Remote/SelfContained/Action.pm

filter: Statementstmt 96.4% 81 / 84 Branchbran 78.5% 11 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 21 / 21 Podpod 100% 1 / 1 totaltotal 90.7% 118 / 130 CC 10 SCAR 9.8
CC
10
Cov
92%
_response_callback
7.1
_make_result
2.5
_make_request
2.0
CRAP 2.7
« blib/lib/Pinto/Remote/SelfContained/Result.pm ↑ Summary blib/lib/Pinto/Remote/SelfContained/Types.pm »
1 package
2 Pinto::Remote::SelfContained::Action; # hide from PAUSE
3 # ABSTRACT: base class for remote Actions
4  
5 5 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use JSON::MaybeXS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Pinto::Remote::SelfContained::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Pinto::Remote::SelfContained::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Pinto::Remote::SelfContained::Types qw(Chrome Uri Username);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Pinto::Remote::SelfContained::Util qw(current_time_offset);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Types::Standard qw(ArrayRef Bool HashRef InstanceOf Maybe Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 our $VERSION = '1.999'; # TRIAL VERSION
19  
20 5 with qw(
21 Pinto::Remote::SelfContained::HasHttptiny
22 );
23  
24 5 has chrome => (is => 'ro', isa => Chrome, required => 1);
25  
26 5 has name => (is => 'ro', isa => Str, required => 1);
27 5 has root => (is => 'ro', isa => Uri, coerce => 1, required => 1);
28 2 ▶has args => (is => 'ro', isa => HashRef, default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30 5 has username => (is => 'ro', isa => Username, required => 1);
31 5 has password => (is => 'ro', isa => Maybe[Str], required => 1);
32  
33 5 has error => (is => 'rw');
34  
35  
36 1 ▶sub execute {
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
37 1 my ($self, $streaming_callback) = @_;
38  
39 1 my $request = $self->_make_request;
40 1 my $response = $self->_send_request($request, $streaming_callback);
41  
42 1 return $self->_make_result($response);
43 }
44  
45 1 ▶sub _make_result {
Subroutine _make_result: called CC=2 SCAR=9.2
46 1 my ($self, $response) = @_;
47  
48 return Pinto::Remote::SelfContained::Result->new
49 1 ▶ if $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"success"} 1 0 1
50  
51 0 $self->error( $response->{content} );
52 0 return Pinto::Remote::SelfContained::Result->new(was_successful => 0);
53 }
54  
55 5 ▶sub _make_request {
Subroutine _make_request: called CC=2 SCAR=7.0
56 5 my ($self, $action_name) = @_;
57  
58 5 ▶ $action_name //= $self->name;
Condition66%2/3
$action_name //= $self->name
l!l&&r!l&&!r
230
MC/DC0%0/2
$action_name //= $self->name
$action_name $self->name
Truth table66%2/3
A //= B
A= $action_nameB= $self->name
ABresult
1X1
011
000
59  
60 5 my $uri = URI->new( $self->root );
61 5 $uri->path_segments('', 'action', lc $action_name);
62  
63 5 return Pinto::Remote::SelfContained::Request->new(
64 username => $self->username,
65 password => $self->password,
66 method => 'POST',
67 uri => $uri,
68 body_parts => $self->_make_body_parts,
69 );
70 }
71  
72 5 ▶sub _make_body_parts {
Subroutine _make_body_parts: called CC=1 SCAR=0.0
73 5 my ($self) = @_;
74  
75 5 return [$self->_chrome_args, $self->_pinto_args, $self->_action_args];
76 }
77  
78 5 ▶sub _chrome_args {
Subroutine _chrome_args: called CC=1 SCAR=0.0
79 5 my ($self) = @_;
80  
81 5 my $chrome_args = {
82 verbose => $self->chrome->verbose,
83 color => $self->chrome->color,
84 palette => $self->chrome->palette,
85 quiet => $self->chrome->quiet,
86 };
87  
88 5 return { name => 'chrome', data => encode_json($chrome_args) };
89 }
90  
91 5 ▶sub _pinto_args {
Subroutine _pinto_args: called CC=1 SCAR=0.0
92 5 my ($self) = @_;
93  
94 5 my $pinto_args = {
95 username => $self->username,
96 time_offset => current_time_offset(),
97 };
98  
99 5 return { name => 'pinto', data => encode_json($pinto_args) };
100 }
101  
102 5 ▶sub _action_args {
Subroutine _action_args: called CC=1 SCAR=0.0
103 5 my ($self) = @_;
104  
105 5 my $action_args = $self->args;
106  
107 5 return { name => 'action', data => encode_json($action_args) };
108 }
109  
110 5 ▶sub _send_request {
Subroutine _send_request: called CC=2 SCAR=6.9
111 5 my ($self, $request, $streaming_callback) = @_;
112  
113 5 ▶ $request //= $self->_make_request;
Condition66%2/3
$request //= $self->_make_request
l!l&&r!l&&!r
320
MC/DC0%0/2
$request //= $self->_make_request
$request $self->_make_request
Truth table66%2/3
A //= B
A= $requestB= $self->_make_request
ABresult
1X1
011
000
114  
115 5 my $status = 0;
116 5 my $buffer = '';
117 5 ▶ my $callback = sub { $self->_response_callback( $streaming_callback, \$status, \$buffer, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
118 5 my $response = $self->httptiny->request( $request->as_request_items($callback) );
119  
120 5 $self->chrome->progress_done;
121  
122 5 return $response;
123 }
124  
125 20 ▶sub _response_callback {
Subroutine _response_callback: called CC=7 SCAR=19.6
126 20 my ($self, $streaming_callback, $status_ref, $buffer_ref, $new_data, $partial_result) = @_;
127  
128 20 $partial_result->{content} .= $new_data;
129 20 $$buffer_ref .= $new_data;
130  
131 20 ▶ while ($$buffer_ref =~ s/\A (.*) \n//x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffer_ref =~ s/\A (.*) \n//x) 10 20 30
132 10 my $line = $1;
133 10 ▶ if ($line eq '## Status: ok') {
Branch: 4 branches
BranchTrueFalseTotal
if ($line eq '## Status: ok') { } 5 5 10
elsif ($line eq '## -- ##') { } 0 5 5
elsif ($line eq '## . ##') { } 0 5 5
elsif ($line =~ /^## (.*)/) { } 2 3 5
134 5 $$status_ref = 1;
135 }
136 elsif ($line eq '## -- ##') {
137 # Null message; discard
138 }
139 elsif ($line eq '## . ##') {
140 # Progress message
141 0 $self->chrome->show_progress;
142 }
143 elsif ($line =~ m{^## (.*)}) {
144 # Diagnostic message; emit as warning
145 2 $self->chrome->diag("$1");
146 }
147 else {
148 # Other: emit as text, and send to any streaming callback
149 3 $self->chrome->show($line);
150 3 ▶ $streaming_callback->($line) if $streaming_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $streaming_callback 2 1 3
151 }
152 }
153 }
154  
155 5 1;
156  
157 __END__
« blib/lib/Pinto/Remote/SelfContained/Result.pm ↑ Summary blib/lib/Pinto/Remote/SelfContained/Types.pm »