| 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
Condition100%3/3
defined $file_name and length $file_name
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
| |||||||||||||||||||||||||||||
| 16 | 19 | ▶ | croak 'A content type cannot hold a line break' if ($content_type // q{}) =~ /[\r\n]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$content_type // ""
MC/DC100%1/1
$content_type // ""
$content_type
Truth table100%2/2
A
A= $content_type
| |||||||||||||||||||||||||||||
| 17 | 18 | ▶ | croak 'File contents must be a string or an open filehandle' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $contents and not &openhandle($contents)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$self->{"bytes"} //= $self->_read
$self->{"bytes"}
$self->_read
Truth table66%2/3
A //= B
A= $self->{"bytes"}B= $self->_read
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 59 | 1 | my $bytes = _slurp_handle($handle); | |||||||||||||||||||||||||||
| 60 | 1 | ▶ | close $handle or croak "Cannot read '$path': $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 // ""
MC/DC100%1/1
$bytes // ""
$bytes
Truth table100%2/2
A
A= $bytes
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 4 | 1; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | __END__ | ||||||||||||||||||||||||||||