API/Album.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.pm ↑ Summary API/Comment.pm »
1 package Pikeo::API::Album;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶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::User;
11  
12 sub _info_fields {qw( id owner_id owner_username title
13 description cover_url )}
14  
15 =head1 NAME
16  
17 Pikeo::API::Album - Abstraction of a pikeo photo album
18  
19 =head1 SYNOPSIS
20  
21 use Pikeo::API;
22 use Pikeo::API::User;
23 use Pikeo::API::Album;
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 $user = Pikeo::API::User->new({ api => $api, id=>1 });
31 # get the albums for this user
32 my $albums = $user->getAlbumsList;
33 # get one album photos
34 my $album = $albums->[0];
35 my $photos = $album->getPhotos();
36  
37 # .. or get a album by id
38 my $other_album = Pikeo::API::Album->new({ api => $api, id=>999 });
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 Optional args are:
55  
56 =item * from_xml
57  
58 XML::LibXML node containing the album details
59  
60 =item * id
61  
62 id of the album
63  
64 =cut
65  
66 sub new {
67 my $class = shift;
68 my $params = shift;
69  
70 my $self = $class->SUPER::new($params);
71  
72 if ( $params->{from_xml} ) {
73 $self->_init_from_xml( $params->{from_xml} );
74 return $self;
75 }
76 if ( $params->{id} ) {
77 $self->{id} = $params->{id};
78 return $self;
79 }
80 croak "Need an xml object";
81 }
82  
83 =back
84  
85 =head2 INSTANCE METHODS
86  
87 =head3 owner()
88  
89 Pikeo::API::User that owns the album
90  
91 =cut
92  
93 sub owner {
94 my $self = shift;
95 return Pikeo::API::User->new({ id => $self->owner_id, api => $self->api });
96 }
97  
98 =head3 id()
99  
100 Returns the album id
101  
102 =cut
103  
104 sub id { return shift->{id} }
105  
106 =head3 getPhotos()
107  
108 Return a list of Pikeo::API::Photo with all the photos in the album
109  
110 =cut
111 sub getPhotos {
112 my $self = shift;
113 my $params = shift;
114  
115 my $req_params = { album_id => $self->id };
116  
117 if ( $params->{'num_items'} ) {
118 $req_params->{'num_items'} = $params->{'num_items'};
119 }
120  
121 my $doc = $self->api->request_parsed( 'pikeo.albums.getPhotos', $req_params );
122  
123 return $self->_photos_from_xml({ xml => [$doc->findnodes('//photo')] });
124 }
125  
126 sub _init_from_xml {
127 my $self = shift;
128 my $doc = shift;
129 my $nodes = $doc->findnodes("./*");
130 for ($nodes->get_nodelist()) {
131 $self->{$_->nodeName} = ( $_->to_literal eq 'null' ? undef : $_->to_literal );
132 }
133 croak "could not init from XML, missing id" unless $self->{id};
134 $self->{_init} = 1;
135 }
136  
137 1;
« API.pm ↑ Summary API/Comment.pm »