API/Photo.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« API/Contact.pm ↑ Summary API/User.pm »
1 package Pikeo::API::Photo;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use base qw( Pikeo::API::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 use Pikeo::API::Comment;
9 use Pikeo::API::User;
10  
11 use Carp;
12 use Data::Dumper;
13  
14 =head1 NAME
15  
16 Pikeo::API::Photo - Abstraction of a pikeo photo
17  
18 =cut
19  
20  
21 sub _info_fields { qw( title is_family is_contact is_friend owner_username
22 original_file_size original width secret url_prefix
23 description height mime_type date_posted date_taken
24 owner_id title tags
25 ) }
26  
27 =head1 FUNCTIONS
28  
29 =head2 CONSTRUCTORS
30  
31 =head3 new( \%args )
32  
33 Returns a Pikeo::API::Contact object.
34  
35 Required args are:
36  
37 =over 4
38  
39 =item * api
40  
41 Pikeo::API object
42  
43 =item * from_xml
44  
45 XML::LibXML node containing the contacts definitions
46  
47 =back
48  
49 =cut
50 sub new {
51 my $class = shift;
52 my $params = shift;
53  
54 my $self = $class->SUPER::new($params);
55  
56 if ( $params->{id} ) {
57 $self->{id} = $params->{id};
58 $self->{_init} = 0;
59 return $self;
60 }
61 croak "Need an id";
62 }
63  
64 =head3 id()
65  
66 Photo id
67  
68 =cut
69 sub id { return shift->{id} }
70  
71 =head3 comments()
72  
73 List of Pikeo::API::Comment objects representing the photo comments.
74  
75 =cut
76 sub comments {
77 my $self = shift;
78 my $doc = $self->api->request_parsed( 'pikeo.comments.getList', {
79 photo_id => $self->{id},
80 } );
81 my $nodes = $doc->findnodes("response/value/array/comment");
82 my @comments = ();
83 for my $c ($nodes->get_nodelist()) {
84 next unless $c;
85 push @comments, Pikeo::API::Comment->new({
86 api => $self->api,
87 from_xml => $c,
88 });
89 }
90 return \@comments;
91 }
92  
93 =head3 setPrivacy(\%args)
94  
95 Change the privacy level of the photo.
96  
97 Required args:
98  
99 =over 4
100  
101 =item * access_type
102  
103 The "access_type" is chosen from :
104  
105 0 for private
106 2 for friends only
107 4 for family only
108 6 for friends and family only
109 7 for publicAuthentication is required.
110  
111 =back
112  
113 Optional args:
114  
115 =over 4
116  
117 =item * force_quit_group
118  
119 force_quit_group allow you to decide if photos can quit groups automatically.
120  
121 It happens when photo privacy change to a more private level that does not correspond to group accessibility.
122  
123 =back
124  
125 =cut
126  
127 sub setPrivacy {
128 my $self = shift;
129 my $params = shift;
130  
131 croak "missing param 'access_type'"
132 unless $params->{access_type};
133  
134 my $req_params = {
135 photo_id_list => "[".$self->id."]",
136 access_type => $params->{access_type},
137 };
138  
139 if ( $params->{force_quit_group} ) {
140 $req_params->{force_quit_group} = ( $params->{force_quit_group} ? 1 : 0 );
141 }
142  
143 $self->api->request_parsed( 'pikeo.photos.setPrivacy', $req_params );
144  
145 return 1;
146 }
147  
148 =head3 addComment(\%args)
149  
150 Adds a comment to the photo
151  
152 Required args:
153  
154 =over 4
155  
156 =item * text
157  
158 The text of the comment
159  
160 =back
161  
162 Optional args:
163  
164 =over 4
165  
166 =item * in_reply_to
167  
168 Pikeo::API::Comment parent comment
169  
170 =item * parent_id
171  
172 id of the parent comment
173  
174 =back
175  
176 =cut
177 sub addComment {
178 my $self = shift;
179 my $params = shift;
180  
181 croak "missing param 'text'"
182 unless $params->{text};
183
184 my $req_params = {
185 photo_id => $self->id,
186 text => $params->{text},
187 };
188  
189 if ( $params->{in_reply_to} ) {
190 croak "invalid param type 'in_reply_to'"
191 unless ref( $params->{in_reply_to} ) eq 'Pikeo::API::Comment';
192 $req_params->{parent_id} = $params->{in_reply_to}->id;
193 }
194 if ( $params->{parent_id} ) {
195 $req_params->{parent_id} = $params->{parent_id};
196 }
197  
198 my $d = $self->api->request_parsed( 'pikeo.comments.addComment', $req_params );
199 return $d->findvalue("//value");
200
201 }
202  
203 =head3 owner()
204  
205 Pikeo::API::User that owns the photo
206  
207 =cut
208  
209 sub owner {
210 my $self = shift;
211 return Pikeo::API::User->new({ id => $self->owner_id, api => $self->api });
212 }
213  
214 =head3 thumbnail_url($size)
215  
216 Returns the url for the photo thumbnail of the given size.
217  
218 Size can be one of the following:
219  
220 1016x762
221 500x400
222 200x150
223 96x72
224 75x75
225 50x50
226  
227 =cut
228  
229 sub thumbnail_url {
230 #urlPrefix/thumb/secret/thumbnail.jpg
231 my $self = shift;
232 my $size = shift;
233  
234 $self->_init unless $self->_init_done();
235  
236 return $self->url_prefix."thumb/".$self->secret.$size.".jpg";
237  
238 }
239  
240 =head3 original_url()
241  
242 Return full url to the original photo file
243  
244 =cut
245 sub original_url {
246 my $self = shift;
247 $self->_init unless $self->_init_done();
248  
249 return $self->url_prefix."upload/".$self->original;
250 }
251  
252 sub _init {
253 my $self = shift;
254 my $doc = $self->api->request_parsed( 'pikeo.photos.getInfo', {
255 'photo_id' => $self->{id},
256 } );
257 $self->_init_from_xml( $doc );
258 $self->{_init} = 1;
259 $self->{_dirty} = 0;
260 }
261  
262 sub _init_from_xml {
263 my $self = shift;
264 my $doc = shift;
265 my $nodes = $doc->findnodes("response/photo/*");
266 for ($nodes->get_nodelist()) {
267 next if $_->nodeName eq 'value';
268 $self->{$_->nodeName} = ( $_->to_literal eq 'null' ? undef : $_->to_literal );
269 }
270  
271 my @parsed_tags = ();
272 my $tags = $doc->findnodes("//tag");
273 for my $tag ($tags->get_nodelist()) {
274 push @parsed_tags, {
275 id => $tag->findvalue("./id"),
276 category => $tag->findvalue("./category"),
277 name => $tag->findvalue("./name"),
278 };
279 }
280 $self->{tags} = \@parsed_tags;
281  
282 }
283 =head3 title()
284  
285 =head4 is_family()
286  
287 =head4 is_contact()
288  
289 =head4 is_friend()
290  
291 =head4 owner_username()
292
293 =head3 original_file_size()
294  
295 =head3 original()
296  
297 =head3 secret()
298  
299 =head3 url_prefix()
300  
301 =head3 description()
302  
303 =head3 height()
304  
305 =head3 width()
306  
307 =head3 mime_type()
308  
309 =head3 date_posted()
310  
311 =head3 date_taken()
312  
313 =head3 owner_id()
314  
315 =head3 title()
316  
317 =head3 tags()
318  
319 =cut
320 1;
« API/Contact.pm ↑ Summary API/User.pm »