File Coverage

blib/lib/XML/Atom/Lifeblog.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package XML::Atom::Lifeblog;
2              
3 2     2   26250 use strict;
  2         4  
  2         77  
4 2     2   10 use vars qw($VERSION);
  2         5  
  2         197  
5             $VERSION = '0.03';
6              
7 2     2   14204 use Encode;
  2         34102  
  2         252  
8 2     2   18 use File::Basename;
  2         4  
  2         224  
9 2     2   1752 use MIME::Types;
  2         25909  
  2         137  
10 2     2   2557 use XML::Atom::Client;
  0            
  0            
11             use XML::Atom::Entry;
12             use XML::Atom::Lifeblog::Media;
13             use base qw(XML::Atom::Client);
14              
15             sub postLifeblog {
16             my($self, $post_uri, $title, $body, $media) = @_;
17             if (!UNIVERSAL::isa($media, "XML::Atom::Lifeblog::Media") && !ref($media)) {
18             $media = XML::Atom::Lifeblog::Media->new(filename => $media);
19             } elsif (ref($media) eq 'HASH') {
20             $media = XML::Atom::Lifeblog::Media->new(%$media);
21             }
22              
23             my $atom_media = $self->_create_media($media);
24             my $posted = $self->_post_entry($post_uri, $atom_media)
25             or return $self->error("POST ($media) failed: " . $self->errstr);
26             my $atom_body = $self->_create_body($title, $body, $posted->id, $media->type);
27             return $self->_post_entry($post_uri, $atom_body);
28             }
29              
30             sub _guess_mime_type {
31             my($self, $media) = @_;
32             # MIME::Types doesn't support 3gpp
33             if ($media =~ /\.3gpp?$/) {
34             # XXX what about audio/3gpp?
35             return "video/3gpp";
36             } else {
37             my $mime = MIME::Types->new->mimeTypeOf($media);
38             return $mime ? $mime->type : "application/octet-stream";
39             }
40             }
41              
42             sub _create_media {
43             my($self, $media) = @_;
44              
45             my $entry = XML::Atom::Entry->new();
46             $entry->title($media->title);
47             $entry->content($media->content);
48             $entry->content->type($media->type);
49              
50             # add 1
51             my $tp = XML::Atom::Namespace->new(standalone => "http://sixapart.com/atom/typepad#");
52             $entry->set($tp => "standalone" => 1);
53             return $entry;
54             }
55              
56             sub _create_body {
57             my($self, $title, $body, $id, $mime_type) = @_;
58             my $entry = XML::Atom::Entry->new();
59             $entry->title($title);
60             $entry->content($body);
61              
62             # add link rel="related" for the uploaded image
63             my $link = XML::Atom::Link->new();
64             $link->type($mime_type);
65             $link->rel('related');
66             $link->href($id);
67             $entry->add_link($link);
68             return $entry;
69             }
70              
71             # XXX XML::Atom::Client's createEntry doesn't return response body
72             sub _post_entry {
73             my $client = shift;
74             my($uri, $entry) = @_;
75             return $client->error("Must pass a PostURI before posting")
76             unless $uri;
77              
78             my $req = HTTP::Request->new(POST => $uri);
79             $req->content_type('application/x.atom+xml');
80              
81             my $xml = $entry->as_xml;
82             Encode::_utf8_off($xml);
83             $req->content_length(length $xml);
84             $req->content($xml);
85              
86             my $res = $client->make_request($req);
87             return $client->error("Error on POST $uri: " . $res->status_line)
88             unless $res->code == 201;
89             return XML::Atom::Entry->new(Stream => \$res->content);
90             }
91              
92             1;
93             __END__