File Coverage

blib/lib/WWW/Phanfare/API.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package WWW::Phanfare::API;
2              
3             =head1 NAME
4              
5             WWW::Phanfare::API - Perl wrapper for Phanfare API
6              
7             =head1 VERSION
8              
9             Version 0.09
10              
11             =cut
12              
13 1     1   28931 use strict;
  1         3  
  1         39  
14 1     1   5 use warnings;
  1         3  
  1         30  
15 1     1   6 use Carp;
  1         6  
  1         76  
16 1     1   1389 use REST::Client;
  1         189998  
  1         35  
17 1     1   13 use Digest::MD5 qw(md5_hex);
  1         1  
  1         81  
18 1     1   6 use URI::Escape;
  1         2  
  1         55  
19 1     1   522 use XML::Simple;
  0            
  0            
20              
21             our $VERSION = '0.09';
22             our $site = 'http://www.phanfare.com/api/?';
23             our $AUTOLOAD;
24              
25             sub new {
26             my $that = shift;
27             my $class = ref($that) || $that;
28             my $self = { @_ };
29             bless $self, $class;
30             return $self;
31             }
32              
33             # Load date from url
34             sub geturl {
35             my($self,$url,$post) = @_;
36              
37             # Create REST agent with cookies
38             unless ( $self->{_rest} ) {
39             $self->{_rest} = new REST::Client;
40             $self->{_rest}->getUseragent()->cookie_jar({});
41             }
42             my $rest = $self->{_rest};
43              
44             # GET or POST
45             if ( $post ) {
46             $rest->POST( $url, $post );
47             } else {
48             $rest->GET( $url );
49             }
50              
51             # Verify Response
52             carp sprintf(
53             "Return code %s: %s",
54             $rest->responseCode(),
55             $rest->responseContent()
56             ) unless $rest->responseCode() eq '200';
57              
58             # Content
59             $rest->responseContent();
60             }
61              
62             # All API methods implemented as autoload functions.
63             #
64             # $papi->Function() becomes REST::Client::GET(..."method=Function"...)
65             #
66             sub AUTOLOAD {
67             my $self = shift;
68             croak "$self is not an object" unless ref($self);
69              
70             my $method = $AUTOLOAD;
71             $method =~ s/.*://; # strip fully-qualified portion
72             croak "method not defined" unless $method;
73              
74             # Verify keys are defined
75             croak 'api_key not defined' unless $self->{api_key};
76             croak 'private_key not defined' unless $self->{private_key};
77              
78             my %param = @_;
79              
80             # Is POST content included
81             delete $param{content} if my $content = $param{content};
82              
83             # Build signature request string
84             my $req = join '&',
85             sprintf('%s=%s', 'api_key', $self->{api_key}),
86             sprintf('%s=%s', 'method', $method),
87             map { sprintf '%s=%s', $_, (defined $param{$_} ? $param{$_} : '') } keys %param;
88              
89             # Sign request string
90             my $sig = md5_hex( $req . $self->{private_key} );
91              
92             # Build URL escaped request string
93             $req = join '&',
94             sprintf('%s=%s', 'api_key', $self->{api_key}),
95             sprintf('%s=%s', 'method', $method),
96             map { sprintf '%s=%s', $_, uri_escape( defined $param{$_} ? $param{$_} : '' ) } keys %param;
97             $req .= sprintf '&%s=%s', 'sig', $sig;
98              
99             return XML::Simple::XMLin $self->geturl( $site.$req, $content );
100             }
101              
102             # Make sure not caught by AUTOLOAD
103             sub DESTROY {}
104              
105             =head1 SYNOPSIS
106              
107             Create agent. Developer API keys required.
108              
109             use WWW::Phanfare::API;
110             my $api = WWW::Phanfare::API->new(
111             api_key => 'xxx',
112             private_key => 'yyy',
113             );
114              
115             Authentication with account:
116              
117             my $session = $api->Authenticate(
118             email_address => 'my@email',
119             password => 'zzz',
120             )
121             die "Cannot authenticate: $session->{code_value}"
122             unless $session->{'stat'} eq 'ok';
123             my $target_uid = $session->{session}{uid};
124            
125             Or authenticate as guest:
126              
127             $api->AuthenticateGuest();
128              
129             Get list of albums:
130              
131             my $albumlist = $api->GetAlbumList(
132             target_uid => $session->{session}{uid}
133             )->{albums}{album};
134              
135             printf(
136             "%s %s %s\n",
137             $_->{album_id}, substr($_->{album_start_date}, 0, 10), $_->{album_name}
138             ) for @$albumlist;
139              
140             Create new album, upload an image to it and delete it all again.
141              
142             my $album = $api->NewAlbum(
143             target_uid => $target_uid,
144             );
145              
146             my $album_id = $album->{album}{album_id};
147             my $section_id = $album->{album}{sections}{section}{section_id};
148             my $content = read_file('IMG_1234.jpg', binmode => ':raw');
149              
150             my $image = $api->NewImage(
151             target_uid => $target_uid,
152             album_id => $album_id,
153             section_id => $section_id,
154             filename => 'IMG_1234.jpg',
155             content => $content,
156             );
157              
158             my $del_album = $api->DeleteAlbum(
159             target_uid => $target_uid,
160             album_id => $album_id,
161             );
162              
163             Load an image.
164              
165             my $image = $api->geturl( $url );
166              
167              
168             =head1 DESCRIPTION
169              
170             Perl wrapper the Phanfare API. A developer API key is required for using
171             this module.
172              
173             =head1 SUBROUTINES/METHODS
174              
175             Refer to methods and required parameters are listed on
176             http://help.phanfare.com/index.php/API .
177              
178             api_key and private_key is only required for the constructor,
179             not for individual methods.
180              
181             Methods return hash references.
182             The value of the 'stat' key will be 'ok' if the call succeeded.
183             Value of 'code_value' key has error message.
184              
185             =head2 new
186              
187             Create a new API agent.
188              
189             =head2 geturl
190              
191             Load data from URL.
192              
193             =head1 AUTHOR
194              
195             Soren Dossing, C<< >>
196              
197             =head1 BUGS
198              
199             Please report any bugs or feature requests to C, or through
200             the web interface at L. I will be notified, and then you'll
201             automatically be notified of progress on your bug as I make changes.
202              
203              
204              
205              
206             =head1 SUPPORT
207              
208             You can find documentation for this module with the perldoc command.
209              
210             perldoc WWW::Phanfare::API
211              
212              
213             You can also look for information at:
214              
215             =over 4
216              
217             =item * Github's request tracker
218              
219             L
220              
221             =item * AnnoCPAN: Annotated CPAN documentation
222              
223             L
224              
225             =item * CPAN Ratings
226              
227             L
228              
229             =item * Search CPAN
230              
231             L
232              
233             =back
234              
235              
236             =head1 SEE ALSO
237              
238             =over 4
239              
240             =item * Official Phanfare API Refence Guide
241              
242             L
243              
244             =back
245              
246              
247             =head1 LICENSE AND COPYRIGHT
248              
249             Copyright 2010 Soren Dossing.
250              
251             This program is free software; you can redistribute it and/or modify it
252             under the terms of either: the GNU General Public License as published
253             by the Free Software Foundation; or the Artistic License.
254              
255             See http://dev.perl.org/licenses/ for more information.
256              
257             =cut
258              
259             1;