Cloudinary.pm

filter: Statementstmt 92.3% 85 / 92 Branchbran 82.3% 28 / 34 Conditioncond 57.5% 19 / 33 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 76.4% 13 / 17 Podpod 100% 3 / 3 totaltotal 77.6% 153 / 197 CC 35 SCAR 17.9
CC
35
Cov
83%
upload
10.4
url_for
9.5
_call_api
6.4
CRAP 6.0
↑ Summary Mojolicious/Plugin/Cloudinary.pm »
1 package Cloudinary;
2 4 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Mojo::Util qw(sha1_sum url_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = '0.16';
9 4 our (%SHORTER, %LONGER);
10 4 my @SIGNATURE_KEYS = qw(callback eager format public_id tags timestamp transformation type);
11  
12 {
13 4 %LONGER = (
14 a => 'angle',
15 b => 'background',
16 c => 'crop',
17 d => 'default_image',
18 e => 'effect',
19 f => 'fetch_format',
20 g => 'gravity',
21 h => 'height',
22 l => 'overlay',
23 p => 'prefix',
24 q => 'quality',
25 r => 'radius',
26 t => 'named_transformation',
27 w => 'width',
28 x => 'x',
29 y => 'y',
30 );
31 4 %SHORTER = reverse %LONGER;
32 }
33  
34 0 ▶has cloud_name => sub { die 'cloud_name is required in constructor' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 0 ▶has api_key => sub { die 'api_key is required in constructor' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
36 0 ▶has api_secret => sub { die 'api_secret is required in constructor' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
37 0 ▶has private_cdn => sub { die 'private_cdn is required in constructor' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
38 4 has _api_url => 'http://api.cloudinary.com/v1_1';
39 4 has _public_cdn => 'http://res.cloudinary.com';
40 1 ▶has _ua => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 1 my $ua = Mojo::UserAgent->new;
42  
43 $ua->on(
44 4 ▶ start => sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
45 4 my ($ua, $tx) = @_;
46  
47 4 for my $part (@{$tx->req->content->parts}) {
48 17 ▶ my $content_type = $part->headers->content_type || '';
Condition50%1/2
$part->headers->content_type || ''
l!l
017
MC/DC0%0/1
$part->headers->content_type || ''
$part->headers->content_type
Truth table50%1/2
A
A= $part->headers->content_type
Aresult
11
00
49 17 ▶ $part->headers->remove('Content-Type') if $content_type eq 'text/plain';
Branch: 1 branch
BranchTrueFalseTotal
if $content_type eq "text/plain" 0 17 17
50 }
51 }
52 1 );
53  
54 1 return $ua;
55 4 };
56  
57 7 ▶sub upload {
Subroutine upload: called CC=9 SCAR=23.4
Pod documented
58 7 my ($self, $args, $cb) = @_;
59  
60 # TODO: transformation, eager
61 7 ▶ $args = {file => $args} if ref $args ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args ne "HASH" 4 3 7
62 7 ▶ $args->{resource_type} ||= 'image';
Condition50%1/2
$args->{"resource_type"} ||= "image"
l!l
07
MC/DC0%0/1
$args->{"resource_type"} ||= "image"
$args->{"resource_type"}
Truth table50%1/2
A
A= $args->{"resource_type"}
Aresult
11
00
63 7 ▶ $args->{timestamp} ||= time;
Condition100%2/2
$args->{"timestamp"} ||= time
l!l
34
MC/DC100%1/1
$args->{"timestamp"} ||= time
$args->{"timestamp"}
Truth table100%2/2
A
A= $args->{"timestamp"}
Aresult
11
00
64  
65 7 ▶ die "Usage: \$self->upload({ file => ... })" unless defined $args->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"file"} 1 6 7
66  
67 6 ▶ if (ref $args->{tags} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{"tags"} eq "ARRAY") 0 6 6
68 0 $args->{tags} = join ',', @{$args->{tags}};
69 }
70 6 ▶ if (UNIVERSAL::isa($args->{file}, 'Mojo::Asset')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($args->{'file'}, 'Mojo::Asset')) { } 4 2 6
elsif (UNIVERSAL::isa($args->{'file'}, 'Mojo::Upload')) { } 0 2 2
71 $args->{file}
72 4 ▶ = {file => $args->{file}, filename => $args->{filename} || basename($args->{file}->path)};
Condition33%1/3
$args->{'filename'} || basename($args->{'file'}->path)
l!l&&r!l&&!r
040
MC/DC0%0/2
$args->{'filename'} || basename($args->{'file'}->path)
$args->{'filename'} basename($args->{'file'}->path)
Truth table33%1/3
A || B
A= $args->{'filename'}B= basename($args->{'file'}->path)
ABresult
1X1
011
000
73 }
74 elsif (UNIVERSAL::isa($args->{file}, 'Mojo::Upload')) {
75 0 $args->{file} = {file => $args->{file}->asset, filename => $args->{file}->filename};
76 }
77  
78 $self->_call_api(
79 upload => $args,
80 {
81 timestamp => time,
82 9 (map { ($_, $args->{$_}) } grep { defined $args->{$_} } @SIGNATURE_KEYS),
83 file => $args->{file},
84 },
85 6 $cb,
86 );
87 }
88  
89 3 ▶sub destroy {
Subroutine destroy: called CC=6 SCAR=18.1
Pod documented
90 3 my ($self, $args, $cb) = @_;
91  
92 3 ▶ $args = {public_id => $args} unless ref $args eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 2 1 3
93  
94 3 ▶ die "Usage: \$self->destroy({ public_id => ... })" unless defined $args->{public_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{"public_id"} 1 2 3
95  
96 2 ▶ $args->{resource_type} ||= 'image';
Condition50%1/2
$args->{"resource_type"} ||= "image"
l!l
02
MC/DC0%0/1
$args->{"resource_type"} ||= "image"
$args->{"resource_type"}
Truth table50%1/2
A
A= $args->{"resource_type"}
Aresult
11
00
97  
98 $self->_call_api(
99 destroy => $args,
100 {
101 public_id => $args->{public_id},
102 timestamp => $args->{timestamp} || time,
103 2 ▶ type => $args->{type} || 'upload',
Condition75%3/4
$args->{'timestamp'} || time
l!l
11
$args->{'type'} || 'upload'
l!l
02
MC/DC100%1/1
$args->{'timestamp'} || time
$args->{'timestamp'}
MC/DC0%0/1
$args->{'type'} || 'upload'
$args->{'type'}
Truth table100%2/2
A
A= $args->{'timestamp'}
Aresult
11
00
Truth table50%1/2
A
A= $args->{'type'}
Aresult
11
00
104 },
105 $cb,
106 );
107 }
108  
109 8 ▶sub _call_api {
Subroutine _call_api: called CC=6 SCAR=18.5
110 8 my ($self, $action, $args, $post, $cb) = @_;
111 8 my $url = join '/', $self->_api_url, $self->cloud_name, $args->{resource_type}, $action;
112 8 my $headers = {'Content-Type' => 'multipart/form-data'};
113  
114 8 $post->{api_key} = $self->api_key;
115 8 $post->{signature} = $self->_api_sign_request($post);
116  
117 8 Scalar::Util::weaken($self);
118 my $tx = $self->_ua->post(
119 $url, $headers,
120 form => $post,
121 8 ▶ $cb ? sub { $self->$cb($_[1]->res->json || {error => $_[1]->error || 'Unknown error'}) } : (),
Subroutine __ANON__: called CC=3 SCAR=12.5
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 2 6 8
Condition25%1/4
$_[1]->res->json || {'error', $_[1]->error || 'Unknown error'}
l!l
20
$_[1]->error || 'Unknown error'
l!l
00
MC/DC0%0/1
$_[1]->res->json || {'error', $_[1]->error || 'Unknown error'}
$_[1]->res->json
MC/DC0%0/1
$_[1]->error || 'Unknown error'
$_[1]->error
Truth table50%1/2
A
A= $_[1]->res->json
Aresult
11
00
Truth table0%0/2
A
A= $_[1]->error
Aresult
11
00
122 );
123  
124 8 ▶ return $self if $cb; # non-blocking
Branch: 1 branch
BranchTrueFalseTotal
if $cb 2 6 8
125 6 my $res = $tx->error;
126 6 ▶ die $res->{message} || 'Unknown error' if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 6 6
Condition0%0/2
$res->{'message'} || 'Unknown error'
l!l
00
MC/DC0%0/1
$res->{'message'} || 'Unknown error'
$res->{'message'}
Truth table0%0/2
A
A= $res->{'message'}
Aresult
11
00
127 6 $res = $tx->res->json;
128 6 ▶ die $res->{error} if $res->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"error"} 1 5 6
129 5 return $res;
130 }
131  
132 11 ▶sub _api_sign_request {
Subroutine _api_sign_request: called CC=4 SCAR=13.9
133 11 my ($self, $args) = @_;
134 11 my @query;
135  
136 11 for my $k (@SIGNATURE_KEYS) {
137 88 ▶ next unless defined $args->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->{$k} 66 22 88
138 22 ▶ my $v = $k eq 'public_id' ? url_escape($args->{$k}, '^A-Za-z0-9\-._~/') : $args->{$k};
Branch: 1 branch
BranchTrueFalseTotal
$k eq 'public_id' ? : 8 14 22
139 22 push @query, "$k=$v";
140 }
141  
142 11 $query[-1] .= $self->api_secret;
143  
144 11 sha1_sum join '&', @query;
145 }
146  
147 6 ▶sub url_for {
Subroutine url_for: called CC=9 SCAR=22.5
Pod documented
148 6 my $self = shift;
149 6 ▶ my $public_id = shift or die 'Usage: $self->url_for($public_id, ...)';
Branch: 1 branch
BranchTrueFalseTotal
my $public_id = shift() or die "Usage: \$self->url_for(\$public_id, ...)" 6 0 6
150 6 ▶ my $args = shift || {};
Condition100%2/2
shift() || {}
l!l
42
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
151 6 ▶ my $format = $public_id =~ s/\.(\w+)// ? $1 : 'jpg';
Branch: 1 branch
BranchTrueFalseTotal
$public_id =~ s/\.(\w+)//u ? : 4 2 6
152 6 ▶ my $url = Mojo::URL->new(delete $args->{secure} ? $self->private_cdn : $self->_public_cdn);
Branch: 1 branch
BranchTrueFalseTotal
delete $args->{'secure'} ? : 0 6 6
153  
154 $url->path(
155 join '/',
156 30 ▶ grep {length} $self->cloud_name,
Condition75%3/4
$args->{'resource_type'} || 'image'
l!l
06
$args->{'type'} || 'upload'
l!l
15
MC/DC0%0/1
$args->{'resource_type'} || 'image'
$args->{'resource_type'}
MC/DC100%1/1
$args->{'type'} || 'upload'
$args->{'type'}
Truth table50%1/2
A
A= $args->{'resource_type'}
Aresult
11
00
Truth table100%2/2
A
A= $args->{'type'}
Aresult
11
00
157 $args->{resource_type} || 'image',
158 $args->{type} || 'upload',
159 join(',',
160 6 ▶ map { ($SHORTER{$_} || $_) . '_' . $args->{$_} }
Condition66%2/3
$Cloudinary::SHORTER{$_} || $_
l!l&&r!l&&!r
240
MC/DC0%0/2
$Cloudinary::SHORTER{$_} || $_
$Cloudinary::SHORTER{$_} $_
Truth table66%2/3
A || B
A= $Cloudinary::SHORTER{$_}B= $_
ABresult
1X1
011
000
161 6 ▶ grep { $_ ne 'resource_type' and $_ ne 'type' } sort keys %$args),
Condition66%2/3
$_ ne 'resource_type' && $_ ne 'type'
!ll&&!rl&&r
016
MC/DC50%1/2
$_ ne 'resource_type' && $_ ne 'type'
$_ ne 'resource_type' $_ ne 'type'
Truth table66%2/3
A && B
A= $_ ne 'resource_type'B= $_ ne 'type'
ABresult
0X0
100
111
162 "$public_id.$format",
163 );
164  
165 6 return $url;
166 }
167  
168 4 1;
169  
170 =encoding utf8
171  
172 =head1 NAME
173  
174 Cloudinary - Talk with cloudinary.com
175  
176 =head1 VERSION
177  
178 0.16
179  
180 =head1 DESCRIPTION
181  
182 This module lets you interface to L<http://cloudinary.com>.
183  
184 =head1 SYNOPSIS
185  
186 =head2 Standalone
187  
188 my $delay = Mojo::IOLoop->delay;
189 my $cloudinary = Cloudinary->new(cloud_name => "a", api_key => "b", api_secret => "c");
190  
191 $delay->begin;
192 $cloudinary->upload({file => {file => $path_to_file}}, sub {
193 my ($cloudinary, $res) = @_;
194 # ...
195 $delay->end;
196 },
197 });
198  
199 # let's you do multiple upload() in parallel
200 # just call $delay->begin once pr upload()
201 # and $delay->end in each callback given to upload()
202 $delay->wait;
203  
204 =head2 With mojolicious
205  
206 See L<Mojolicious::Plugin::Cloudinary>.
207  
208 =head2 Options
209  
210 As from 0.04 all methods support the short and long option, meaning
211 the examples below work the same:
212  
213 $self->url_for('billclinton.jpg' => { w => 50 });
214 $self->url_for('billclinton.jpg' => { width => 50 });
215  
216 =head2 url_for() examples
217  
218 $cloudinary->url_for('billclinton.jpg', { type => 'facebook' });
219 $cloudinary->url_for('billclinton.jpg', { type => 'twitter_name', h => 70, w => 100 });
220 $cloudinary->url_for('18913373.jpg', { type => 'twitter_name' });
221 $cloudinary->url_for('my-uploaded-image.jpg', { h => 50, w => 50 });
222 $cloudinary->url_for('myrawid', { resource_type => 'raw' });
223  
224 =head2 Aliases
225  
226 This module provides alias for the Cloudinary transformations:
227  
228 a = angle
229 b = background
230 c = crop
231 d = default_image
232 e = effect
233 f = fetch_format
234 g = gravity
235 h = height
236 l = overlay
237 p = prefix
238 q = quality
239 r = radius
240 t = named_transformation
241 w = width
242 x = x
243 y = y
244  
245 =head1 ATTRIBUTES
246  
247 =head2 cloud_name
248  
249 Your cloud name from L<https://cloudinary.com/console>
250  
251 =head2 api_key
252  
253 Your API key from L<https://cloudinary.com/console>
254  
255 =head2 api_secret
256  
257 Your API secret from L<https://cloudinary.com/console>
258  
259 =head2 private_cdn
260  
261 Your private CDN url from L<https://cloudinary.com/console>.
262  
263 =head1 METHODS
264  
265 =head2 upload
266  
267 $self->upload(
268 {
269 file => $binary_str | $url, # required
270 format => $str, # optional
271 public_id => $str, # optional
272 resource_type => $str, # image or raw. defaults to "image"
273 tags => ['foo', 'bar'], # optional
274 timestamp => $epoch, # time()
275 },
276 sub { my ($cloudinary, $res) = @_ }
277 );
278  
279 Will upload a file to L<http://cloudinary.com> using the parameters given
280 L</cloud_name>, L</api_key> and L</api_secret>. C<$res> in the callback
281 will be the json response from cloudinary:
282  
283 {
284 url => $str,
285 secure_url => $str,
286 public_id => $str,
287 version => $str,
288 width => $int, # only for images
289 height => $int, # only for images
290 }
291  
292 C<$res> on error can be either C<undef> if there was an issue
293 connecting/communicating with cloudinary or a an error data structure:
294  
295 {error => {message: $str}}
296  
297 The C<file> can be:
298  
299 =over 4
300  
301 =item * A hash
302  
303 { file => 'path/to/image' }
304  
305 =item * A L<Mojo::Upload> object.
306  
307 =item * A L<Mojo::Asset> object.
308  
309 =item * A URL
310  
311 =back
312  
313 C<res> in callbacks will be the JSON response from L<http://cloudinary.com>
314 as a hash ref. It may also be C<undef> if something went wrong with the
315 actual HTTP POST.
316  
317 See also L<https://cloudinary.com/documentation/upload_images> and
318 L<http://cloudinary.com/documentation/upload_images#raw_uploads>.
319  
320 =head2 destroy
321  
322 $self->destroy(
323 {
324 public_id => $public_id,
325 resource_type => $str, # image or raw. defaults to "image"
326 },
327 sub { my ($cloudinary, $res) = @_; }
328 );
329  
330 Will delete an image from cloudinary, identified by C<$public_id>.
331 The callback will be called when the image got deleted or if an error occur.
332  
333 On error, look for:
334  
335 {error => {message: $str}}
336  
337 See also L<https://cloudinary.com/documentation/upload_images#deleting_images>.
338  
339 =head2 url_for
340  
341 $url_obj = $self->url_for("$public_id.$format", \%args);
342  
343 This method will return a public URL to the image at L<http://cloudinary.com>.
344 It will use L</private_cdn> or the public CDN and L</cloud_name> to construct
345 the URL. The return value is a L<Mojo::URL> object.
346  
347 Example C<%args>:
348  
349 {
350 h => 150, # height of image
351 w => 100, # width of image
352 resource_type => $str, # image or raw. defaults to "image"
353 secure => $bool, # use private_cdn or public cdn
354 type => $str, # upload, facebook. defaults to "upload"
355 }
356  
357 See also L<http://cloudinary.com/documentation/upload_images#accessing_uploaded_images>
358 and L<http://cloudinary.com/documentation/image_transformations>.
359  
360 =head1 COPYRIGHT & LICENSE
361  
362 This library is free software. You can redistribute it and/or
363 modify it under the same terms as Perl itself.
364  
365 =head1 AUTHOR
366  
367 Jan Henning Thorsen - jhthorsen@cpan.org
368  
369 =cut
↑ Summary Mojolicious/Plugin/Cloudinary.pm »