File Coverage

blib/lib/WebService/Blogger/Blog/Entry.pm
Criterion Covered Total %
statement 12 40 30.0
branch 0 6 0.0
condition n/a
subroutine 4 10 40.0
pod 3 6 50.0
total 19 62 30.6


line stmt bran cond sub pod time code
1             package WebService::Blogger::Blog::Entry;
2             our $VERSION = '0.23';
3 2     2   16 use warnings;
  2         4  
  2         69  
4 2     2   10 use strict;
  2         5  
  2         38  
5              
6 2     2   11 use Moose;
  2         3  
  2         10  
7 2     2   12913 use XML::Simple ();
  2         4  
  2         1661  
8              
9             with 'WebService::Blogger::AtomReading';
10              
11             # Properties that can be updated in existing entries.
12             has title => ( is => 'rw', isa => 'Maybe[Str]' );
13             has content => ( is => 'rw', isa => 'Maybe[Str]' );
14             has categories => ( is => 'rw', isa => 'ArrayRef[Str]', auto_deref => 1 );
15              
16             # Read-only properties.
17             has id => ( is => 'ro', isa => 'Str' );
18             has author => ( is => 'ro', isa => 'Str' );
19             has published => ( is => 'ro', isa => 'Str' );
20             has updated => ( is => 'ro', isa => 'Str' );
21             has edit_url => ( is => 'ro', isa => 'Str' );
22             has id_url => ( is => 'ro', isa => 'Str' );
23             has public_url => ( is => 'ro', isa => 'Str' );
24              
25             # Service properties.
26             has source_xml_tree => ( is => 'ro', isa => 'HashRef', default => sub { {} }, required => 1 );
27             has blog => ( is => 'ro', isa => 'WebService::Blogger::Blog', required => 1 );
28              
29             # Speed Moose up.
30             __PACKAGE__->meta->make_immutable;
31              
32              
33             sub BUILDARGS {
34             ## Populates object attributes from parsed XML source.
35 0     0 1   my $class = shift;
36 0           my %params = @_;
37              
38             # Use shorter name for clarity.
39 0           my $tree = $params{source_xml_tree};
40              
41             # Extract attributes from the XML tree and return the to be set as
42             # attributes.
43              
44             return {
45             id => $tree->{id}[0],
46             author => $tree->{author}[0]{name}[0],
47             published => $tree->{published}[0],
48             updated => $tree->{updated}[0],
49             title => $tree->{title}[0]{content},
50             content => $tree->{content}{content},
51             public_url => $class->get_link_href_by_rel($tree, 'alternate'),
52             id_url => $class->get_link_href_by_rel($tree, 'self'),
53             edit_url => $class->get_link_href_by_rel($tree, 'edit'),
54 0 0         categories => [ map $_->{term}, @{ $tree->{category} || [] } ],
  0            
55             %params,
56             };
57             }
58              
59              
60             sub xml_for_creation {
61             ## Class method. Returns XML for creation of a new entry with given properties.
62 0     0 0   my $class = shift;
63 0           my %props = @_;
64              
65             # Build data structure to generate XML from.
66             my %xml_tree = (
67             title => [ {
68             content => $props{title},
69             type => 'text',
70             } ],
71             content => [ {
72             content => $props{content},
73             type => 'html',
74             } ],
75             category => [
76             map {
77             scheme => 'http://www.blogger.com/atom/ns#',
78             term => $_,
79             },
80 0 0         @{ $props{categories} || [] }
  0            
81             ],
82             );
83 0           $class->add_xml_ns(\%xml_tree);
84              
85             # Convert data tree to XML.
86 0           return XML::Simple::XMLout(\%xml_tree, RootName => 'entry');
87             }
88              
89              
90             sub add_xml_ns {
91             ## Adds XML namespace attributes to the given XML hash tree.
92 0     0 0   my $class = shift;
93 0           my ($dest) = @_;
94              
95 0           my %xml_ns = (
96             '' => 'http://www.w3.org/2005/Atom',
97             ':thr' => 'http://purl.org/rss/1.0/modules/threading/',
98             ':gd' => 'http://schemas.google.com/g/2005',
99             );
100 0           while (my ($postfix, $url) = each %xml_ns) {
101 0           $dest->{"xmlns$postfix"} = $url;
102             }
103             }
104              
105              
106             sub as_xml {
107             ## Returns XML string representing the entry.
108 0     0 0   my $self = shift;
109              
110             # Place attribute values into original data tree. Don't generate an Atom entry anew as
111             # Blogger wants us to preserve all original data when updating posts.
112 0           $self->source_xml_tree->{title}[0] = {
113             content => $self->title,
114             type => 'text',
115             };
116             $self->source_xml_tree->{content} = {
117 0           content => $self->content,
118             type => 'html',
119             };
120             $self->source_xml_tree->{category} = [
121 0           map {
122             scheme => 'http://www.blogger.com/atom/ns#',
123             term => $_,
124             },
125             $self->categories
126             ];
127 0           $self->add_xml_ns($self->source_xml_tree);
128              
129             # Convert data tree to XML.
130 0           return XML::Simple::XMLout($self->source_xml_tree, RootName => 'entry');
131             }
132              
133              
134             sub save {
135             ## Saves the entry to blogger.
136 0     0 1   my $self = shift;
137              
138 0           my $response = $self->blog->blogger->http_put($self->edit_url => $self->as_xml);
139 0 0         die 'Unable to save entry: ' . $response->status_line unless $response->is_success;
140 0           return $response;
141             }
142              
143              
144             sub delete {
145             ## Deletes the entry from server.
146 0     0 1   my $self = shift;
147              
148 0           $self->blog->delete_entry($self);
149             }
150              
151              
152             1;
153              
154             __END__
155              
156             =head1 NAME
157              
158             WebService::Blogger::Entry - represents blog entry in WebService::Blogger package.
159              
160             =head1 SYNOPSIS
161              
162             Please see L<WebService::Blogger>.
163              
164             =head1 ATTRIBUTES
165              
166             =head3 C<id>
167              
168             =over
169              
170             Unique numeric ID of the entry.
171              
172             =back
173              
174             =head3 C<title>
175              
176             =over
177              
178             Title of the entry.
179              
180             =back
181              
182              
183             =head3 C<content>
184              
185             =over
186              
187             Content of the entry. Currently entries are always submitted with
188             content type set to "html".
189              
190             =back
191              
192              
193             =head3 C<author>
194              
195             =over
196              
197             Author of the entry, as name only. Editing of this field is currently
198             not supported by Blogger API.
199              
200             =back
201              
202             =head3 C<published>
203              
204             =over
205              
206             Time when entry was published, in ISO format.
207              
208             =back
209              
210             =head3 C<updated>
211              
212             =over
213              
214             Time when entry was last updated, in ISO format.
215              
216             =back
217              
218             =head3 C<public_url>
219              
220             =over
221              
222             The human-readable, SEO-friendly URL of the entry.
223              
224             =back
225              
226             =head3 C<id_url>
227              
228             =over
229              
230             The never-changing URL of the entry, based on its numeric ID.
231              
232             =back
233              
234             =head3 C<categories>
235              
236             =over
237              
238             Categories (tags) of the entry, as array of strings.
239              
240             =back
241              
242             =head3 C<blog>
243              
244             =over
245              
246             The blog in which entry is published, as instance of WebService::Blogger::Blog
247              
248             =back
249              
250             =cut
251              
252             =head1 METHODS
253              
254             =over 1
255              
256             =item new()
257              
258             Creates new entry. Requires C<blog>, C<content> and C<title> attributes.
259              
260             =item save()
261              
262             Saves changes to the entry.
263              
264             =item delete()
265              
266             Deltes the entry from server and parent blog object.
267              
268             =cut
269              
270             =back
271              
272             =head1 AUTHOR
273              
274             Kedar Warriner, C<< <kedar at cpan.org> >>
275              
276             =head1 BUGS
277              
278             Please report any bugs or feature requests to C<bug-net-google-api-blogger at rt.cpan.org>, or through
279             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WebService-Blogger>. I will be notified, and then you'll
280             automatically be notified of progress on your bug as I make changes.
281              
282             =head1 SUPPORT
283              
284             You can find documentation for this module with the perldoc command.
285              
286             perldoc WebService::Blogger
287              
288             You can also look for information at:
289              
290             =over 4
291              
292             =item * RT: CPAN's request tracker
293              
294             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WebService-Blogger>
295              
296             =item * AnnoCPAN: Annotated CPAN documentation
297              
298             L<http://annocpan.org/dist/WebService-Blogger>
299              
300             =item * CPAN Ratings
301              
302             L<http://cpanratings.perl.org/d/WebService-Blogger>
303              
304             =item * Search CPAN
305              
306             L<http://search.cpan.org/dist/WebService-Blogger/>
307              
308             =back
309              
310             =head1 ACKNOWLEDGEMENTS
311              
312             Many thanks to:
313             - Egor Shipovalov who wrote the original version of this module
314             - Everyone involved with CPAN.
315              
316             =head1 LICENSE AND COPYRIGHT
317              
318             Copyright 2010 Kedar Warriner.
319              
320             This program is free software; you can redistribute it and/or modify it
321             under the terms of either: the GNU General Public License as published
322             by the Free Software Foundation; or the Artistic License.
323              
324             See http://dev.perl.org/licenses/ for more information.
325              
326             =cut