blib/lib/Image/Imgur.pm

filter: Statementstmt 76.1% 32 / 42 Branchbran 35.7% 5 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 1 totaltotal 67.6% 44 / 65 CC 8 SCAR 13.1
CC
8
Cov
66%
_read_file
6.0
upload
5.1
_upload
4.6
CRAP 3.7
↑ Summary
1 package Image::Imgur;
2 # Imgur.pm
3 # simple perl module for uploading pics to imgur.com
4  
5 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moose; # a Mouse is fine too
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 has 'key' => (is => 'rw', isa => 'Str');
12  
13 # errors:
14 # 0 -- no api key
15 # -1 -- problem with the file
16 1 ▶sub upload {
Subroutine upload: called CC=4 SCAR=16.3
Pod undocumented
17 1 my $self = shift;
18 1 my $image_path = shift;
19 1 ▶ return 0 unless($self->key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->key 0 1 1
20 1 my $res;
21 1 ▶ if($image_path =~ /http:\/\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($image_path =~ m[http://]) { } 1 0 1
22 1 $res = $image_path;
23 } else {
24 0 my $fh;
25 0 ▶ open $fh,"<", $image_path or return -1;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $image_path or return -1 0 0 0
26 0 $res = _read_file($image_path);
27 }
28 1 $res = $self->_upload($res);
29 1 return $res;
30 }
31  
32  
33 # base64'd image
34 0 ▶sub _read_file {
Subroutine _read_file: not called CC=2 SCAR=17.9
35 0 my $fname = shift;
36 0 my $fh;
37 0 ▶ open $fh, "<", $fname or return -1;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $fname or return -1 0 0 0
38 0 binmode $fh;
39 0 return encode_base64(join("" => <$fh>));
40 }
41  
42 # errors:
43 # 1000 No image selected
44 # 1001 Image failed to upload
45 # 1002 Image larger than 10MB
46 # 1003 Invalid image type or URL
47 # 1004 Invalid API Key
48 # 1005 Upload failed during process
49 # 1006 Upload failed during the copy process
50 # 1007 Upload failed during thumbnail process
51 # 1008 Upload limit reached
52 # 1009 Animated GIF is larger than 2MB
53 # 1010 Animated PNG is larger than 2MB
54 # 1011 Invalid URL
55 # 1012 Could not download the image from that URL
56 # 9000 Invalid API request
57 # 9001 Invalid response format
58 # -3 Something is really wrong...
59 # else: image url
60 1 ▶sub _upload {
Subroutine _upload: called CC=4 SCAR=15.2
61 1 my $self = shift;
62 1 my $image = shift;
63 1 ▶ return undef unless($image);
Branch: 1 branch
BranchTrueFalseTotal
unless $image 0 1 1
64 1 my $user_a = LWP::UserAgent->new(agent => "Perl");
65 1 my $res = $user_a->post('http://imgur.com/api/upload.xml', ['image' => $image, 'key' => $self->key]);
66 1 ▶ if($res->content =~ /<original_image>(.*?)<\/original_image>/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res->content =~ m[<original_image>(.*?)</original_image>]) { } 0 1 1
elsif ($res->content =~ m[<error_code>(\d+)</error_code>]) { } 0 1 1
67 0 return $1;
68 } elsif ($res->content =~ /<error_code>(\d+)<\/error_code>/) {
69 0 return $1;
70 } else {
71 1 return -3;
72 }
73 }
74  
75 1 1;
76 __END__
↑ Summary