NewsReach.pm

filter: Statementstmt 48.2% 27 / 56 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 54.5% 6 / 11 Podpod 50.0% 1 / 2 totaltotal 47.8% 34 / 71 CC 3 SCAR 13.5
CC
3
Cov
47%
get_news
12.0
BUILD
2.0
_build_ua
2.0
CRAP 3.8
« NewsReach/Category.pm ↑ Summary NewsReach/Photo/Instance.pm »
1 # ABSTRACT: Perl wrapper for the NewsReach API
2  
3 package WWW::NewsReach;
4  
5 3 our $VERSION = '0.06';
6  
7 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Data::Dump qw( pp );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use WWW::NewsReach::NewsItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use WWW::NewsReach::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 my $API_BASE = 'http://api.newsreach.co.uk/';
17  
18 3 has api_key => (
19 is => 'ro',
20 isa => 'Str',
21 required => 1,
22 );
23  
24 3 has ua => (
25 is => 'ro',
26 isa => 'WWW::NewsReach::Client',
27 lazy_build => 1,
28 );
29  
30 3 has api_url => (
31 is => 'ro',
32 isa => 'Str',
33 lazy_build => 1,
34 );
35  
36 3 has news_url => (
37 is => 'rw',
38 isa => 'Str',
39 required => 0,
40 );
41  
42 3 has comments_url => (
43 is => 'rw',
44 isa => 'Str',
45 required => 0,
46 );
47  
48 3 has categories_url => (
49 is => 'rw',
50 isa => 'Str',
51 required => 0,
52 );
53  
54 # Construct some of the API URLs based on the root API response
55 # This is definitely slow but it's what they recommend in the docs:
56 #
57 # "It doesn't intend to imply how the API should be accessed.
58 # Your code should only have knowledge about the Base URL and API Key.
59 # Do not hard code resource URL logic into your software. Rather it should
60 # be discovered from the Root of the API."
61 #
62 # http://support.newsreach.co.uk/index.php?title=Reference
63  
64 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my $self = shift;
66 0 my ( $args ) = @_;
67  
68 0 my $resp = $self->ua->request( $self->api_url );
69 0 my $xp = XML::LibXML->new->parse_string( $resp );
70 # Use XPath to find the element attributes from the XML response
71 0 my $news_url = $xp->findnodes('//news/@href')->[0]->textContent;
72 0 my $comments_url = $xp->findnodes('//comments/@href')->[0]->textContent;
73 0 my $categories_url = $xp->findnodes(
74 '//categoryDefinitions/@href'
75 )->[0]->textContent;
76  
77 0 $self->news_url( $news_url );
78 0 $self->comments_url( $comments_url );
79 0 $self->categories_url( $categories_url );
80  
81 }
82  
83 0 ▶sub _build_ua {
Subroutine _build_ua: not called CC=1 SCAR=6.9
84 0 my $self = shift;
85  
86 0 return WWW::NewsReach::Client->new;
87 }
88  
89 0 ▶sub _build_api_url {
Subroutine _build_api_url: not called CC=1 SCAR=6.9
90 0 my $self = shift;
91  
92 0 return $API_BASE . $self->api_key;
93 }
94  
95  
96  
97 0 ▶sub get_news {
Subroutine get_news: not called CC=3 SCAR=24.8
Pod documented
98 0 my $self = shift;
99  
100 0 my $resp = $self->ua->request( $self->news_url );
101  
102 0 my $news;
103 0 my $xp = XML::LibXML->new->parse_string( $resp );
104 0 foreach ($xp->findnodes('//newsListItem')) {
105 0 my $id = $_->find('id')->[0]->textContent;
106 0 my $xml = $self->_get_news_item_xml( $id );
107 0 push @$news, WWW::NewsReach::NewsItem->new_from_xml( $xml );
108 }
109  
110 0 ▶ return wantarray ? @$news : $news;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
111 }
112  
113 0 ▶sub _get_news_item_xml {
Subroutine _get_news_item_xml: not called CC=1 SCAR=6.9
114 0 my $self = shift;
115 0 my ( $id ) = @_;
116  
117 0 my $news_item_url = $self->news_url . $id;
118 0 my $resp = $self->ua->request( $news_item_url );
119 0 my $xml = XML::LibXML->new->parse_string( $resp );
120  
121 0 return $xml;
122 }
123  
124  
125 3 1;
126  
127 __END__
« NewsReach/Category.pm ↑ Summary NewsReach/Photo/Instance.pm »