File Coverage

blib/lib/Mojolicious/Plugin/Cloudinary.pm
Criterion Covered Total %
statement 46 49 93.8
branch 5 10 50.0
condition 8 18 44.4
subroutine 12 12 100.0
pod 1 1 100.0
total 72 90 80.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Cloudinary;
2 2     2   255611 use Mojo::Base -base;
  2         9  
  2         12  
3 2     2   269 use File::Basename;
  2         4  
  2         121  
4 2     2   416 use Mojo::UserAgent;
  2         81796  
  2         18  
5 2     2   61 use Mojo::Util qw(sha1_sum url_escape);
  2         4  
  2         91  
6 2     2   11 use Scalar::Util 'weaken';
  2         3  
  2         73  
7 2     2   8 use base qw(Cloudinary Mojolicious::Plugin);
  2         3  
  2         768  
8              
9             our $VERSION = 0.0402; # just need something higher than the previous version
10              
11             has js_image => '/image/blank.png';
12              
13             sub register {
14 1     1 1 31 my ($self, $app, $config) = @_;
15              
16 1 50       2 for my $k (keys %{$config || {}}) {
  1         4  
17 1 50       5 $self->$k($config->{$k}) if exists $config->{$k};
18             }
19              
20 1         14 $self->_ua($app->ua);
21              
22             $app->helper(
23             cloudinary_upload => sub {
24 1     1   12743 my $c = shift;
25 1         11 $self->upload(@_);
26             }
27 1         78 );
28             $app->helper(
29             cloudinary_destroy => sub {
30 1     1   66403 my $c = shift;
31 1         17 $self->destroy(@_);
32             }
33 1         27 );
34             $app->helper(
35             cloudinary_url_for => sub {
36 1     1   38122 my ($c, $public_id, $args) = @_;
37 1   50     6 my $scheme = $c->req->url->scheme || '';
38              
39 1 50 33     41 if (not defined $args->{secure} and $scheme eq 'https') {
40 0         0 $args->{secure} = 1;
41             }
42              
43 1         14 return $self->url_for($public_id, $args);
44             }
45 1         14 );
46             $app->helper(
47             cloudinary_image => sub {
48 1     1   16597 my ($c, $public_id, $args, $image_args) = @_;
49 1   50     5 my $scheme = $c->req->url->scheme || '';
50              
51 1 50 33     43 if (not defined $args->{secure} and $scheme eq 'https') {
52 0         0 $args->{secure} = 1;
53             }
54              
55 1         16 return $c->image($self->url_for($public_id, $args), alt => $public_id, %$image_args);
56             }
57 1         13 );
58             $app->helper(
59             cloudinary_js_image => sub {
60 1     1   13154 my ($c, $public_id, $args) = @_;
61 1   50     3 my $scheme = $c->req->url->scheme || '';
62              
63 1 50 33     23 if (not defined $args->{secure} and $scheme eq 'https') {
64 0         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   66     6 my $k = $Cloudinary::LONGER{$_} || $_;
  2         14  
74 2         9 ("data-$k" => $args->{$_})
75             } keys %$args
76             );
77             }
78 1         14 );
79             }
80              
81             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 module as helpers in
92             your L web application. See L 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. The default is "/image/blank.png".
122              
123             =head1 HELPERS
124              
125             =head2 cloudinary_upload
126              
127             See L.
128              
129             =head2 cloudinary_destroy
130              
131             See L.
132              
133             =head2 cloudinary_url_for
134              
135             See L.
136              
137             =head2 cloudinary_image
138              
139             $str = $c->cloudinary_image($public_id, $url_for_args, $image_args);
140              
141             This will use L to create an image
142             tag where "src" is set to a cloudinary image. C<$url_for_args> are passed
143             on to L and C<$image_args> are passed on to
144             L.
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, except it creates an image which can
151             handled by the cloudinary jQuery plugin which you can read more about here:
152             L
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            
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 in the L application.
178              
179             =head1 SEE ALSO
180              
181             L.
182              
183             =cut