blib/lib/XML/RSS/FromAtom.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 ########################################################################
2 #
3 # XML::RSS::FromAtom
4 #
5 # Copyright 2005, Marcus Thiesen (marcus@thiesen.org) All rights reserved.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of either:
9 #
10 # a) the GNU General Public License as published by the Free Software
11 # Foundation; either version 1, or (at your option) any later
12 # version, or
13 #
14 # b) the "Artistic License" which comes with Perl.
15 #
16 # On Debian GNU/Linux systems, the complete text of the GNU General
17 # Public License can be found in `/usr/share/common-licenses/GPL' and
18 # the Artistic Licence in `/usr/share/common-licenses/Artistic'.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 #
24 ########################################################################
25  
26 package XML::RSS::FromAtom;
27  
28 2 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use DateTime::Format::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 ▶use XML::Atom::Syndication::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 0 ▶use XML::RSS;
Subroutine BEGIN: called CC=1 SCAR=2.6
40  
41 # Version set by dist.ini; do not change here.
42 our $VERSION = '0.02_03'; # VERSION
43  
44 has 'syndicator' => (
45 is => 'ro',
46 lazy => 1,
47 builder => 'build_syndicator' );
48  
49 sub build_syndicator {
50 my $self = shift;
51 return XML::Atom::Syndication::Feed->new(\$self->content);
52 }
53  
54 has 'rss_processor' => (
55 is => 'ro',
56 lazy => 1,
57 builder => 'build_rss_processor',
58 handles => [qw(add_item channel)] );
59  
60 sub build_rss_processor {
61 return XML::RSS->new(version => '2.0');
62 }
63  
64 has 'content' => (
65 is => 'rw'
66 );
67  
68 sub parse {
69 my $self = shift;
70 my $text = shift;
71  
72 $self->content( $text );
73 return $self->atom_to_rss();
74 }
75  
76 sub atom_to_rss {
77 my $self = shift;
78 my $doc = $self->syndicator();
79  
80 my $feed_title = $doc->title->body();
81 my $feed_description = $doc->subtitle->body();
82 my $feed_link = $doc->link->href();
83  
84 $self->channel(title => $feed_title) if ($feed_title);
85 $self->channel(description => $feed_description) if ($feed_description);
86 $self->channel(link => $feed_link) if ($feed_link);
87  
88 my @entries = $doc->entries();
89 for my $e (@entries) {
90 my $desc = '';
91 $desc = $e->summary->body();
92 my $content =
93 try { $e->content->body() }
94 catch { undef };
95 if (defined($content) &&
96 length($content) > length($desc)) {
97 $desc = $$content;
98 }
99  
100 my $title = $e->title->body();
101  
102 my $mod = $e->modified();
103 my $upd = $e->updated();
104 my $ts = defined($mod) ? $mod : $upd;
105 my $dt = DateTime::Format::ISO8601->parse_datetime( $ts );
106  
107 my $link = $e->link->href();
108 my $author = $e->author->name();
109  
110 $self->add_item(
111 title => $title,
112 link => $link,
113 description => $desc,
114 pubDate => DateTime::Format::Mail->format_datetime($dt),
115 author => $author ? $author : undef,
116 );
117 }
118  
119 return $self->rss_processor;
120 }
121  
122 # ABSTRACT: create a XML::RSS object out of an Atom feed
123  
124 1;
125  
126 __END__
↑ Summary