Shutterstock/Customer.pm

filter: Statementstmt 18.8% 30 / 159 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 33.3% 8 / 24 Podpod 88.8% 8 / 9 totaltotal 15.8% 46 / 290 CC 43 SCAR 36.6
CC
43
Cov
12%
_valid_metadata
90.0
__ANON__
72.0
_find_subscription_from_arg
56.0
CRAP 39.0
« Shutterstock/LicensedMedia.pm ↑ Summary Shutterstock/SearchResults.pm »
1 package WebService::Shutterstock::Customer;
2 {
3 1 $WebService::Shutterstock::Customer::VERSION = '0.006';
4 }
5  
6 # ABSTRACT: Class allowing API operations in the context of a specific customer
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use WebService::Shutterstock::Subscription;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use WebService::Shutterstock::LicensedVideo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use WebService::Shutterstock::AuthedClient;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 with 'WebService::Shutterstock::AuthedClient';
18  
19  
20 0 ▶sub account_id {
Subroutine account_id: not called CC=1 SCAR=6.9
Pod documented
21 0 my $self = shift;
22 0 return $self->_info->{account_id};
23 }
24  
25  
26 0 ▶sub metadata_field_definitions {
Subroutine metadata_field_definitions: not called CC=1 SCAR=6.9
Pod documented
27 0 my $self = shift;
28 0 return $self->_info->{metadata_field_definitions};
29 }
30  
31 1 has _info => ( is => 'lazy' );
32 0 ▶sub _build__info {
Subroutine _build__info: not called CC=1 SCAR=6.9
33 0 my $self = shift;
34 0 my $client = $self->client;
35 0 $client->GET( sprintf( '/customers/%s.json', $self->username ), $self->with_auth_params );
36 0 my $data = $client->process_response;
37 0 return $data;
38 }
39  
40  
41 1 has subscriptions => ( is => 'lazy' );
42 0 ▶sub _build_subscriptions {
Subroutine _build_subscriptions: not called CC=1 SCAR=6.9
43 0 my $self = shift;
44 0 $self->client->GET( sprintf( '/customers/%s/subscriptions.json', $self->username ), $self->with_auth_params );
45 0 my $subscriptions = $self->client->process_response;
46 0 return [ map { $self->new_with_auth( 'WebService::Shutterstock::Subscription', %$_ ) } @$subscriptions ]
47 }
48  
49  
50 0 ▶sub subscription {
Subroutine subscription: not called CC=1 SCAR=6.9
Pod documented
51 0 my $self = shift;
52 0 my ( $subscription, @extra ) = $self->find_subscriptions(@_);
53 0 return $subscription;
54 }
55  
56  
57 0 ▶sub find_subscriptions {
Subroutine find_subscriptions: not called CC=1 SCAR=6.9
Pod documented
58 0 my $self = shift;
59 0 my %criteria = @_;
60 0 ▶ my $filter = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
61 0 my $s = shift;
62 0 foreach my $m(keys %criteria){
63 0 ▶ croak "Invalid subscription filter key '$m': no such attribute on WebService::Shutterstock::Subscription" if !$s->can($m);
Branch: 1 branch
BranchTrueFalseTotal
unless $s->can($m) 0 0 0
64 0 my $value = $s->$m;
65 0 my $matcher = $criteria{$m};
66 0 ▶ if(ref $matcher eq 'CODE'){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $matcher eq 'CODE') { } 0 0 0
elsif (ref $matcher eq 'Regexp') { } 0 0 0
67 0 local $_ = $value;
68 0 ▶ return unless $matcher->($value);
Branch: 1 branch
BranchTrueFalseTotal
unless &$matcher($value) 0 0 0
69 } elsif(ref $matcher eq 'Regexp'){
70 0 ▶ return unless $value =~ $matcher;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$matcher/ 0 0 0
71 } else {
72 0 ▶ return unless $value eq $matcher;
Branch: 1 branch
BranchTrueFalseTotal
unless $value eq $matcher 0 0 0
73 }
74 }
75 0 return 1;
76 0 };
77 0 return grep { $filter->($_) } @{ $self->subscriptions };
78 }
79  
80  
81 0 ▶sub lightboxes {
Subroutine lightboxes: not called CC=3 SCAR=24.8
Pod documented
82 0 my $self = shift;
83 0 ▶ my $extended = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
84 0 ▶ $self->client->GET(
Branch: 1 branch
BranchTrueFalseTotal
$extended ? : 0 0 0
85 sprintf(
86 '/customers/%s/lightboxes%s.json',
87 $self->username, $extended ? '/extended' : ''
88 ),
89 $self->with_auth_params
90 );
91 0 my $lightboxes = $self->client->process_response;
92 0 return [ map {$self->new_with_auth('WebService::Shutterstock::Lightbox', %$_) } @$lightboxes ];
93 }
94  
95  
96 0 ▶sub lightbox {
Subroutine lightbox: not called CC=5 SCAR=34.0
Pod documented
97 0 my $self = shift;
98 0 my $id = shift;
99 0 my $lightbox = $self->new_with_auth('WebService::Shutterstock::Lightbox', lightbox_id => $id);
100 0 ▶ eval { $lightbox->load; 1 } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $lightbox->load; 1 } } or do { my $e = $@; if (eval { do { $e->isa("WebService::Shutterstock::Exception") } } and $e->code eq 404 || $e->code eq 500) { $lightbox = undef; } else { die $e; } } 0 0 0
101 0 my $e = $@;
102 0 ▶ if(eval { $e->isa('WebService::Shutterstock::Exception') } && ($e->code eq 404 || $e->code eq 500)){
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $e->isa('WebService::Shutterstock::Exception') } } and $e->code eq 404 || $e->code eq 500) { } 0 0 0
Condition0%0/6
eval { do { $e->isa("WebService::Shutterstock::Exception") } } and $e->code eq 404 || $e->code eq 500
!ll&&!rl&&r
000
$e->code eq 404 or $e->code eq 500
l!l&&r!l&&!r
000
MC/DC0%0/3
eval { do { $e->isa("WebService::Shutterstock::Exception") } } and $e->code eq 404 || $e->code eq 500
eval { do { $e->isa("WebService::Shutterstock::Exception") } } $e->code eq 404 $e->code eq 500
Truth table0%0/4
A and B or C
A= eval { do { $e->isa("WebService::Shutterstock::Exception") } }B= $e->code eq 404C= $e->code eq 500
ABCresult
0XX0
1000
11X1
1011
103 0 $lightbox = undef;
104 } else {
105 0 die $e;
106 }
107 };
108 0 return $lightbox;
109 }
110  
111  
112 0 ▶sub downloads {
Subroutine downloads: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114 0 my %args = @_;
115 0 $self->client->GET(
116 sprintf( '/customers/%s/images/downloads.json', $self->username ),
117 $self->with_auth_params(%args) );
118 0 return $self->client->process_response;
119 }
120  
121  
122 0 ▶sub license_image {
Subroutine license_image: not called CC=6 SCAR=37.4
Pod documented
123 0 my $self = shift;
124 0 my %args = @_;
125  
126 0 ▶ my $image_id = $args{image_id} or croak "Must specify image_id to license";
Branch: 1 branch
BranchTrueFalseTotal
my $image_id = $args{"image_id"} or croak("Must specify image_id to license") 0 0 0
127  
128 0 ▶ my $editorial_acknowledgement = $args{editorial_acknowledgement} || 0;
Condition0%0/2
$args{'editorial_acknowledgement'} || 0
l!l
00
MC/DC0%0/1
$args{'editorial_acknowledgement'} || 0
$args{'editorial_acknowledgement'}
Truth table0%0/2
A
A= $args{'editorial_acknowledgement'}
Aresult
11
00
129  
130 0 my $subscription = $self->_find_subscription_from_arg('photo', $args{subscription});
131 0 ▶ if(!$subscription){
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription) 0 0 0
132 0 croak "Must specify a subscription to license images under";
133 }
134  
135 0 my $metadata = $self->_valid_metadata($args{metadata});
136  
137 0 my $size = $self->_valid_size_for_subscription($subscription, $args{size});
138  
139 0 ▶ my $format = $size eq 'vector' ? 'eps' : 'jpg';
Branch: 1 branch
BranchTrueFalseTotal
$size eq 'vector' ? : 0 0 0
140 0 my $client = $self->client;
141  
142 0 ▶ $client->POST(
Branch: 1 branch
BranchTrueFalseTotal
$metadata ? : 0 0 0
143 sprintf(
144 '/subscriptions/%s/images/%s/sizes/%s.json',
145 $subscription->id, $image_id, $size
146 ),
147 $self->with_auth_params(
148 format => $format,
149 editorial_acknowledgement => $editorial_acknowledgement,
150 ( $metadata ? ( metadata => encode_json($metadata) ) : () ),
151 )
152 );
153  
154 0 return WebService::Shutterstock::LicensedImage->new($client->process_response);
155 }
156  
157  
158 0 ▶sub license_video {
Subroutine license_video: not called CC=5 SCAR=34.0
Pod undocumented
159 0 my $self = shift;
160 0 my %args = @_;
161  
162 0 ▶ my $video_id = $args{video_id} or croak "Must specify video_id to license";
Branch: 1 branch
BranchTrueFalseTotal
my $video_id = $args{"video_id"} or croak("Must specify video_id to license") 0 0 0
163  
164 0 ▶ my $editorial_acknowledgement = $args{editorial_acknowledgement} || 0;
Condition0%0/2
$args{'editorial_acknowledgement'} || 0
l!l
00
MC/DC0%0/1
$args{'editorial_acknowledgement'} || 0
$args{'editorial_acknowledgement'}
Truth table0%0/2
A
A= $args{'editorial_acknowledgement'}
Aresult
11
00
165  
166 0 my $subscription = $self->_find_subscription_from_arg('video', $args{subscription});
167 0 ▶ if(!$subscription){
Branch: 1 branch
BranchTrueFalseTotal
unless ($subscription) 0 0 0
168 0 croak "Must specify a subscription to license videos under";
169 }
170  
171 0 my $metadata = $self->_valid_metadata($args{metadata});
172  
173 0 my $size = $self->_valid_size_for_subscription($subscription, $args{size});
174  
175 0 my $client = $self->client;
176  
177 0 ▶ $client->POST(
Branch: 1 branch
BranchTrueFalseTotal
$metadata ? : 0 0 0
178 sprintf(
179 '/subscriptions/%s/videos/%s/sizes/%s.json',
180 $subscription->id, $video_id, $size
181 ),
182 $self->with_auth_params(
183 editorial_acknowledgement => $editorial_acknowledgement,
184 ( $metadata ? ( metadata => encode_json($metadata) ) : () ),
185 )
186 );
187  
188 0 return WebService::Shutterstock::LicensedVideo->new($client->process_response);
189 }
190  
191 0 ▶sub _find_subscription_from_arg {
Subroutine _find_subscription_from_arg: not called CC=7 SCAR=40.3
192 0 my $self = shift;
193 0 my $type = shift;
194 0 my $sub_arg = shift;
195  
196  
197 0 ▶ my $single_finder = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
198 0 my %criteria = @_;
199 0 my @matching = $self->find_subscriptions( %criteria, is_active => 1, site => "${type}_subscription" );
200 0 ▶ if ( @matching == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@matching == 0) { } 0 0 0
elsif (@matching > 1) { } 0 0 0
201 0 croak "Unable to find a matching subscription to license ${type}s";
202 } elsif ( @matching > 1 ) {
203 0 croak "You have more than one active subscription. Please provide a WebService::Shutterstock::Subscription object or specify unique critiria to identify which subscription you would like to use (i.e. { license => 'standard' } or { id => 26374582 } )";
204 }
205 0 return $matching[0];
206 0 };
207  
208 0 my $subscription;
209  
210 0 ▶ if($sub_arg){
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_arg) { } 0 0 0
211 0 ▶ if(!ref($sub_arg)){
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $sub_arg) { } 0 0 0
elsif (ref $sub_arg eq 'HASH') { } 0 0 0
elsif (eval { do { $sub_arg->isa('WebService::Shutterstock::Subscription') } }) { } 0 0 0
212 0 $subscription = $self->subscription( id => $sub_arg );
213 } elsif(ref($sub_arg) eq 'HASH'){
214 0 $subscription = $single_finder->( %$sub_arg );
215 0 } elsif(eval { $sub_arg->isa('WebService::Shutterstock::Subscription') }){
216 0 $subscription = $sub_arg;
217 }
218 } else {
219 0 $subscription = $single_finder->();
220 }
221 0 ▶ if($subscription && $subscription->site ne "${type}_subscription"){
Branch: 1 branch
BranchTrueFalseTotal
if ($subscription and $subscription->site ne "${type}_subscription") 0 0 0
Condition0%0/3
$subscription and $subscription->site ne "${type}_subscription"
!ll&&!rl&&r
000
MC/DC0%0/2
$subscription and $subscription->site ne "${type}_subscription"
$subscription $subscription->site ne "${type}_subscription"
Truth table0%0/3
A and B
A= $subscriptionB= $subscription->site ne "${type}_subscription"
ABresult
0X0
100
111
222 0 croak sprintf('The subscription specified is for the wrong "site" (needed "%s_subscription", found %s)', $type, $subscription->site);
223 }
224 0 return $subscription;
225 }
226  
227 0 ▶sub _valid_size_for_subscription {
Subroutine _valid_size_for_subscription: not called CC=5 SCAR=34.0
228 0 my $self = shift;
229 0 my $subscription = shift;
230 0 my $size = shift;
231  
232 0 my @valid_sizes = $subscription->sizes_for_licensing;
233  
234 0 ▶ if(!$size && @valid_sizes == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (not $size and @valid_sizes == 1) 0 0 0
Condition0%0/3
not $size and @valid_sizes == 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $size and @valid_sizes == 1
not $size @valid_sizes == 1
Truth table0%0/3
not(A) and B
A= not $sizeB= @valid_sizes == 1
ABresult
0X0
100
111
235 0 $size = $valid_sizes[0];
236 }
237 0 ▶ croak "Must specify size of video to license" if !$size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 0 0
238  
239 0 ▶ if ( !grep { $_ eq $size } @valid_sizes ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $size;} @valid_sizes) 0 0 0
240 0 my %uniq_sizes = map {$_ => 1} @valid_sizes;
241 0 croak "Invalid size '$size', please specify a valid size: " . join(", ", keys %uniq_sizes);
242 }
243 0 return $size;
244 }
245  
246 0 ▶sub _valid_metadata {
Subroutine _valid_metadata: not called CC=9 SCAR=45.0
247 0 my $self = shift;
248 0 my $metadata = shift;
249 0 my $valid;
250 0 ▶ if(my $metadata_definitions = $self->metadata_field_definitions){
Branch: 1 branch
BranchTrueFalseTotal
if (my $metadata_definitions = $self->metadata_field_definitions) 0 0 0
251 0 ▶ $valid = $metadata || {};
Condition0%0/2
$metadata || {}
l!l
00
MC/DC0%0/1
$metadata || {}
$metadata
Truth table0%0/2
A
A= $metadata
Aresult
11
00
252 0 my @missing;
253 0 foreach my $md(@{ $metadata_definitions }){
254 0 ▶ $valid->{$md->{name_api}} = '' if !defined $valid->{$md->{name_api}};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$valid{$md->{"name_api"}} 0 0 0
255 0 ▶ if($md->{is_required} && $valid->{$md->{name_api}} eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if ($md->{"is_required"} and $$valid{$md->{"name_api"}} eq "") 0 0 0
Condition0%0/3
$md->{"is_required"} and $$valid{$md->{"name_api"}} eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$md->{"is_required"} and $$valid{$md->{"name_api"}} eq ""
$md->{"is_required"} $$valid{$md->{"name_api"}} eq ""
Truth table0%0/3
A and B
A= $md->{"is_required"}B= $$valid{$md->{"name_api"}} eq ""
ABresult
0X0
100
111
256 0 push @missing, $md->{name_api};
257 }
258 }
259 0 ▶ if(@missing){
Branch: 1 branch
BranchTrueFalseTotal
if (@missing) 0 0 0
260 0 ▶ croak
Branch: 1 branch
BranchTrueFalseTotal
@missing == 1 ? : 0 0 0
261 sprintf(
262 'Missing required metadata field%s for licensing image: %s',
263 @missing == 1 ? '' : 's', join ', ', @missing );
264 }
265 }
266 0 return $valid;
267 }
268  
269 1 1;
270  
271 __END__
« Shutterstock/LicensedMedia.pm ↑ Summary Shutterstock/SearchResults.pm »