blib/lib/XML/FeedPP/Plugin/DumpJSON.pm

filter: Statementstmt 60.4% 75 / 124 Branchbran 40.3% 25 / 62 Conditioncond 53.3% 8 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 73.3% 11 / 15 Podpod 0.0% 0 / 9 totaltotal 51.7% 121 / 234 CC 40 SCAR 32.0
CC
40
Cov
54%
dump_json
71.5
dump_json_pp2
30.0
slim_feed
13.0
CRAP 24.5
↑ Summary
1 =head1 NAME
2  
3 XML::FeedPP::Plugin::DumpJSON - FeedPP Plugin for generating JSON
4  
5 =head1 SYNOPSIS
6  
7 use XML::FeedPP;
8 my $feed = XML::FeedPP->new( 'index.rss' );
9 $feed->limit_item( 10 );
10 $feed->call( DumpJSON => 'index-rss.json' );
11  
12 =head1 DESCRIPTION
13  
14 This plugin generates a JSON data representation.
15  
16 =head1 FILE OR STRING
17  
18 If a JSON filename is C<undef> or C<''>, this module returns a JSON
19 string instead of generating a JSON file.
20  
21 $feed->call( DumpJSON => 'feed.json' ); # generates a JSON file
22 my $json = $feed->call( 'DumpJSON' ); # returns a JSON string
23  
24 =head1 OPTIONS
25  
26 This plugin allows some optoinal arguments following:
27  
28 my %opt = (
29 slim => 1,
30 slim_element_add => [ 'media:thumbnail@url' ],
31 slim_element => [ 'link', 'title', 'pubDate' ],
32 );
33 my $json = $feed->call( DumpJSON => %opt );
34  
35 =head2 slim
36  
37 This plugin converts the whole feed into JSON format by default.
38 All elements and attribuets are included in a JSON generated.
39 If this boolean is true, some limited elements are only included.
40  
41 =head2 slim_element_add
42  
43 An array reference for element/attribute names
44 which is given by set()/get() method's format.
45 These elements/attributes are also appended for slim JSON.
46  
47 =head2 slim_element
48  
49 An array reference for element/attribute names.
50 The default list of limited elements is replaced by this value.
51  
52 =head1 MODULE DEPENDENCIES
53  
54 L<XML::FeedPP>, L<XML::TreePP> and L<JSON::Syck>
55  
56 =head1 SEE ALSO
57  
58 JSON, JavaScript Object Notation:
59 L<http://www.json.org/>
60  
61 =head1 AUTHOR
62  
63 Yusuke Kawasaki, http://www.kawa.net/
64  
65 =head1 COPYRIGHT AND LICENSE
66  
67 Copyright (c) 2006-2008 Yusuke Kawasaki. All rights reserved.
68 This program is free software; you can redistribute it
69 and/or modify it under the same terms as Perl itself.
70  
71 =cut
72 # ----------------------------------------------------------------
73 package XML::FeedPP::Plugin::DumpJSON;
74 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 4 ▶use vars qw( @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
76 4 @ISA = qw( XML::FeedPP::Plugin );
77 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 4 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 4 require 5.008;
80 4 ▶use JSON::Syck;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 # use JSON::PP;
82 # use JSON::XS;
83  
84 4 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
85 4 $VERSION = "0.33";
86  
87 4 *XML::FeedPP::to_json = \&to_json;
88  
89 4 my $SLIM_ELEM = [qw(
90 link title pubDate dc:date modified issued dc:subject category
91 image/url media:content@url media:thumbnail@url
92 )];
93 4 my $DEFAULT_OPTS = {
94 slim_element => undef,
95 slim_element_add => undef,
96 utf8_flag => undef,
97 use_json_syck => 1,
98 use_json_pp => undef,
99 };
100  
101 13 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
102 13 my $class = shift;
103 13 my $feed = shift;
104 13 &to_json( $feed, @_ );
105 }
106  
107 13 ▶sub to_json {
Subroutine to_json: called CC=7 SCAR=19.8
Pod undocumented
108 13 my $data = shift;
109 13 ▶ my $file = shift if scalar @_ % 2; # odd arguments
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ % 2 2 11 13
110 13 my $opts = { %$DEFAULT_OPTS, @_ };
111 13 ▶ $file = $opts->{file} if exists $opts->{file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"file"} 0 13 13
112  
113 # cut some elements out
114 13 ▶ if ( $opts->{slim} || $opts->{slim_element} || $opts->{slim_element_add} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"slim"} or $opts->{"slim_element"} or $opts->{"slim_element_add"}) 6 7 13
Condition83%5/6
$opts->{"slim"} or $opts->{"slim_element"} or $opts->{"slim_element_add"}
l!l&&r!l&&!r
607
$opts->{"slim"} or $opts->{"slim_element"}
l!l&&r!l&&!r
337
MC/DC66%2/3
$opts->{"slim"} or $opts->{"slim_element"} or $opts->{"slim_element_add"}
$opts->{"slim"} $opts->{"slim_element"} $opts->{"slim_element_add"}
Truth table75%3/4
A or B or C
A= $opts->{"slim"}B= $opts->{"slim_element"}C= $opts->{"slim_element_add"}
ABCresult
1XX1
01X1
0011
0000
115 6 $data = &slim_feed( $data, $opts->{slim_element}, $opts->{slim_element_add} );
116 }
117  
118 # perl object to json
119 13 my $json = &dump_json( $data, $opts );
120  
121 # json to file
122 13 ▶ if ( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 0 13 13
123 0 &write_file( $file, $json, $opts );
124 }
125 13 $json;
126 }
127  
128 0 ▶sub write_file {
Subroutine write_file: not called CC=2 SCAR=17.9
Pod undocumented
129 0 my $file = shift;
130 0 my $fh = Symbol::gensym();
131 0 ▶ open( $fh, ">$file" ) or Carp::croak "$! - $file";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">$file" or Carp::croak("$! - $file") 0 0 0
132 0 print $fh @_;
133 0 close($fh);
134 }
135  
136 13 ▶sub dump_json {
Subroutine dump_json: called CC=12 SCAR=42.7
Pod undocumented
137 13 my $data = shift;
138 13 my $opts = shift;
139  
140 13 my $usesyck = $opts->{use_json_syck};
141 13 my $usepp = $opts->{use_json_pp};
142 13 ▶ $usesyck = 1 unless $usepp;
Branch: 1 branch
BranchTrueFalseTotal
unless $usepp 13 0 13
143 13 ▶ $usepp = 1 unless $usesyck;
Branch: 1 branch
BranchTrueFalseTotal
unless $usesyck 0 13 13
144  
145 13 ▶ if ( $usesyck && defined $JSON::Syck::VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usesyck and defined $JSON::Syck::VERSION) 13 0 13
Condition33%1/3
$usesyck and defined $JSON::Syck::VERSION
!ll&&!rl&&r
0013
MC/DC0%0/2
$usesyck and defined $JSON::Syck::VERSION
$usesyck defined $JSON::Syck::VERSION
Truth table33%1/3
A and B
A= $usesyckB= defined $JSON::Syck::VERSION
ABresult
0X0
100
111
146 13 return &dump_json_syck($data,$opts);
147 }
148 0 ▶ if ( $usepp && defined $JSON::VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usepp and defined $JSON::VERSION) 0 0 0
Condition0%0/3
$usepp and defined $JSON::VERSION
!ll&&!rl&&r
000
MC/DC0%0/2
$usepp and defined $JSON::VERSION
$usepp defined $JSON::VERSION
Truth table0%0/3
A and B
A= $useppB= defined $JSON::VERSION
ABresult
0X0
100
111
149 0 return &dump_json_pm($data,$opts);
150 }
151 0 ▶ if ( $usesyck ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usesyck) 0 0 0
152 0 local $@;
153 0 eval { require JSON::Syck; };
154 0 ▶ return &dump_json_syck($data,$opts) unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
155 }
156 0 ▶ if ( $usepp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usepp) 0 0 0
157 0 local $@;
158 0 eval { require JSON; };
159 0 ▶ return &dump_json_pm($data,$opts) unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
160 }
161 0 ▶ if ( $usepp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usepp) { } 0 0 0
162 0 Carp::croak "JSON::PP or JSON::Syck is required";
163 }
164 else {
165 0 Carp::croak "JSON::Syck is required";
166 }
167 }
168  
169 13 ▶sub dump_json_syck {
Subroutine dump_json_syck: called CC=2 SCAR=7.0
Pod undocumented
170 13 my $data = shift;
171 13 my $opts = shift;
172 # warn "[JSON::Syck $JSON::Syck::VERSION]\n";
173 13 ▶ local $JSON::Syck::ImplicitUnicode = $opts->{utf8_flag} if exists $opts->{utf8_flag};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"utf8_flag"} 13 0 13
174 # local $JSON::Syck::SingleQuote = 0;
175 13 JSON::Syck::Dump($data);
176 }
177  
178 0 ▶sub dump_json_pm {
Subroutine dump_json_pm: not called CC=3 SCAR=24.8
Pod undocumented
179 0 my $data = shift;
180 0 my $opts = shift;
181  
182 0 my $ver = ( $JSON::VERSION =~ /^([\d\.]+)/ )[0];
183 0 ▶ Carp::croak "JSON::PP is not correctly loaded." unless $ver;
Branch: 1 branch
BranchTrueFalseTotal
unless $ver 0 0 0
184 0 ▶ return &dump_json_pp1($data,$opts) if ( $ver < 1.99 );
Branch: 1 branch
BranchTrueFalseTotal
if $ver < "1.99" 0 0 0
185 0 return &dump_json_pp2($data,$opts);
186 }
187  
188 0 ▶sub dump_json_pp2 {
Subroutine dump_json_pp2: not called CC=5 SCAR=34.0
Pod undocumented
189 0 my $data = shift;
190 0 my $opts = shift;
191 0 ▶ if ( ! defined $JSON::PP::VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $JSON::PP::VERSION) 0 0 0
192 0 local $@;
193 0 eval { require JSON::PP; };
194 0 ▶ Carp::croak "JSON::PP is required" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
195 }
196 # warn "[JSON::PP $JSON::PP::VERSION]\n";
197 0 my $json = JSON::PP->new();
198 0 ▶ my $utf8 = $opts->{utf8_flag} if exists $opts->{utf8_flag};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"utf8_flag"} 0 0 0
199 0 ▶ my $bool = $utf8 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$utf8 ? : 0 0 0
200 0 $json->utf8($bool);
201 0 $json->allow_blessed(1);
202 0 $json->as_nonblessed(1);
203 0 $json->encode($data);
204 }
205  
206 0 ▶sub dump_json_pp1 {
Subroutine dump_json_pp1: not called CC=3 SCAR=24.8
Pod undocumented
207 0 my $data = shift;
208 0 my $opts = shift;
209 # warn "[JSON $JSON::VERSION]\n";
210 0 my $json = JSON->new();
211 0 ▶ my $utf8 = $opts->{utf8_flag} if exists $opts->{utf8_flag};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"utf8_flag"} 0 0 0
212 0 ▶ local $JSON::UTF8 = $utf8 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$utf8 ? : 0 0 0
213 0 $json->convblessed(1);
214 0 $json->objToJson($data)
215 }
216  
217 6 ▶sub slim_feed {
Subroutine slim_feed: called CC=13 SCAR=25.7
Pod undocumented
218 6 my $feed = shift;
219 6 ▶ my $list = shift || $SLIM_ELEM;
Condition66%2/3
shift() || $SLIM_ELEM
l!l&&r!l&&!r
330
MC/DC0%0/2
shift() || $SLIM_ELEM
shift() $SLIM_ELEM
Truth table66%2/3
A || B
A= shift()B= $SLIM_ELEM
ABresult
1X1
011
000
220 6 my $add = shift;
221 6 my $slim = {};
222 6 my $root = ( keys %$feed )[0];
223 6 ▶ if ( ref $add ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $add) 3 3 6
224 3 $list = [ @$list, @$add ];
225 }
226 6 my $channel = {};
227 6 foreach my $key ( @$list ) {
228 45 ▶ my $val = ( $key eq "link" ) ? $feed->link() : $feed->get($key);
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'link' ? : 6 39 45
229 45 ▶ $channel->{$key} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 15 30 45
230 }
231 6 my $entries = [];
232 6 foreach my $item ( $feed->get_item() ) {
233 12 my $hash = {};
234 12 foreach my $key ( @$list ) {
235 90 ▶ my $val = ( $key eq "link" ) ? $item->link() : $item->get($key);
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'link' ? : 12 78 90
236 90 ▶ $hash->{$key} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 32 58 90
237 }
238 12 push( @$entries, $hash );
239 }
240 6 my $data;
241 6 ▶ if ( $root eq 'rss' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($root eq 'rss') { } 2 4 6
elsif ($root eq 'rdf:RDF') { } 2 2 4
elsif ($root eq 'feed') { } 2 0 2
242 2 $channel->{item} = $entries;
243 2 $data = { rss => { channel => $channel }};
244 }
245 elsif ( $root eq 'rdf:RDF' ) {
246 2 $data = { 'rdf:RDF' => { channel => $channel, item => $entries }};
247 }
248 elsif ( $root eq 'feed' ) {
249 2 $channel->{entry} = $entries;
250 2 $data = { feed => $channel };
251 }
252 else {
253 0 Carp::croak "Invalid feed type: $root";
254 }
255 6 $data;
256 }
257  
258 # ----------------------------------------------------------------
259 4 1;
260 # ----------------------------------------------------------------
↑ Summary