File Coverage

blib/lib/Net/Google/Blogger/Blog.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Net::Google::Blogger::Blog;
2              
3 1     1   3305 use warnings;
  1         3  
  1         55  
4 1     1   6 use strict;
  1         2  
  1         40  
5              
6 1     1   7 use Any::Moose;
  1         2  
  1         8  
7 1     1   1375 use XML::Simple ();
  0            
  0            
8             use URI::Escape ();
9              
10             use Net::Google::Blogger::Blog::Entry;
11              
12              
13             our $VERSION = '0.09';
14              
15             has id => ( is => 'ro', isa => 'Str', required => 1 );
16             has numeric_id => ( is => 'ro', isa => 'Str', required => 1 );
17             has title => ( is => 'rw', isa => 'Str', required => 1 );
18             has public_url => ( is => 'ro', isa => 'Str', required => 1 );
19             has id_url => ( is => 'ro', isa => 'Str', required => 1 );
20             has post_url => ( is => 'ro', isa => 'Str', required => 1 );
21             has source_xml_tree => ( is => 'ro', isa => 'HashRef', required => 1 );
22             has blogger => ( is => 'ro', isa => 'Net::Google::Blogger', required => 1 );
23              
24             has entries => (
25             is => 'rw',
26             isa => 'ArrayRef[Net::Google::Blogger::Blog::Entry]',
27             lazy_build => 1,
28             auto_deref => 1,
29             );
30              
31             __PACKAGE__->meta->make_immutable;
32              
33              
34             sub BUILDARGS {
35             ## Parses source XML into initial attribute values.
36             my $class = shift;
37             my %params = @_;
38              
39             my $id = $params{source_xml_tree}{id}[0];
40             my $links = $params{source_xml_tree}{link};
41              
42             return {
43             id => $id,
44             numeric_id => $id =~ /(\d+)$/,
45             title => $params{source_xml_tree}{title}[0]{content},
46             id_url => (grep $_->{rel} eq 'self', @$links)[0]{href},
47             public_url => (grep $_->{rel} eq 'alternate', @$links)[0]{href},
48             post_url => (grep $_->{rel} =~ /#post$/, @$links)[0]{href},
49             %params,
50             };
51             }
52              
53              
54             sub _build_entries {
55             ## Populates the entries attribute, loading all entries for the blog.
56             my $self = shift;
57              
58             # Search with no parameters.
59             return $self->search_entries;
60             }
61              
62              
63             sub search_entries {
64             ## Returns entries matching search criteria.
65             my $self = shift;
66             my %params = @_;
67              
68             # Construct request URL, incorporating category criteria into it, if given.
69             my $url = 'http://www.blogger.com/feeds/' . $self->numeric_id . '/posts/default';
70             $url .= '/-/' . join '/', map URI::Escape::uri_escape($_), @{ $params{categories} }
71             if $params{categories};
72              
73             # Map our parameter names to Blogger's.
74             my %params_to_req_args_map = (
75             max_results => 'max-results',
76             published_min => 'published-min',
77             published_max => 'published-max',
78             updated_min => 'updated-min',
79             updated_max => 'updated-max',
80             order_by => 'orderby',
81             offset => 'start-index',
82             );
83              
84             # Map our sort mode parameter names to Blogger's.
85             my %sort_mode_map = (
86             last_modified => 'lastmodified',
87             start_time => 'starttime',
88             updated => 'updated',
89             );
90              
91             # Populate request arguments hash WRT above mappings.
92             my %req_args = (
93             alt => 'atom',
94             );
95             foreach (keys %params_to_req_args_map) {
96             $req_args{$params_to_req_args_map{$_}} = $params{$_} if exists $params{$_};
97             }
98             if (my $sort_mode = $params{sort_by}) {
99             $req_args{orderby} = $sort_mode_map{$sort_mode};
100             }
101              
102             # Execute request and parse the response.
103             my $response = $self->blogger->http_get($url, %req_args);
104             my $response_tree = XML::Simple::XMLin($response->content, ForceArray => 1);
105              
106             # Return list of entry objects constructed from list of hashes in parsed data.
107             my @entries
108             = map Net::Google::Blogger::Blog::Entry->new(
109             source_xml_tree => $_,
110             blog => $self,
111             ),
112             @{ $response_tree->{entry} };
113             return wantarray ? @entries : \@entries;
114             }
115              
116              
117             sub add_entry {
118             ## Adds given entry to the blog.
119             my $self = shift;
120             my ($entry) = @_;
121              
122             my $response = $self->blogger->http_post(
123             $self->post_url,
124             'Content-Type' => 'application/atom+xml',
125             Content => $entry->as_xml,
126             );
127              
128             die 'Unable to add entry to blog: ' . $response->status_line unless $response->is_success;
129             $entry->update_from_http_response($response);
130              
131             push @{ $self->entries }, $entry;
132             return $entry;
133             }
134              
135              
136             sub delete_entry {
137             ## Deletes given entry from server as well as list of entries held in blog object.
138             my $self = shift;
139             my ($entry) = @_;
140              
141             my $response = $self->blogger->http_post(
142             $entry->edit_url,
143             'X-HTTP-Method-Override' => 'DELETE',
144             );
145             die 'Could not delete entry from server: ' . $response->status_line unless $response->is_success;
146              
147             $self->entries([ grep $_ ne $entry, $self->entries ]);
148             }
149              
150              
151             sub destroy {
152             ## Removes references to the blog from child entries, so they're
153             ## no longer circular. Blog object as well as entries can then be
154             ## garbage-collected.
155             my $self = shift;
156              
157             $_->blog(undef) foreach $self->entries;
158             }
159              
160              
161             1;
162              
163             __END__