Posterous.pm

filter: Statementstmt 24.7% 42 / 170 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 44.8% 13 / 29 Podpod 100% 10 / 10 totaltotal 23.0% 65 / 282 CC 33 SCAR 29.8
CC
33
Cov
18%
_request
56.0
_post
42.0
_load
42.0
CRAP 19.7
↑ Summary Posterous/Post.pm »
1 package Net::Posterous;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Net::Posterous::Site;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Net::Posterous::Post;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Net::Posterous::Media;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Net::Posterous::Tag;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Net::Posterous::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 2 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 our $VERSION = "0.8";
21 2 our $DOMAIN = "http://posterous.com";
22  
23  
24  
25 =head1 NAME
26  
27 Net::Posterous - read and post from Posterous blogs
28  
29 =head1 SYNOPSIS
30  
31 my $api = Net::Posterous->new($user, $pass);
32
33 # Get a list of sites the user owns as Net::Posterous::Site objects
34 my @sites = $api->get_sites;
35
36 # Set the site to use
37 $api->site($sites[0]);
38  
39 # Create a post
40 my $post = Net::Posterous::Post->new(%opts);
41 my $res = $api->post($post);
42
43 # Update the post
44 $post->title("New Title");
45 my $res = $api->post($post);
46
47 # Get a list of posts
48 my @posts = $api->get_posts;
49
50 # Get an individual post using the http://post.ly shortcode
51 # i.e 123abc in http://post.ly/123abc
52 my $post = $api->get_post($post->short_code);
53
54
55 # Get a list of tags
56 my @tags = $api->get_tags;
57
58 # Create a post with an video attached
59 my $video = Net::Posterous::Media::Local->new(file => $path_to_movie);
60 my $vpost = Net::Posterous::Media::Post->new( title => "My movie", media => $video );
61 my $res = $api->post($vpost);
62
63 # Create a post with a flipbook of pictures
64 my @images = map { Net::Posterous::Media::Local->new(file => $_) } qw(1.jpg 2.jpg 3.jpg);
65 my $ipost = Net::Posterous::Media::Post->new( title => "My flipbook", media => \@images );
66 my $res = $api->post($ipost);
67
68 # Add a comment
69 my $comment = Net::Posterous::Comment->new( body => "Nice flipbook!" );
70 my $res = $api->comment($ipost, $comment);
71
72
73
74 =head1 DESCRIPTION
75  
76 This allows reading a writing from Posterous sites.
77  
78 It's very similar to the C<Posterous> module but:
79  
80 =over 4
81  
82 =item It doesn't require Perl 5.10
83  
84 =item It's slightly more user friendly
85  
86 =item It's more CPAN namespace friendly
87  
88 =back
89  
90 =head1 METHODS
91  
92 =head2 new <username> <password> [site]
93  
94 Create a new client object.
95  
96 Requires a username and a password.
97  
98 Optionally you can pass in either a site id or a C<Net::Posterous::Site> object to
99 specify which site to read/write from.
100  
101 If it's not passed in then it can be passed in later or your default site will be used.
102  
103 =cut
104 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
105 0 my $class = shift;
106 # TODO none authenticated
107 0 ▶ my $user = shift || die "You must pass a username";
Condition0%0/2
shift() || die('You must pass a username')
l!l
00
MC/DC0%0/1
shift() || die('You must pass a username')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
108 0 ▶ my $pass = shift || die "You must pass a password";
Condition0%0/2
shift() || die('You must pass a password')
l!l
00
MC/DC0%0/1
shift() || die('You must pass a password')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
109 0 my $self = bless { user => $user, pass => $pass, ua => _get_ua(), auth_key => encode_base64($user.":".$pass) };
110  
111 0 $self->site(@_);
112  
113 0 return $self;
114
115 }
116  
117 0 ▶sub _get_ua {
Subroutine _get_ua: not called CC=1 SCAR=6.9
118 0 return LWP::UserAgent->new( agent => __PACKAGE__."-".$VERSION );
119 }
120  
121 =head2 site [site]
122  
123 Get or set the current site being used.
124  
125 May return undef.
126  
127 =cut
128 0 ▶sub site {
Subroutine site: not called CC=2 SCAR=17.9
Pod documented
129 0 my $self = shift;
130  
131 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
132 0 ($self->{site_id}, $self->{site}) = $self->_load_site(shift);
133 }
134 0 return $self->{site};
135 }
136  
137 0 ▶sub _load_site {
Subroutine _load_site: not called CC=3 SCAR=24.8
138 0 my $self = shift;
139 0 ▶ my $site = shift || return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
140
141 0 ▶ if (ref($site)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $site) { } 0 0 0
142 0 return ($site->id, $site);
143 } else {
144 0 return ($site, $self->site_from_id($site));
145 }
146 }
147  
148 =head2 site_from_id <id>
149  
150 Return a C<Net::Posterous::Site> object based on a site id.
151  
152 Returns undef if the site can't be found.
153  
154 =cut
155  
156  
157 0 ▶sub site_from_id {
Subroutine site_from_id: not called CC=1 SCAR=6.9
Pod documented
158 0 my $self = shift;
159 0 my $id = shift;
160 0 $self->get_sites; # Force loading of sites
161 0 return $self->{sites}->{$id};
162 }
163  
164 =head2 get_sites
165  
166 Get a list of all the user's sites.
167  
168 =cut
169 0 ▶sub get_sites {
Subroutine get_sites: not called CC=2 SCAR=17.9
Pod documented
170 0 my $self = shift;
171 0 my @sites = $self->_load("GET", "${DOMAIN}/api/getsites", 'site', 'name', 'Net::Posterous::Site');
172 0 $self->{sites}->{$_->id} = $_ for @sites;
173 0 return @sites;
174 }
175  
176 =head2 get_posts [opt[s]]
177  
178 Get the posts from a site.
179  
180 Uses, in order - the site passed in using the option key C<site>, the site set by the user, the default site.
181  
182 The options are
183  
184 =over 4
185  
186 =item site_id
187  
188 The id of the site to read from
189  
190 =item hostname
191  
192 Subdomain of the site to read from
193  
194 =item num_posts
195  
196 How many posts you want. Default is 10, max is 50.
197  
198 =item page
199  
200 What 'page' you want (based on num_posts). Default is 1
201  
202 =item tag
203  
204 Only get items with this tag.
205  
206 =back
207  
208  
209 =cut
210 0 ▶sub get_posts {
Subroutine get_posts: not called CC=3 SCAR=24.8
Pod documented
211 0 my $self = shift;
212 0 my %opts = @_;
213 0 ▶ my $site = $self->_load_site(delete $opts{site} || $self->site); # normalise the site to an object
Condition0%0/3
delete $opts{'site'} || $self->site
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts{'site'} || $self->site
delete $opts{'site'} $self->site
Truth table0%0/3
A || B
A= delete $opts{'site'}B= $self->site
ABresult
1X1
011
000
214 0 ▶ my %params = $site ? ( %opts, site_id => $site->id ) : %opts;
Branch: 1 branch
BranchTrueFalseTotal
$site ? : 0 0 0
215 0 return $self->_load("GET", "${DOMAIN}/api/readposts", 'post', 'id', 'Net::Posterous::Post', %params);
216 }
217  
218 =head2 get_post <short code>
219  
220 Get an id via the http://post.ly short code i.e 123abc in http://post.ly/123abc
221  
222 =cut
223  
224 0 ▶sub get_post {
Subroutine get_post: not called CC=1 SCAR=6.9
Pod documented
225 0 my $self = shift;
226 0 my $id = shift;
227
228 0 $id =~ s!^http://post\.ly/!!; # be liberal in what you accept etc etc
229
230 0 my @posts = $self->_load("GET", "${DOMAIN}/api/getpost", 'post', 'id', 'Net::Posterous::Post', id => $id);
231 0 return shift @posts;
232 }
233  
234 =head2 get_tags [opt[s]]
235  
236 Get a list of tags for a site.
237  
238 Uses, in order - the site passed in using the option key C<site>, the site set by the user, the default site.
239  
240 The options are
241  
242 =over 4
243  
244 =item site_id
245  
246 Optional. Id of the site to read from
247  
248 =item hostname
249  
250 Optional. Subdomain of the site to read from
251  
252 =back
253  
254 =cut
255 0 ▶sub get_tags {
Subroutine get_tags: not called CC=3 SCAR=24.8
Pod documented
256 0 my $self = shift;
257 0 my %opts = @_;
258 0 ▶ my $site = $self->_load_site(delete $opts{site} || $self->site); # normalise the site to an object
Condition0%0/3
delete $opts{'site'} || $self->site
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts{'site'} || $self->site
delete $opts{'site'} $self->site
Truth table0%0/3
A || B
A= delete $opts{'site'}B= $self->site
ABresult
1X1
011
000
259 0 ▶ my %params = $site ? ( %opts, site_id => $site->id ) : %opts;
Branch: 1 branch
BranchTrueFalseTotal
$site ? : 0 0 0
260 0 return $self->_load("GET", "${DOMAIN}/api/gettags", 'tag', 'id', 'Net::Posterous::Tag', %params);
261 }
262  
263 =head2 post <post> [opt[s]]
264  
265 Post or update a C<Net::Posterous::Post> or <Net::Posterous::Media> object.
266  
267 Uses, in order - the site passed in using the option key C<site>, the site set by the user, the default site.
268  
269 =cut
270 0 ▶sub post {
Subroutine post: not called CC=4 SCAR=30.0
Pod documented
271 0 my $self = shift;
272 0 my $post = shift;
273 0 my %opts = @_;
274 0 ▶ my $site = $self->_load_site(delete $opts{site} || $self->site); # normalise the site to an object
Condition0%0/3
delete $opts{'site'} || $self->site
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $opts{'site'} || $self->site
delete $opts{'site'} $self->site
Truth table0%0/3
A || B
A= delete $opts{'site'}B= $self->site
ABresult
1X1
011
000
275
276 0 my %params = $post->_to_params();
277
278 0 my $url = $DOMAIN;
279 # Update or Create depending on whether the Post already has an id
280 0 ▶ if (my $id = delete $params{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = delete $params{'id'}) { } 0 0 0
281 0 $url .= "/api/updatepost";
282 0 $params{post_id} = $id;
283 } else {
284 0 $url .= "/api/newpost";
285 0 ▶ $params{site_id} = $site->id if $site;
Branch: 1 branch
BranchTrueFalseTotal
if $site 0 0 0
286 }
287 0 return $self->_post($post, $url , "post", %params);
288 }
289  
290 =head2 comment <post> <comment>
291  
292 Add a comment to the post.
293  
294 =cut
295 0 ▶sub comment {
Subroutine comment: not called CC=2 SCAR=17.9
Pod documented
296 0 my $self = shift;
297 0 my $post = shift;
298 0 my $comment = shift;
299
300 0 ▶ my $post_id = ref($post) ? $post->id : $post;
Branch: 1 branch
BranchTrueFalseTotal
ref $post ? : 0 0 0
301 0 my %params = ($comment->_to_params, post_id => $post_id);
302 0 return $self->_post($comment, "${DOMAIN}/api/newcomment" , "comment", %params);
303 }
304  
305 0 ▶sub _post {
Subroutine _post: not called CC=6 SCAR=37.4
306 0 my $self = shift;
307 0 my $obj = shift;
308 0 my $url = shift;
309 0 my $key = shift;
310 0 my %params = @_;
311
312 0 ▶ my $res = $self->_request("POST", $url, %params) || return undef;
Condition0%0/2
$self->_request('POST', $url, %params) || (return undef)
l!l
00
MC/DC0%0/1
$self->_request('POST', $url, %params) || (return undef)
$self->_request('POST', $url, %params)
Truth table0%0/2
A
A= $self->_request('POST', $url, %params)
Aresult
11
00
313
314 0 my $data = eval { XMLin($res->content) };
315 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
316 0 $self->error("Couldn't parse XML response: $@");
317 0 return undef;
318 }
319
320 # Check to see if we got an error, despite getting a 200 ok
321 0 ▶ if (my $error = $self->_xml_nok($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->_xml_nok($data)) 0 0 0
322 0 $self->error("Couldn't POST $url - $error");
323 0 return undef;
324 }
325
326 # Merge the new data with object
327 # It would be awesome if Posterous returned a full representation of the new object after a POST
328 0 ▶ my $tmp = $data->{$key} || {};
Condition0%0/2
$data->{$key} || {}
l!l
00
MC/DC0%0/1
$data->{$key} || {}
$data->{$key}
Truth table0%0/2
A
A= $data->{$key}
Aresult
11
00
329 0 foreach my $key (keys %$tmp) {
330 0 $obj->$key($tmp->{$key});
331 }
332 0 return $obj;
333 }
334  
335  
336  
337 0 ▶sub _load {
Subroutine _load: not called CC=6 SCAR=37.4
338 0 my $self = shift;
339 0 my $meth = shift;
340 0 my $url = shift;
341 0 my $key = shift;
342 0 my $id_name = shift;
343 0 my $class = shift;
344 0 my %params = @_;
345
346 # TODO paging
347
348 0 ▶ my $res = $self->_request($meth, $url, %params) || return ();
Condition0%0/2
$self->_request($meth, $url, %params) || (return ())
l!l
00
MC/DC0%0/1
$self->_request($meth, $url, %params) || (return ())
$self->_request($meth, $url, %params)
Truth table0%0/2
A
A= $self->_request($meth, $url, %params)
Aresult
11
00
349 0 my $data = eval { XMLin($res->content, ForceArray => [$key, 'media'], KeyAttr => $id_name) };
350 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
351 0 $self->error("Couldn't parse XML response: $@");
352 0 return undef;
353 }
354
355 # Check to see if we got an error, despite getting a 200 ok
356 0 ▶ if (my $error = $self->_xml_nok($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->_xml_nok($data)) 0 0 0
357 0 $self->error("Couldn't $meth $url - $error");
358 0 return undef;
359 }
360
361 # Create new objects from the results
362 0 ▶ my $items = $data->{$key} || {};
Condition0%0/2
$data->{$key} || {}
l!l
00
MC/DC0%0/1
$data->{$key} || {}
$data->{$key}
Truth table0%0/2
A
A= $data->{$key}
Aresult
11
00
363 0 my @results;
364 0 foreach my $id (keys %$items) {
365 0 my $obj = $items->{$id};
366 0 $obj->{$id_name} = $id;
367 0 push @results, $class->new(%$obj);
368 }
369 0 return @results;
370 }
371  
372 # Check the XML for errors
373 0 ▶sub _xml_nok {
Subroutine _xml_nok: not called CC=2 SCAR=17.9
374 0 my $self = shift;
375 0 my $data = shift;
376 0 ▶ return 0 if "ok" eq $data->{stat};
Branch: 1 branch
BranchTrueFalseTotal
if "ok" eq $data->{"stat"} 0 0 0
377 0 return $data->{err}->{code}.": ".$data->{err}->{msg};
378 }
379  
380 0 ▶sub _request {
Subroutine _request: not called CC=7 SCAR=40.3
381 0 my $self = shift;
382 0 my $meth = shift;
383 0 my $url = shift;
384 0 my %opts = @_;
385 0 my $ua = $self->{ua};
386  
387 0 $self->{error} = undef;
388
389
390 0 my $req;
391 0 my $req_url = URI->new($url);
392 0 $req_url->query_form(%opts);
393 0 ▶ if ('GET' eq $meth || 'PUT' eq $meth) {
Branch: 1 branch
BranchTrueFalseTotal
if ('GET' eq $meth or 'PUT' eq $meth) { } 0 0 0
Condition0%0/3
"GET" eq $meth or "PUT" eq $meth
l!l&&r!l&&!r
000
MC/DC0%0/2
"GET" eq $meth or "PUT" eq $meth
"GET" eq $meth "PUT" eq $meth
Truth table0%0/3
A or B
A= "GET" eq $methB= "PUT" eq $meth
ABresult
1X1
011
000
394 0 $req = HTTP::Request->new( $meth => $req_url);
395 } else {
396 0 my @content;
397 # This little shennanigans is to allow us to post multiple file uploads
398 # by getting round HTTP::Request::Common's helper feature
399 0 foreach my $key (keys %opts) {
400 0 my $value = $opts{$key};
401 0 ▶ $value = [$value] unless ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 0 0
402 0 push @content, ($key, $_) for @$value;
403 }
404 0 $req = POST($url, Content_Type => 'form-data', Content => [ @content ]);
405 }
406 0 $req->header(Authorization => "Basic ".$self->{auth_key});
407  
408 0 my $res = $ua->request($req);
409 0 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
410 0 $self->error("Couldn't $meth $url: ".$res->content);
411 0 return undef;
412 }
413 0 return $res;
414 }
415  
416 =head2 error
417  
418 Get or set the last error
419  
420 =cut
421 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
422 0 my $self = shift;
423 0 ▶ $self->{error} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
424 0 return $self->{error};
425 }
426  
427 =head1 ADDING MEDIA
428  
429 The way to add media is to create a new C<Net::Posterous::Media::Local> file and then add that to the
430 C<Net::Posterous::Post> object that's going to be created or updated.
431  
432 It will then be turned into a proper C<Net::Posterous::Media> object when the Post is retrieved.
433  
434 =head1 BUGS
435  
436 The Posterous API docs mention being able to post "common document formats" but there's no docs for it.
437  
438 I'll add it when I come across it.
439  
440 =head1 DEVELOPERS
441  
442 The latest code for this module can be found at
443  
444 https://svn.unixbeard.net/simon/Net-Posterous
445  
446 =head1 AUTHOR
447  
448 Simon Wistow, C<<simon@thegestalt.org >>
449  
450 =head1 BUGS
451  
452 Please report any bugs or feature requests to C<bug-net-posterous at rt.cpan.org>, or through
453 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Posterous>. I will be notified, and then you'll
454 automatically be notified of progress on your bug as I make changes.
455  
456 =head1 SUPPORT
457  
458 You can find documentation for this module with the perldoc command.
459  
460 perldoc Net::Posterous
461  
462 You can also look for information at:
463  
464 =over 4
465  
466 =item * RT: CPAN's request tracker
467  
468 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Posterous>
469  
470 =item * AnnoCPAN: Annotated CPAN documentation
471  
472 L<http://annocpan.org/dist/Net-Posterous>
473  
474 =item * CPAN Ratings
475  
476 L<http://cpanratings.perl.org/d/Net-Posterous>
477  
478 =item * Search CPAN
479  
480 L<http://search.cpan.org/dist/Net-Posterous/>
481  
482 =back
483  
484 =head1 COPYRIGHT & LICENSE
485  
486 Copyright 2010 Simon Wistow, all rights reserved.
487  
488 This program is free software; you can redistribute it and/or modify it
489 under the same terms as Perl itself.
490  
491 =cut
492  
493 2 1;
↑ Summary Posterous/Post.pm »