Mojolicious/Plugin/Cloudinary.pm

filter: Statementstmt 94.2% 49 / 52 Branchbran 50.0% 4 / 8 Conditioncond 45.0% 9 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 71.4% 75 / 105 CC 14 SCAR 13.6
CC
14
Cov
78%
__ANON__
6.1
__ANON__
5.2
__ANON__
5.2
CRAP 3.9
« Cloudinary.pm ↑ Summary
1 package Mojolicious::Plugin::Cloudinary;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::Util qw(sha1_sum url_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use base qw(Cloudinary Mojolicious::Plugin);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = 0.0402; # just need something higher than the previous version
10  
11 2 has js_image => '/image/blank.png';
12  
13 1 ▶sub register {
Subroutine register: called CC=4 SCAR=14.9
Pod documented
14 1 my ($self, $app, $config) = @_;
15  
16 1 ▶ for my $k (keys %{$config || {}}) {
Condition50%1/2
$config || {}
l!l
10
MC/DC0%0/1
$config || {}
$config
Truth table50%1/2
A
A= $config
Aresult
11
00
17 1 ▶ $self->$k($config->{$k}) if exists $config->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $config->{$k} 1 0 1
18 }
19  
20 1 $self->_ua($app->ua);
21  
22 $app->helper(
23 1 ▶ cloudinary_upload => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1 my $c = shift;
25 1 $self->upload(@_);
26 }
27 1 );
28 $app->helper(
29 1 ▶ cloudinary_destroy => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 my $c = shift;
31 1 $self->destroy(@_);
32 }
33 1 );
34 $app->helper(
35 1 ▶ cloudinary_url_for => sub {
Subroutine __ANON__: called CC=4 SCAR=16.4
36 1 my ($c, $public_id, $args) = @_;
37 1 ▶ my $scheme = $c->req->url->scheme || '';
Condition50%1/2
$c->req->url->scheme || ''
l!l
01
MC/DC0%0/1
$c->req->url->scheme || ''
$c->req->url->scheme
Truth table50%1/2
A
A= $c->req->url->scheme
Aresult
11
00
38  
39 1 ▶ if (not defined $args->{secure} and $scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args->{"secure"} and $scheme eq "https") 0 1 1
Condition33%1/3
not defined $args->{"secure"} and $scheme eq "https"
!ll&&!rl&&r
010
MC/DC0%0/2
not defined $args->{"secure"} and $scheme eq "https"
not defined $args->{"secure"} $scheme eq "https"
Truth table33%1/3
not(A) and B
A= not defined $args->{"secure"}B= $scheme eq "https"
ABresult
0X0
100
111
40 0 $args->{secure} = 1;
41 }
42  
43 1 return $self->url_for($public_id, $args);
44 }
45 1 );
46 $app->helper(
47 1 ▶ cloudinary_image => sub {
Subroutine __ANON__: called CC=4 SCAR=16.4
48 1 my ($c, $public_id, $args, $image_args) = @_;
49 1 ▶ my $scheme = $c->req->url->scheme || '';
Condition50%1/2
$c->req->url->scheme || ''
l!l
01
MC/DC0%0/1
$c->req->url->scheme || ''
$c->req->url->scheme
Truth table50%1/2
A
A= $c->req->url->scheme
Aresult
11
00
50  
51 1 ▶ if (not defined $args->{secure} and $scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args->{"secure"} and $scheme eq "https") 0 1 1
Condition33%1/3
not defined $args->{"secure"} and $scheme eq "https"
!ll&&!rl&&r
010
MC/DC0%0/2
not defined $args->{"secure"} and $scheme eq "https"
not defined $args->{"secure"} $scheme eq "https"
Truth table33%1/3
not(A) and B
A= not defined $args->{"secure"}B= $scheme eq "https"
ABresult
0X0
100
111
52 0 $args->{secure} = 1;
53 }
54  
55 1 return $c->image($self->url_for($public_id, $args), alt => $public_id, %$image_args);
56 }
57 1 );
58 $app->helper(
59 1 ▶ cloudinary_js_image => sub {
Subroutine __ANON__: called CC=5 SCAR=18.1
60 1 my ($c, $public_id, $args) = @_;
61 1 ▶ my $scheme = $c->req->url->scheme || '';
Condition50%1/2
$c->req->url->scheme || ''
l!l
01
MC/DC0%0/1
$c->req->url->scheme || ''
$c->req->url->scheme
Truth table50%1/2
A
A= $c->req->url->scheme
Aresult
11
00
62  
63 1 ▶ if (not defined $args->{secure} and $scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args->{"secure"} and $scheme eq "https") 0 1 1
Condition33%1/3
not defined $args->{"secure"} and $scheme eq "https"
!ll&&!rl&&r
010
MC/DC0%0/2
not defined $args->{"secure"} and $scheme eq "https"
not defined $args->{"secure"} $scheme eq "https"
Truth table33%1/3
not(A) and B
A= not defined $args->{"secure"}B= $scheme eq "https"
ABresult
0X0
100
111
64 0 $args->{secure} = 1;
65 }
66  
67 return $c->image(
68 $self->js_image,
69 'alt' => $public_id,
70 'class' => 'cloudinary-js-image',
71 'data-src' => $public_id,
72 map {
73 1 ▶ my $k = $Cloudinary::LONGER{$_} || $_;
Condition66%2/3
$Cloudinary::LONGER{$_} || $_
l!l&&r!l&&!r
110
MC/DC0%0/2
$Cloudinary::LONGER{$_} || $_
$Cloudinary::LONGER{$_} $_
Truth table66%2/3
A || B
A= $Cloudinary::LONGER{$_}B= $_
ABresult
1X1
011
000
74 2 ("data-$k" => $args->{$_})
75 } keys %$args
76 );
77 }
78 1 );
79 }
80  
81 2 1;
82  
83 =encoding utf8
84  
85 =head1 NAME
86  
87 Mojolicious::Plugin::Cloudinary - Talk with cloudinary.com
88  
89 =head1 DESCRIPTION
90  
91 This register the methods from the L<Cloudinary> module as helpers in
92 your L<Mojolicious> web application. See L</HELPERS> for details.
93  
94 =head1 SYNOPSIS
95  
96 use Mojolicious::Lite;
97  
98 plugin cloudinary => {cloud_name => $str, api_key => $str, api_secret => $str};
99  
100 post "/upload" => sub {
101 my $c = shift;
102  
103 $c->delay(
104 sub {
105 my ($delay) = @_;
106 $c->cloudinary_upload({file => $c->param("upload_param")}, $delay->begin);
107 },
108 sub {
109 my ($delay, $res, $tx) = @_;
110 return $c->render(json => $res) if $res;
111 return $c->render_exception;
112 },
113 );
114 }
115  
116 =head1 ATTRIBUTES
117  
118 =head2 js_image
119  
120 This string will be used as the image src for images constructed by
121 L</cloudinary_js_image>. The default is "/image/blank.png".
122  
123 =head1 HELPERS
124  
125 =head2 cloudinary_upload
126  
127 See L<Cloudinary/upload>.
128  
129 =head2 cloudinary_destroy
130  
131 See L<Cloudinary/destroy>.
132  
133 =head2 cloudinary_url_for
134  
135 See L<Cloudinary/url_for>.
136  
137 =head2 cloudinary_image
138  
139 $str = $c->cloudinary_image($public_id, $url_for_args, $image_args);
140  
141 This will use L<Mojolicious::Plugin::TagHelpers/image> to create an image
142 tag where "src" is set to a cloudinary image. C<$url_for_args> are passed
143 on to L</url_for> and C<$image_args> are passed on to
144 L<Mojolicious::Plugin::TagHelpers/image>.
145  
146 =head2 cloudinary_js_image
147  
148 $str = $c->cloudinary_js_image($public_id, $url_for_args);
149  
150 About the same as L</cloudinary_image>, except it creates an image which can
151 handled by the cloudinary jQuery plugin which you can read more about here:
152 L<http://cloudinary.com/blog/cloudinary_s_jquery_library_for_embedding_and_transforming_images>
153  
154 Example usage:
155  
156 $c->cloudinary_js_image(1234567890 =>
157 {width => 115, height => 115, crop => "thumb", gravity => "faces", radius => "20"});
158  
159 ...will produce:
160  
161 <img src="/image/blank.png"
162 class="cloudinary-js-image"
163 alt="1234567890"
164 data-src="1234567890"
165 data-width="115"
166 data-height="135"
167 data-crop="thumb"
168 data-gravity="faces"
169 data-radius="20">
170  
171 Note: The "class" and "alt" attributes are fixed for now.
172  
173 =head1 METHODS
174  
175 =head2 register
176  
177 Will register the L</HELPERS> in the L<Mojolicious> application.
178  
179 =head1 SEE ALSO
180  
181 L<Cloudinary>.
182  
183 =cut
« Cloudinary.pm ↑ Summary