FeedParser.pm

filter: Statementstmt 40.4% 36 / 89 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 71.4% 10 / 14 Podpod 100% 1 / 1 totaltotal 39.1% 47 / 120 CC 14 SCAR 44.8
CC
14
Cov
34%
parse
182.0
__ANON__
3.7
__ANON__
2.0
CRAP 88.4
↑ Summary Feed.pm »
1 package Search::OpenSearch::FeedParser;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Types::Standard qw( ArrayRef Bool );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Search::OpenSearch::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use XML::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Transformer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Search::Tools::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 has 'debug' =>
14 0 ▶ ( is => 'rw', isa => Bool, default => sub { $ENV{SOS_DEBUG} || 0 } );
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"SOS_DEBUG"} or 0 0 0 0
15 has 'fields' => (
16 is => 'rw',
17 isa => ArrayRef,
18 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 0 [qw( title id link summary modified tags category author issued )];
20 },
21 1 );
22  
23 =head1 NAME
24  
25 Search::OpenSearch::FeedParser - parse Search::OpenSearch::Response::XML
26  
27 =cut
28  
29 1 our $VERSION = '0.101';
30  
31 1 my $XMLer = Search::Tools::XML->new();
32 my $XML_ESCAPER = Data::Transformer->new(
33 0 ▶ normal => sub { local ($_) = shift; $$_ = $XMLer->escape($$_); } );
Subroutine __ANON__: not called CC=1 SCAR=2.6
34  
35 =head1 SYNOPSIS
36  
37 use Search::OpenSearch::FeedParser;
38 my $parser = Search::OpenSearch::FeedParser->new();
39 my $feed = $parser->parse( $sos_response );
40  
41 =head1 DESCRIPTION
42  
43 Search::OpenSearch::FeedParser is the client parser for Search::OpenSearch::Response::XML.
44 It is similar in concept to WWW::OpenSearch but because the SOS::Response::XML format
45 is a superset of the OpenSearch standard, this module implements a different API.
46  
47 =head1 METHODS
48  
49 =head2 new( I<args> )
50  
51 Instantiate a new FeedParser object. You can re-use a single FeedParser object to parse
52 multiple responses.
53  
54 I<args> should be a hash of key/value pairs, including:
55  
56 =over
57  
58 =item debug I<bool>
59  
60 =item fields I<arrayref>
61  
62 =back
63  
64 =cut
65  
66 =head2 debug([I<bool>])
67  
68 Get/set the debug flag for the parser.
69  
70 =head2 fields
71  
72 Get/set the arrayref of field names the parser should expect in each response.
73  
74 =head2 parse( I<sos_response> )
75  
76 Parse the contents of I<sos_response> and return a Search::OpenSearch::Feed object.
77 I<sos_response> may be a string of XML or a Search::OpenSearch::Response::XML object.
78  
79 =cut
80  
81 0 ▶sub parse {
Subroutine parse: not called CC=13 SCAR=52.0
Pod documented
82 0 my $self = shift;
83 0 ▶ my $resp = shift or croak "sos_response required";
Branch: 1 branch
BranchTrueFalseTotal
my $resp = shift() or croak("sos_response required") 0 0 0
84  
85 # if it is an object, stringify it
86 0 my $xml = "$resp";
87  
88 0 my $fields = $self->fields;
89 0 my %feed;
90 0 my $xfeed = XML::Feed->parse( \$xml );
91 0 ▶ if ( !$xfeed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xfeed) 0 0 0
92 0 croak "invalid XML response: " . XML::Feed->errstr;
93 }
94  
95 #dump $xfeed;
96  
97 #
98 # we must re-escape the XML content since the feed parser
99 # and XML::Simple will escape values automatically
100 #
101 0 my @entries;
102 0 for my $item ( $xfeed->entries ) {
103 0 my $e = {};
104 0 for my $f (@$fields) {
105 0 $e->{$f} = $item->$f;
106 0 ▶ if ( blessed( $e->{$f} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($e->{$f})) { } 0 0 0
107  
108 #dump( $e->{$f} );
109 0 ▶ if ( $e->{$f}->isa('XML::Feed::Content') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->{$f}->isa('XML::Feed::Content')) { } 0 0 0
elsif ($e->{$f}->isa('DateTime')) { } 0 0 0
110 0 $e->{$f} = $XMLer->escape( $e->{$f}->body );
111 }
112 elsif ( $e->{$f}->isa('DateTime') ) {
113 0 $e->{$f} = $e->{$f}->epoch;
114 }
115 }
116 else {
117 0 $e->{$f} = $XMLer->escape( $e->{$f} );
118 }
119 }
120  
121 #dump $e;
122 0 my $content = $item->content;
123 0 my $xfields = XMLin( $content->body, NoAttr => 0 );
124  
125 #dump $fields;
126  
127 0 for my $f ( keys %$xfields ) {
128 0 $e->{$f} = $xfields->{$f};
129 0 ▶ if ( ref $e->{$f} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e->{$f}) { } 0 0 0
130 0 $XML_ESCAPER->traverse( $e->{$f} );
131 }
132 else {
133 0 $e->{$f} = $XMLer->escape( $e->{$f} );
134 }
135 }
136  
137 # massage some field names
138 0 $e->{mtime} = delete $e->{modified};
139 0 $e->{uri} = delete $e->{id};
140  
141 #dump $content;
142 #dump $e;
143 0 push @entries, $e;
144  
145 }
146  
147 # facets require digging into the raw xml
148 0 my $xml_feed = XMLin( $xfeed->as_xml, NoAttr => 0 );
149  
150 #dump($xml_feed);
151  
152 # must turn facets inside out in order
153 # to aggregate counts correctly
154 0 my %facets;
155 0 ▶ if ( $xml_feed->{category}->{sos}->{facets} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml_feed->{"category"}{"sos"}{"facets"}) 0 0 0
156 0 my $facet_feed = $xml_feed->{category}->{sos}->{facets};
157 0 for my $name ( keys %$facet_feed ) {
158 0 for my $facet ( @{ $facet_feed->{$name}->{$name} } ) {
159 0 $facets{$name}->{ $facet->{term} } += $facet->{count};
160 }
161 }
162 }
163  
164 0 $feed{total} = $xml_feed->{'opensearch:totalResults'};
165 0 $feed{query} = $xml_feed->{'opensearch:Query'}->{searchTerms};
166 0 $feed{page_size} = $xml_feed->{'opensearch:itemsPerPage'};
167 0 $feed{offset} = $xml_feed->{'opensearch:startIndex'};
168 0 $feed{updated} = $xml_feed->{updated};
169 0 $feed{title} = $xml_feed->{title};
170 0 $feed{id} = $xml_feed->{id};
171 0 $feed{build_time} = $xml_feed->{category}->{sos}->{build_time};
172 0 $feed{search_time} = $xml_feed->{category}->{sos}->{search_time};
173 0 $feed{suggestions} = $xml_feed->{category}->{sos}->{suggestions};
174 0 $feed{facets} = \%facets;
175 0 $feed{entries} = \@entries;
176 0 return Search::OpenSearch::Feed->new(%feed);
177 }
178  
179 1 1;
180  
181 __END__
↑ Summary Feed.pm »