Blogger/Blog/Entry.pm

filter: Statementstmt 43.5% 27 / 62 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 33.3% 4 / 12 Podpod 50.0% 3 / 6 totaltotal 36.5% 34 / 93 CC 9 SCAR 18.5
CC
9
Cov
36%
BUILDARGS
12.0
save
12.0
source_xml_tree_to_attrs
6.0
CRAP 6.4
« Blogger/Blog.pm ↑ Summary Blogger.pm »
1 package Net::Google::Blogger::Blog::Entry;
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  
9  
10 2 our $VERSION = '0.09';
11  
12 2 has id => ( is => 'rw', isa => 'Str' );
13 2 has title => ( is => 'rw', isa => 'Str' );
14 2 has content => ( is => 'rw', isa => 'Str' );
15 2 has author => ( is => 'rw', isa => 'Str' );
16 2 has published => ( is => 'rw', isa => 'Str' );
17 2 has updated => ( is => 'rw', isa => 'Str' );
18 2 has edit_url => ( is => 'rw', isa => 'Str' );
19 2 has id_url => ( is => 'rw', isa => 'Str' );
20 2 has public_url => ( is => 'rw', isa => 'Str' );
21 0 ▶has source_xml_tree => ( is => 'rw', isa => 'HashRef', default => sub { {} }, required => 1 );
Subroutine __ANON__: not called CC=1 SCAR=1.2
22 2 has categories => ( is => 'rw', isa => 'ArrayRef[Str]', auto_deref => 1 );
23 2 has blog => ( is => 'rw', isa => 'Net::Google::Blogger::Blog', required => 1 );
24  
25 2 __PACKAGE__->meta->make_immutable;
26  
27  
28 0 ▶sub BUILDARGS {
Subroutine BUILDARGS: not called CC=3 SCAR=24.8
Pod documented
29 ## Populates object attributes from parsed XML source.
30 0 my $class = shift;
31 0 my %params = @_;
32  
33 my $attrs = $class->source_xml_tree_to_attrs($params{source_xml_tree})
34 0 ▶ if $params{source_xml_tree};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"source_xml_tree"} 0 0 0
35  
36 0 $attrs->{$_} = $params{$_} foreach keys %params;
37 0 return $attrs;
38 }
39  
40  
41 0 ▶sub source_xml_tree_to_attrs {
Subroutine source_xml_tree_to_attrs: not called CC=2 SCAR=17.9
Pod undocumented
42 ## Returns hash of attributes extracted from XML tree.
43 0 my $class = shift;
44 0 my ($tree) = @_;
45  
46 0 ▶ my $get_link_by_rel = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
47 ## Returns value for 'href' attribute for link with given 'ref' attribute, if it's present.
48 0 my ($rel_value) = @_;
49  
50 0 my ($link) = grep $_->{rel} eq $rel_value, @{ $tree->{link} };
51 0 ▶ return $link->{href} if $link;
Branch: 1 branch
BranchTrueFalseTotal
if $link 0 0 0
52 0 };
53  
54 return {
55 id => $tree->{id}[0],
56 author => $tree->{author}[0]{name}[0],
57 published => $tree->{published}[0],
58 updated => $tree->{updated}[0],
59 title => $tree->{title}[0]{content},
60 content => $tree->{content}{content},
61 public_url => $get_link_by_rel->('alternate'),
62 id_url => $get_link_by_rel->('self'),
63 edit_url => $get_link_by_rel->('edit'),
64 0 ▶ categories => [ map $_->{term}, @{ $tree->{category} || [] } ],
Condition0%0/2
$tree->{'category'} || []
l!l
00
MC/DC0%0/1
$tree->{'category'} || []
$tree->{'category'}
Truth table0%0/2
A
A= $tree->{'category'}
Aresult
11
00
65 };
66 }
67  
68  
69 0 ▶sub update_from_http_response {
Subroutine update_from_http_response: not called CC=2 SCAR=17.9
Pod undocumented
70 ## Updates entry internal structures from given HTTP
71 ## response. Used to update entry after it's been created on the
72 ## server.
73 0 my $self = shift;
74 0 my ($response) = @_;
75  
76 0 my $xml_tree = XML::Simple::XMLin($response->content, ForceArray => 1);
77 0 $self->source_xml_tree($xml_tree);
78  
79 0 my $new_attrs = $self->source_xml_tree_to_attrs($xml_tree);
80 0 $self->$_($new_attrs->{$_}) foreach keys %$new_attrs;
81 }
82  
83  
84 0 ▶sub as_xml {
Subroutine as_xml: not called CC=2 SCAR=17.9
Pod undocumented
85 ## Returns XML string representing the entry.
86 0 my $self = shift;
87  
88 # Add namespace specifiers to the root element, which appears to be undocumented requirement.
89 0 $self->source_xml_tree->{xmlns} = 'http://www.w3.org/2005/Atom';
90 0 ▶ $self->source_xml_tree->{'xmlns:thr'} = 'http://purl.org/rss/1.0/modules/threading/' if $self->id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->id 0 0 0
91  
92 # Place attribute values into original data tree. Don't generate an Atom entry anew as
93 # Blogger wants us to preserve all original data when updating posts.
94 0 $self->source_xml_tree->{title}[0] = {
95 content => $self->title,
96 type => 'text',
97 };
98 $self->source_xml_tree->{content} = {
99 0 content => $self->content,
100 type => 'html',
101 };
102 $self->source_xml_tree->{category} = [
103 0 map {
104 scheme => 'http://www.blogger.com/atom/ns#',
105 term => $_,
106 },
107 $self->categories
108 ];
109  
110 # Convert data tree to XML.
111 0 return XML::Simple::XMLout($self->source_xml_tree, RootName => 'entry');
112 }
113  
114  
115 0 ▶sub save {
Subroutine save: not called CC=3 SCAR=24.8
Pod documented
116 ## Saves the entry to blogger.
117 0 my $self = shift;
118  
119 0 ▶ if ($self->id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->id) { } 0 0 0
120 # Update the entry.
121 0 my $response = $self->blog->blogger->http_put($self->edit_url => $self->as_xml);
122 0 ▶ die 'Unable to save entry: ' . $response->status_line unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
123 }
124 else {
125 # Create new entry.
126 0 $self->blog->add_entry($self);
127 }
128 }
129  
130  
131 0 ▶sub delete {
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
132 ## Deletes the entry from server.
133 0 my $self = shift;
134  
135 0 $self->blog->delete_entry($self);
136 }
137  
138  
139 2 1;
140  
141 __END__
« Blogger/Blog.pm ↑ Summary Blogger.pm »