Mojolicious::Plugin::Cloudinary - Talk with cloudinary.com
0.01
This module lets you interface to http://cloudinary.com. Its primary target is to be a Mojolicious plugin, but it can also be used as a generic module - just skip calling "register".
package MyWebApp; use Mojo::Base 'Mojolicious'; sub startup { my $self = shift; $self->plugin('Mojolicious::Plugin::Cloudinary', { cloud_name => $str, api_key => $str, api_secret => $str, }); } package MyWebApp::SomeController; sub upload { my $self = shift; $self->render_later; $self->cloudinary_upload({ file => $self->param('upload_param'), on_success => sub { my $res = shift; $self->render_json($res); }, on_error => sub { my $res = shift || { error => 'Unknown' }; $self->render_json($res); }, }); }
my $delay = Mojo::IOLoop->delay; my $cloudinary = Mojolicious::Plugin::Cloudinary->new( cloud_name => '...', api_key => '...', api_secret => '...', ); $delay->begin; $cloudinary->upload({ file => { file => $path_to_file }, on_success => sub { # ... $delay->end; }, on_error => sub { # ... $delay->end; }, }); # let's you do multiple upload() in parallel # just call $delay->begin once pr upload() # and $delay->end in each on_xxx callback $delay->wait;
Your cloud name from https://cloudinary.com/console
Your API key from https://cloudinary.com/console
Your API secret from https://cloudinary.com/console
Default is http://api.cloudinary.com/v1_1.
Your private CDN url from http://api.cloudinary.com/v1_1.
Default is http://res.cloudinary.com.
$self->upload({ file => $binary_str|$url, # required timestamp => $epoch, # time() public_id => $str, # optional format => $str, # optional tags => ['foo', 'bar'], # optional on_success => sub { my($res) = @_; # ... }, on_error => sub { my($res, $tx) = @_; # ... }, });
Will upload a file to http://cloudinary.com using the parameters given "cloud_name" "api_key" and "api_secret". The file can be:
file
A hash
{ file => 'path/to/image' }
A Mojo::Upload object.
A Mojo::Asset object.
A URL
res in callbacks will be the JSON response from http://cloudinary.com as a hash ref. It may also be undef if something went wrong with the actual HTTP POST.
res
undef
See also https://cloudinary.com/documentation/upload_images.
$url_obj = $self->url_for("$public_id.$format", \%args);
This method will return a public URL to the image at http://cloudinary.com. It will use "private_cdn" or "public_cdn" and "cloud_name" to construct the URL. The return value is a Mojo::URL object.
Example %args:
%args
{ w => 100, # width of image h => 150, # height of image secure => $bool, # use private_cdn or public_cdn }
Adds the helpers to your controller:
cloudinary_upload
See "upload".
cloudinary_url_for
See "url_for".
See Oppstarter
Jan Henning Thorsen - jan.henning@oppstarter.no
To install Mojolicious::Plugin::Cloudinary, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Cloudinary
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Cloudinary
For more information on module installation, please visit the detailed CPAN module installation guide.