Blogger/Blog.pm

filter: Statementstmt 43.4% 30 / 69 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod 100% 5 / 5 totaltotal 41.8% 41 / 98 CC 9 SCAR 27.0
CC
9
Cov
37%
search_entries
42.0
add_entry
6.0
delete_entry
6.0
CRAP 14.9
↑ Summary Blogger/Blog/Entry.pm »
1 package Net::Google::Blogger::Blog;
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  
6 2 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use XML::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Net::Google::Blogger::Blog::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 2 our $VERSION = '0.09';
14  
15 2 has id => ( is => 'ro', isa => 'Str', required => 1 );
16 2 has numeric_id => ( is => 'ro', isa => 'Str', required => 1 );
17 2 has title => ( is => 'rw', isa => 'Str', required => 1 );
18 2 has public_url => ( is => 'ro', isa => 'Str', required => 1 );
19 2 has id_url => ( is => 'ro', isa => 'Str', required => 1 );
20 2 has post_url => ( is => 'ro', isa => 'Str', required => 1 );
21 2 has source_xml_tree => ( is => 'ro', isa => 'HashRef', required => 1 );
22 2 has blogger => ( is => 'ro', isa => 'Net::Google::Blogger', required => 1 );
23  
24 2 has entries => (
25 is => 'rw',
26 isa => 'ArrayRef[Net::Google::Blogger::Blog::Entry]',
27 lazy_build => 1,
28 auto_deref => 1,
29 );
30  
31 2 __PACKAGE__->meta->make_immutable;
32  
33  
34 0 ▶sub BUILDARGS {
Subroutine BUILDARGS: not called CC=1 SCAR=6.9
Pod documented
35 ## Parses source XML into initial attribute values.
36 0 my $class = shift;
37 0 my %params = @_;
38  
39 0 my $id = $params{source_xml_tree}{id}[0];
40 0 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 0 %params,
50 };
51 }
52  
53  
54 0 ▶sub _build_entries {
Subroutine _build_entries: not called CC=1 SCAR=6.9
55 ## Populates the entries attribute, loading all entries for the blog.
56 0 my $self = shift;
57  
58 # Search with no parameters.
59 0 return $self->search_entries;
60 }
61  
62  
63 0 ▶sub search_entries {
Subroutine search_entries: not called CC=6 SCAR=37.4
Pod documented
64 ## Returns entries matching search criteria.
65 0 my $self = shift;
66 0 my %params = @_;
67  
68 # Construct request URL, incorporating category criteria into it, if given.
69 0 my $url = 'http://www.blogger.com/feeds/' . $self->numeric_id . '/posts/default';
70 0 $url .= '/-/' . join '/', map URI::Escape::uri_escape($_), @{ $params{categories} }
71 0 ▶ if $params{categories};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"categories"} 0 0 0
72  
73 # Map our parameter names to Blogger's.
74 0 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 0 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 0 my %req_args = (
93 alt => 'atom',
94 );
95 0 foreach (keys %params_to_req_args_map) {
96 0 ▶ $req_args{$params_to_req_args_map{$_}} = $params{$_} if exists $params{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$_} 0 0 0
97 }
98 0 ▶ if (my $sort_mode = $params{sort_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sort_mode = $params{"sort_by"}) 0 0 0
99 0 $req_args{orderby} = $sort_mode_map{$sort_mode};
100 }
101  
102 # Execute request and parse the response.
103 0 my $response = $self->blogger->http_get($url, %req_args);
104 0 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 0 @{ $response_tree->{entry} };
113 0 ▶ return wantarray ? @entries : \@entries;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
114 }
115  
116  
117 0 ▶sub add_entry {
Subroutine add_entry: not called CC=2 SCAR=17.9
Pod documented
118 ## Adds given entry to the blog.
119 0 my $self = shift;
120 0 my ($entry) = @_;
121  
122 0 my $response = $self->blogger->http_post(
123 $self->post_url,
124 'Content-Type' => 'application/atom+xml',
125 Content => $entry->as_xml,
126 );
127  
128 0 ▶ die 'Unable to add entry to blog: ' . $response->status_line unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
129 0 $entry->update_from_http_response($response);
130  
131 0 push @{ $self->entries }, $entry;
132 0 return $entry;
133 }
134  
135  
136 0 ▶sub delete_entry {
Subroutine delete_entry: not called CC=2 SCAR=17.9
Pod documented
137 ## Deletes given entry from server as well as list of entries held in blog object.
138 0 my $self = shift;
139 0 my ($entry) = @_;
140  
141 0 my $response = $self->blogger->http_post(
142 $entry->edit_url,
143 'X-HTTP-Method-Override' => 'DELETE',
144 );
145 0 ▶ die 'Could not delete entry from server: ' . $response->status_line unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
146  
147 0 $self->entries([ grep $_ ne $entry, $self->entries ]);
148 }
149  
150  
151 0 ▶sub destroy {
Subroutine destroy: not called CC=2 SCAR=17.9
Pod documented
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 0 my $self = shift;
156  
157 0 $_->blog(undef) foreach $self->entries;
158 }
159  
160  
161 2 1;
162  
163 __END__
↑ Summary Blogger/Blog/Entry.pm »