API/User/Logged.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« API/Photos.pm ↑ Summary API.pm »
1 package Pikeo::API::User::Logged;
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 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Pikeo::API::Photo;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Pikeo::API::Contact;
9  
10 use base qw( Pikeo::API::User );
11  
12 sub _info_fields { qw( real_name location avatar_url username email
13 profile_url birthday blog_url
14 ) }
15  
16 =head1 NAME
17  
18 Pikeo::API::User::Logged - Abstraction the logged in pikeo user
19  
20 =head1 DESCRIPTION
21  
22 This modules provides an interface to the logged in user in pikeo.
23  
24 This module inherits from Pikeo::API::User so all the methods of
25 Pikeo::API::User are also available.
26  
27 =head1 SYNOPSIS
28  
29 use Pikeo::API;
30 use Pikeo::API::User::Logged;
31  
32 # create an API object to maintain you session
33 # trough out the diferent calls
34 my $api = Pikeo::API->new({api_secret=>'asd', api_key=>'asdas'});
35 $api->login({ username => 'a', password => 'b' });
36
37 # Get the logged in user
38 my $user1 = Pikeo::API::User::Logged->new({ api => $api });
39  
40 #get the public photos
41 my $photos = $user->getPublicPhotos();
42  
43  
44 =head1 FUNCTIONS
45  
46 =head2 CONSTRUCTORS
47  
48 =head3 new( \%args )
49  
50 Returns a Pikeo::API::User object.
51  
52 Required args are:
53  
54 =over 4
55  
56 =item * api
57  
58 Pikeo::API object
59  
60 =cut
61  
62  
63 sub new {
64 my $class = shift;
65 my $params= shift;
66  
67 croak "need an api" unless $params->{api};
68  
69 croak "you must be logged in" unless $params->{api}->is_logged_in();
70  
71 my $self = bless { _init_done => 1, _api => $params->{api} }, $class;
72  
73 my $doc = $params->{api}->request_parsed('pikeo.people.getMyInfo',{});
74 for ( @{$doc->findnodes('/response/my_person/*')} ) {
75 $self->{$_->nodeName} = ( $_->to_literal eq 'null' ? undef : $_->to_literal );
76 }
77
78 croak "could not instantiate current user" unless $self->id;
79  
80 return $self;
81 }
82  
83 =back
84  
85 =head2 INSTANCE METHODS
86  
87 =head3 uploadPhoto(\%args)
88  
89 Upload one photo to the photo repository
90  
91 Returns the Pikeo::API::Photo object representing the uploaded photo
92  
93 Required args:
94  
95 =over 4
96  
97 =item * picture
98  
99 One file name containing the picture or a IO::File object pointing to the picture
100  
101 =back
102  
103 Optional args:
104  
105 =over 4
106  
107 =item * title
108  
109 The picture title
110  
111 =item * access_type
112  
113 The access type of the picture: 0 for PRIVATE, 2 for FRIEND, 4 for FAMILY, 6 for FRIEND AND FAMILY and 7 for PUBLIC. By default the picture is PUBLIC
114  
115 =item * title
116  
117 Title associated to the photo
118  
119 =item * description
120  
121 Description of the photo
122  
123 =item *
124  
125 A list of tags associated to the uploaded photo
126  
127 The list of tags is a reference to an array containing 'category' => 'tag' pairs.
128  
129 Example: [ 'where' => 'lisbon', 'who' => 'donald' , ... ]
130  
131 Tag categories can be: who, what or where.
132  
133 Only letters, figures and spaces are authorized in the tag itself.
134  
135 =back
136  
137 =cut
138  
139 sub uploadPhoto {
140 my $self = shift;
141 my $params = shift;
142  
143 my $req_params = {
144 title => $params->{title} || '',
145 access_type => (defined($params->{access_type}) ? ($params->{access_type}) : 7),
146 description => $params->{description} || '',
147 };
148  
149 croak "missing required param 'picture'"
150 unless $params->{picture};
151  
152 # Is this a file path?
153 if (ref($params->{picture}) eq '') {
154 # let's convert this to an IO::File
155 $params->{picture} = IO::File->new($params->{picture}, 'r');
156 }
157 elsif ( ref($params->{picture}) ne 'IO::File' ) {
158 croak "invalid param format for 'picture'";
159 }
160 $req_params->{picture} = encode_base64(join('',$params->{picture}->getlines()));
161  
162 if ( $params->{'tags'} ) {
163 my @tags = ();
164 while (scalar(@{$params->{'tags'}})) {
165 push @tags, shift(@{$params->{'tags'}}).":".shift(@{$params->{'tags'}});
166 }
167 $req_params->{tags} = join(",",@tags);
168 }
169  
170 my $doc = $self->api->request_parsed( 'pikeo.photos.upload', $req_params );
171 my $photo_id = $doc->findvalue("//value");
172 croak "Failed to upload photo, no photo id returned"
173 unless $photo_id;
174  
175 return Pikeo::API::Photo->new({ id => $photo_id, api => $self->api });
176 }
177  
178  
179 =head3 getMyCommentedPhotos()
180  
181 Return all the photos that the user owns that are commented.
182  
183 Return a list of Pikeo::API::Photo
184  
185 =cut
186  
187 sub getMyCommentedPhotos {
188 my $self = shift;
189 my $params = shift;
190  
191 my $req_params = {};
192
193 if ( $params->{num_items} ) {
194 $req_params->{num_items} = $params->{num_items};
195 }
196  
197 my $doc = $self->api->request_parsed( 'pikeo.comments.getMyCommentedPhotos', $req_params );
198  
199 return $self->_photos_from_xml({ xml => [$doc->findnodes('//photo')] });
200 }
201  
202 =head3 getPicturesCommentedByMe()
203  
204 Returns all the photos that were commented by the user.
205  
206 Return a list of Pikeo::API::Photo
207  
208 =cut
209  
210 sub getPicturesCommentedByMe {
211 my $self = shift;
212 my $params = shift;
213  
214 my $req_params = {};
215
216 if ( $params->{num_items} ) {
217 $req_params->{num_items} = $params->{num_items};
218 }
219  
220 my $doc = $self->api->request_parsed( 'pikeo.comments.getPicturesCommentedByMe', $req_params );
221  
222 return $self->_photos_from_xml({ xml => [$doc->findnodes('//photo')] });
223 }
224  
225 =head3 getContactsList()
226  
227 The function will get the contacts of the logged user.
228  
229 Returns a list of Pikeo::API::Contact objects.
230  
231 =cut
232  
233 sub getContactsList {
234 my $self = shift;
235 my $params = shift;
236  
237 my $doc = $self->api->request_parsed( 'pikeo.contacts.getList', {} );
238  
239 my @contacts = ();
240 for my $contact (@{ $doc->findnodes('//contact') }) {
241 push @contacts, Pikeo::API::Contact->new({api=>$self->api, from_xml=>$contact});
242 }
243 return \@contacts;
244 }
245  
246  
247 sub _init { shift->{_init_done} = 1 }
248  
249 1;
« API/Photos.pm ↑ Summary API.pm »