| 1 | package WebService::Shutterstock::LicensedMedia; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 5 | $WebService::Shutterstock::LicensedMedia::VERSION = '0.006'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Role for providing common functionality for licensed media | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use WebService::Shutterstock::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | has download_url => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub download { | ||||||||||||||||||||||||||
|
Subroutine
download: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 21 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 22 | 0 | my @unknown_args = grep { !/^(file|directory)$/ } keys %args; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | croak "Invalid args: @unknown_args (expected either 'file' or 'download')" if @unknown_args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | my $url = $self->download_url; | |||||||||||||||||||||||||||
| 27 | 0 | my $destination; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | if($args{directory}){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 29 | 0 | $destination = $args{directory}; | |||||||||||||||||||||||||||
| 30 | 0 | $destination =~ s{/$}{}; | |||||||||||||||||||||||||||
| 31 | 0 | my($basename) = $url =~ m{.+/(.+)}; | |||||||||||||||||||||||||||
| 32 | 0 | $destination .= "/$basename"; | |||||||||||||||||||||||||||
| 33 | } elsif($args{file}){ | ||||||||||||||||||||||||||||
| 34 | 0 | $destination = $args{file}; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if(!defined $destination && !defined wantarray){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $destination or defined wantarray
MC/DC0%0/2
defined $destination or defined wantarray
defined $destination
defined wantarray
Truth table0%0/3
A or B
A= defined $destinationB= defined wantarray
| |||||||||||||||||||||||||||||
| 37 | 0 | croak "Refusing to download media in void context without specifying a destination file or directory (specify ->download(file => \$some_file) or ->download(directory => \$some_dir)"; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $response = $ua->get( $url, ( $destination ? ( ':content_file' => $destination ) : () ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | if(my $died = $response->header('X-Died') ){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 42 | 0 | die WebService::Shutterstock::Exception->new( | |||||||||||||||||||||||||||
| 43 | response => $response, | ||||||||||||||||||||||||||||
| 44 | error => "Unable to save media to $destination: $died" | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | } elsif($response->code == 200){ | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | return $destination || $response->content; | ||||||||||||||||||||||||||
|
Condition0%0/3
$destination || $response->content
MC/DC0%0/2
$destination || $response->content
$destination
$response->content
Truth table0%0/3
A || B
A= $destinationB= $response->content
| |||||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||
| 49 | 0 | die WebService::Shutterstock::Exception->new( | |||||||||||||||||||||||||||
| 50 | response => $response, | ||||||||||||||||||||||||||||
| 51 | error => $response->status_line . ": unable to retrieve media", | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 5 | 1; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | __END__ | ||||||||||||||||||||||||||||