NewsReach/NewsItem.pm

filter: Statementstmt 50.7% 32 / 63 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 8 / 10 Podpod 100% 1 / 1 totaltotal 55.4% 41 / 74 CC 6 SCAR 28.6
CC
6
Cov
51%
new_from_xml
42.0
_get_related_xml
2.0
BEGIN
1.0
CRAP 17.5
↑ Summary NewsReach/Photo.pm »
1 # ABSTRACT: Model a news article in the NewsReach API
2 package WWW::NewsReach::NewsItem;
3  
4 3 our $VERSION = '0.06';
5  
6 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use WWW::NewsReach::Photo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use WWW::NewsReach::Category;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use WWW::NewsReach::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use WWW::NewsReach::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 has $_ => (
18 is => 'ro',
19 isa => 'Str',
20 3 ) for qw(headline text state extract);
21  
22 has $_ => (
23 is => 'ro',
24 isa => 'DateTime',
25 3 ) for qw(publishDate lastModifiedDate);
26  
27 3 has id => (
28 is => 'ro',
29 isa => 'Int',
30 );
31  
32 3 has photos => (
33 is => 'ro',
34 isa => 'ArrayRef[WWW::NewsReach::Photo]',
35 );
36  
37 3 has categories => (
38 is => 'ro',
39 isa => 'ArrayRef[WWW::NewsReach::Category]',
40 );
41  
42 3 has comments => (
43 is => 'ro',
44 isa => 'ArrayRef[WWW::NewsReach::Comment]',
45 );
46  
47  
48 0 ▶sub new_from_xml {
Subroutine new_from_xml: not called CC=6 SCAR=37.4
Pod documented
49 0 my $class = shift;
50 0 my ( $xml ) = @_;
51  
52 0 my $self = {};
53  
54 0 foreach (qw[id headline text state extract]) {
55 # extract is optional
56 0 eval {
57 0 $self->{$_} = $xml->findnodes("//$_")->[0]->textContent;
58 };
59 }
60  
61 0 my $iso8601 = DateTime::Format::ISO8601->new;
62 0 foreach (qw[publishDate lastModifiedDate]) {
63 0 my $dt_str = $xml->findnodes("//$_")->[0]->textContent;
64 0 my $dt = $iso8601->parse_datetime( $dt_str );
65 0 $self->{$_} = $dt;
66 }
67  
68 0 my $photo_xml = $class->_get_related_xml( $xml, 'photos' );
69 0 foreach ( $photo_xml->findnodes('//photo') ) {
70 0 push @{$self->{photos}},
71 WWW::NewsReach::Photo->new_from_xml( $_ );
72 }
73  
74 0 my $comment_xml = $class->_get_related_xml( $xml, 'comments' );
75 0 foreach ( $comment_xml->findnodes('//commentListItem') ) {
76 0 push @{$self->{Comment}},
77 WWW::NewsReach::Comment->new_from_xml( $_ );
78 }
79  
80 0 my $category_xml = $class->_get_related_xml( $xml, 'categories' );
81 0 foreach ( $category_xml->findnodes('//category') ) {
82 0 push @{$self->{categories}},
83 WWW::NewsReach::Category->new_from_xml( $_ );
84 }
85  
86 0 return $class->new( $self );
87 }
88  
89 0 ▶sub _get_related_xml {
Subroutine _get_related_xml: not called CC=1 SCAR=6.9
90 0 my $class = shift;
91 0 my ( $xml, $type ) = @_;
92  
93 0 my $url = $xml->findnodes("//$type/".'@href')->[0]->textContent;
94  
95 0 my $ua = WWW::NewsReach::Client->new;
96 0 my $resp = $ua->request( $url );
97 0 my $related_xml = XML::LibXML->new->parse_string( $resp );
98  
99 0 return $related_xml;
100 }
101  
102 3 1;
103  
104 __END__
↑ Summary NewsReach/Photo.pm »