Twilio/Http/File.pm

filter: Statementstmt 100% 43 / 43 Branchbran 94.4% 17 / 18 Conditioncond 92.3% 12 / 13 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 6 / 6 totaltotal 96.0% 98 / 102 CC 15 SCAR 12.1
CC
15
Cov
97%
new
7.0
_read
4.0
_slurp_path
3.0
CRAP 3.4
« Twilio/Version.pm ↑ Summary Twilio/Page.pm »
1 package Twilio::Http::File;
2  
3 4 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Scalar::Util qw(openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '2.001';
11  
12 21 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
13 21 my ($class, $file_name, $contents, $content_type) = @_;
14  
15 21 ▶ croak 'A file needs a name' if !defined $file_name || !length $file_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file_name and length $file_name 2 19 21
Condition100%3/3
defined $file_name and length $file_name
!ll&&!rl&&r
1119
MC/DC100%2/2
defined $file_name and length $file_name
defined $file_name length $file_name
Truth table100%3/3
A and B
A= defined $file_nameB= length $file_name
ABresult
0X0
100
111
16 19 ▶ croak 'A content type cannot hold a line break' if ($content_type // q{}) =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type // "") =~ /[\r\n]/u 1 18 19
Condition100%2/2
$content_type // ""
l!l
415
MC/DC100%1/1
$content_type // ""
$content_type
Truth table100%2/2
A
A= $content_type
Aresult
11
00
17 18 ▶ croak 'File contents must be a string or an open filehandle'
Branch: 1 branch
BranchTrueFalseTotal
if ref $contents and not &openhandle($contents) 1 17 18
Condition100%3/3
ref $contents and not &openhandle($contents)
!ll&&!rl&&r
1431
MC/DC100%2/2
ref $contents and not &openhandle($contents)
ref $contents not &openhandle($contents)
Truth table100%3/3
A and not(B)
A= ref $contentsB= not &openhandle($contents)
ABresult
0X0
100
111
18 if ref $contents && !openhandle($contents);
19  
20 17 return bless { file_name => $file_name, contents => $contents, content_type => $content_type },
21 $class;
22 }
23  
24 1 ▶sub get_file_name { return $_[0]{file_name} }
Subroutine get_file_name: called CC=1 SCAR=0.0
Pod documented
25 2 ▶sub get_contents { return $_[0]{contents} }
Subroutine get_contents: called CC=1 SCAR=0.0
Pod documented
26 9 ▶sub get_content_type { return $_[0]{content_type} }
Subroutine get_content_type: called CC=1 SCAR=0.0
Pod documented
27  
28 # A file read from disk is sent under its own name, without the folders it was in.
29 10 ▶sub upload_name {
Subroutine upload_name: called CC=2 SCAR=6.9
Pod documented
30 10 my ($self) = @_;
31 10 ▶ return defined $self->{contents} ? $self->{file_name} : basename($self->{file_name});
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'contents'} ? : 9 1 10
32 }
33  
34 # Read once and kept: a request is built again for each retry after a 429, and a filehandle
35 # would be empty by the second one.
36 18 ▶sub bytes {
Subroutine bytes: called CC=2 SCAR=7.0
Pod documented
37 18 my ($self) = @_;
38 18 ▶ $self->{bytes} //= $self->_read;
Condition66%2/3
$self->{"bytes"} //= $self->_read
l!l&&r!l&&!r
2160
MC/DC0%0/2
$self->{"bytes"} //= $self->_read
$self->{"bytes"} $self->_read
Truth table66%2/3
A //= B
A= $self->{"bytes"}B= $self->_read
ABresult
1X1
011
000
39 16 return $self->{bytes};
40 }
41  
42 16 ▶sub _read {
Subroutine _read: called CC=4 SCAR=13.9
43 16 my ($self) = @_;
44  
45 16 my $contents = $self->{contents};
46 my $bytes =
47 !defined $contents ? _slurp_path($self->{file_name})
48 16 ▶ : ref $contents ? _slurp_handle($contents)
Branch: 2 branches
BranchTrueFalseTotal
!defined($contents) ? : 2 14 16
ref $contents ? : 3 11 14
49 : $contents;
50  
51 # Guessing an encoding for text would change what the caller meant to send.
52 15 ▶ utf8::downgrade($bytes, 1) or croak 'File contents must be bytes: encode text before sending it';
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($bytes, 1) or croak("File contents must be bytes: encode text before sending it") 14 1 15
53 14 return $bytes;
54 }
55  
56 2 ▶sub _slurp_path {
Subroutine _slurp_path: called CC=3 SCAR=11.0
57 2 my ($path) = @_;
58 2 ▶ open my $handle, '<:raw', $path or croak "Cannot read '$path': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<:raw", $path or croak("Cannot read '${path}': $!") 1 1 2
59 1 my $bytes = _slurp_handle($handle);
60 1 ▶ close $handle or croak "Cannot read '$path': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $handle or croak("Cannot read '${path}': $!") 1 0 1
61 1 return $bytes;
62 }
63  
64 4 ▶sub _slurp_handle {
Subroutine _slurp_handle: called CC=2 SCAR=6.9
65 4 my ($handle) = @_;
66 4 local $/ = undef;
67 4 my $bytes = readline $handle;
68 4 ▶ return $bytes // q{};
Condition100%2/2
$bytes // ""
l!l
31
MC/DC100%1/1
$bytes // ""
$bytes
Truth table100%2/2
A
A= $bytes
Aresult
11
00
69 }
70  
71 4 1;
72  
73 __END__
« Twilio/Version.pm ↑ Summary Twilio/Page.pm »