File Coverage

blib/lib/Flickr/API/Upload.pm
Criterion Covered Total %
statement 27 95 28.4
branch 0 14 0.0
condition 0 7 0.0
subroutine 9 11 81.8
pod 1 2 50.0
total 37 129 28.6


line stmt bran cond sub pod time code
1             package Flickr::API::Upload;
2              
3 1     1   728 use strict;
  1         3  
  1         43  
4 1     1   6 use warnings;
  1         2  
  1         128  
5 1     1   656 use HTTP::Request::Common;
  1         3507  
  1         96  
6 1     1   8 use Net::OAuth;
  1         2  
  1         26  
7 1     1   8 use URI;
  1         1  
  1         22  
8 1     1   5 use Carp;
  1         2  
  1         68  
9 1     1   6 use Digest::MD5 qw(md5_hex);
  1         2  
  1         67  
10 1     1   7 use Encode qw(encode_utf8);
  1         3  
  1         52  
11              
12 1     1   6 use parent qw(HTTP::Request);
  1         1  
  1         25  
13              
14             our $VERSION = '1.28';
15              
16              
17             sub new {
18              
19 0     0 1   my ($class, $args) = @_;
20 0           my $self;
21              
22 0           my @params = (
23             "title",
24             "description",
25             "tags",
26             "is_public",
27             "is_friend",
28             "is_family",
29             "safety_level",
30             "content_type",
31             "hidden"
32             );
33              
34 0           my $photo = {};
35              
36 0 0 0       unless ( -f $args->{photo}->{photo} && -r $args->{photo}->{photo} ) {
37              
38 0           carp "\nPhoto: ",$args->{photo}->{photo},", is not a readable file.\n";
39 0           return;
40              
41             }
42              
43             #
44             # make hashref of valid arguments for an upload, ignore extraneous
45             #
46 0           $photo->{photo} = $args->{photo}->{photo};
47 0   0       $photo->{async} = $args->{photo}->{async} || '0';
48              
49 0           for my $param (@params) {
50              
51 0 0         if (defined($args->{photo}->{$param})) { $photo->{$param} = $args->{photo}->{$param}; }
  0            
52              
53             }
54              
55 0           chomp $photo->{'description'};
56 0           delete($args->{photo});
57              
58 0           $args->{api}->{request_method} = 'POST'; # required to be POST
59              
60 0 0 0       if (($args->{api_type} || '') eq 'oauth') {
61              
62 0           $args->{api}->{extra_params} = $photo;
63              
64 0           $Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
65              
66 0           my $orequest = Net::OAuth->request('protected resource')->new(%{$args->{api}});
  0            
67 0           $orequest->sign();
68              
69 0           my $buzo = $orequest->to_hash();
70              
71 0           my @msgarr;
72              
73 0           for my $param (sort keys %{$buzo}) {
  0            
74 0           push(@msgarr,$param);
75 0           push(@msgarr,$buzo->{$param});
76             }
77              
78 0           push(@msgarr,'photo');
79 0           push(@msgarr, [$photo->{photo}]);
80              
81             $self = POST $args->{api}->{request_url},
82 0           'Content-Type' => 'form-data',
83             'Content' => \@msgarr;
84              
85              
86              
87             } # if oauth
88             else {
89              
90 0           my $pixfile = $photo->{photo};
91 0           delete $photo->{photo};
92              
93 0           $photo->{api_key} = $args->{api}->{api_key};
94 0           $photo->{auth_token} = $args->{api}->{token};
95 0           my $sig = $args->{api}->{api_secret};
96              
97 0           foreach my $key (sort {$a cmp $b} keys %{$photo}) {
  0            
  0            
98              
99 0 0         my $value = (defined($photo->{$key})) ? $photo->{$key} : "";
100 0           $sig .= $key . $value;
101             }
102              
103 0 0         if ($args->{api}->{unicode}) {
104              
105 0           $photo->{api_sig} = md5_hex(encode_utf8($sig));
106              
107             }
108             else {
109              
110 0           $photo->{api_sig} = md5_hex($sig);
111             }
112              
113 0           my @msgarr;
114              
115              
116 0           for my $param (sort keys %{$photo}) {
  0            
117 0           push(@msgarr,$param);
118 0           push(@msgarr,$photo->{$param});
119             }
120 0           push(@msgarr,'photo');
121 0           push(@msgarr, [$pixfile]);
122              
123             $self = POST $args->{api}->{request_url},
124 0           'Content-Type' => 'form-data',
125             'Content' => \@msgarr;
126              
127             } # else i'm flickr
128              
129 0           bless $self, $class;
130              
131 0           return $self;
132              
133             }
134              
135              
136              
137             sub encode_args {
138 0     0 0   my ($self) = @_;
139              
140 0           my $content;
141 0           my $url = URI->new('https:');
142              
143 0 0         if ($self->{unicode}){
144 0           for my $k(keys %{$self->{api_args}}){
  0            
145 0           $self->{api_args}->{$k} = encode_utf8($self->{api_args}->{$k});
146             }
147             }
148 0           $url->query_form(%{$self->{api_args}});
  0            
149 0           $content = $url->query;
150              
151              
152 0           $self->header('Content-Type' => 'application/x-www-form-urlencoded');
153 0 0         if (defined($content)) {
154 0           $self->header('Content-Length' => length($content));
155 0           $self->content($content);
156             }
157 0           return;
158             }
159              
160             1;
161              
162             __END__