API/User.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/Photo.pm ↑ Summary
1 package Pikeo::API::User;
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 Carp;
9 use Data::Dumper;
10 use Pikeo::API::Album;
11  
12 =head1 NAME
13  
14 Pikeo::API::User - Abstraction of a pikeo user/person
15  
16 =cut
17  
18 sub _info_fields { qw( username profile_url avatar_url location ) }
19  
20 =head1 SYNOPSIS
21  
22 use Pikeo::API;
23 use Pikeo::API::User;
24  
25 # create an API object to maintain you session
26 # trough out the diferent calls
27 my $api = Pikeo::API->new({api_secret=>'asd', api_key=>'asdas'});
28
29 # Get a user by id...
30 my $user1 = Pikeo::API::User->new({ api => $api, id=>1 });
31  
32 # .. or get a user by username
33 my $user2 = Pikeo::API::User->new({ api => $api, username=>'foo' });
34  
35 #get the public photos
36 my $photos = $user2->getPublicPhotos();
37  
38 =head1 FUNCTIONS
39  
40 =head2 CONSTRUCTORS
41  
42 =head3 new( \%args )
43  
44 Returns a Pikeo::API::User object.
45  
46 Required args are:
47  
48 =over 4
49  
50 =item * api
51  
52 Pikeo::API object
53  
54 =item * username or id
55  
56 Id or username of the user.
57  
58 =cut
59  
60 sub new {
61 my $class = shift;
62 my $params = shift;
63  
64 my $self = $class->SUPER::new($params);
65  
66 if ( $params->{id} ) {
67 $self->{id} = $params->{id};
68 $self->{_init} = 0;
69 return $self;
70 }
71 elsif ($params->{username}){
72 my $doc = $self->api->request_parsed('pikeo.people.findByUsername',
73 { username => $params->{username} }
74 );
75 $self->{id} = $doc->findvalue("//value") || croak "unkown user";
76 $self->{_init} = 0;
77 return $self;
78  
79 }
80  
81 croak "Need an id or a username";
82 }
83  
84 =back
85  
86 =head2 INSTANCE METHODS
87  
88 =head3 getPublicPhotos(\%args)
89  
90 Return a list of Pikeo::API::Photo owned by the user
91 and marked as public.
92  
93 =cut
94  
95 sub getPublicPhotos {
96 my $self = shift;
97 my $params = shift;
98  
99 my $req_params = {
100 user_id => $self->id,
101 };
102 if ( $params->{'num_items'} ) {
103 $req_params->{'num_items'} = $params->{'num_items'};
104 }
105  
106 my $doc = $self->api->request_parsed( 'pikeo.people.getPublicPhotos', $req_params );
107  
108 return $self->_photos_from_xml({ xml => [$doc->findnodes('//photo')] });
109 }
110  
111 =head3 getContactsPublicPhotos(\%args)
112  
113 Return a list of Pikeo::API::Photo owned by contacts
114 of the user and marked as public
115  
116 =cut
117  
118 sub getContactsPublicPhotos {
119 my $self = shift;
120 my $params = shift;
121  
122 my $req_params = {
123 user_id => $self->id,
124 };
125 if ( $params->{'num_items'} ) {
126 $req_params->{'num_items'} = $params->{'num_items'};
127 }
128  
129 my $doc = $self->api->request_parsed( 'pikeo.photos.getContactsPublicPhotos', $req_params );
130  
131 return $self->_photos_from_xml({ xml => [$doc->findnodes('//photo')] });
132 }
133  
134  
135 =head3 getUserPhotos(\%args)
136  
137 Return a list of Pikeo::API::Photo containing all the
138 photos of the user.
139  
140 =cut
141 sub getUserPhotos {
142 my $self = shift;
143 my $params = shift;
144  
145 my $req_params = {
146 user_id => $self->id,
147 };
148 if ( $params->{'num_items'} ) {
149 $req_params->{'num_items'} = $params->{'num_items'};
150 }
151  
152 my $doc = $self->api->request_parsed( 'pikeo.photos.getUserPhotos', $req_params );
153  
154 return $self->_photos_from_xml({ xml => [$doc->findnodes('//photo')] });
155 }
156  
157 =head3 getAlbumsList()
158  
159 Return a list of Pikeo::API::Album owned by the user
160  
161 =cut
162 sub getAlbumsList {
163 my $self = shift;
164 my $params = {};
165  
166 my $albums = $self->api->request_parsed( 'pikeo.people.getInfo',
167 {
168 'user_id' => $self->id,
169 } );
170  
171 my @a = ();
172 for my $album_xml ( @{$albums->findnodes("//album")} ) {
173 push @a, Pikeo::API::Album->new({ api=>$self->api,
174 from_xml => $album_xml });
175 }
176 return \@a;
177  
178 }
179  
180 =head3 username()
181  
182 Returns the user username
183  
184 =head3 profile_url()
185  
186 Returns the user profile url
187  
188 =head3 avatar_url()
189  
190 Returns the user avatar url
191  
192 =head3 location()
193  
194 Returns the user location
195  
196 =head3 id()
197  
198 Returns the user id
199  
200 =cut
201
202 sub id { return shift->{id} }
203  
204 sub _init {
205 my $self = shift;
206 my $doc = $self->api->request_parsed( 'pikeo.people.getInfo', {
207 'user_id' => $self->id,
208 } );
209 $self->_init_from_xml( [$doc->findnodes("response/person/*")] );
210 $self->{_init} = 1;
211 $self->{_dirty} = 0;
212 }
213  
214 sub _init_from_xml {
215 my $self = shift;
216 my $nodes = shift;
217 for ( @$nodes ) {
218 $self->{$_->nodeName} = ( $_->to_literal eq 'null' ? undef : $_->to_literal );
219 }
220 }
221  
222 1;
« API/Photo.pm ↑ Summary