File Coverage

blib/lib/WebService/Bloglines/Entries.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package WebService::Bloglines::Entries;
2              
3 4     4   21 use vars qw($VERSION);
  4         8  
  4         221  
4             $VERSION = 0.09;
5              
6 4     4   21 use strict;
  4         8  
  4         118  
7 4     4   26 use Carp;
  4         7  
  4         287  
8 4     4   3868 use Encode;
  4         52518  
  4         397  
9 4     4   4894 use XML::RSS::LibXML;
  0            
  0            
10             use XML::LibXML;
11              
12             sub parse {
13             my($class, $xml, $liberal) = @_;
14              
15             # temporary workaround till Bloglines fixes this bug
16             $xml =~ s!(.*?)!encode_xml($1)!eg;
17              
18             # okay, Bloglines has sometimes include \xEF in their feeds and
19             # that can't be decoded as UTF-8. Trying to fix it by roundtrips
20             $xml = Encode::decode_utf8($xml);
21             $xml = Encode::encode_utf8($xml);
22              
23             my $parser;
24             if ($liberal) {
25             eval { require XML::Liberal };
26             if ($@) {
27             croak "XML::Liberal is not installed: $@";
28             }
29             $parser = XML::Liberal->new('LibXML');
30             } else {
31             $parser = XML::LibXML->new;
32             }
33              
34             my $doc = $parser->parse_string($xml);
35             my $rssparent = $doc->find("/rss")->get_node(0);
36             my $channelnode = $doc->find("/rss/channel");
37             $rssparent->removeChildNodes();
38              
39             my @entries;
40             for my $node ($channelnode->get_nodelist()) {
41             my $xml = $rssparent->toString();
42             my $channel = $node->toString();
43             $xml =~ s!<(rss.*?)/>$!<$1>\n$channel\n!; # wooh
44             push @entries, $class->new($xml);
45             }
46             return wantarray ? @entries : $entries[0];
47             }
48              
49             my %Map = ('&' => '&', '"' => '"',
50             '<' => '<', '>' => '>',
51             '\'' => ''');
52             my $RE = join '|', keys %Map;
53              
54             sub encode_xml {
55             my $str = shift;
56             $str =~ s!($RE)!$Map{$1}!g;
57             $str;
58             }
59              
60             sub new {
61             my($class, $xml) = @_;
62             my $self = bless {
63             _xml => $xml,
64             }, $class;
65             $self->_parse_xml();
66             $self;
67             }
68              
69             sub _parse_xml {
70             my $self = shift;
71              
72             my $rss = XML::RSS::LibXML->new();
73             $rss->add_module(prefix => "bloglines", uri => "http://www.bloglines.com/services/module");
74             $rss->parse($self->{_xml});
75             $self->{_rss} = $rss;
76             }
77              
78             sub feed {
79             my $self = shift;
80             return $self->{_rss}->{channel};
81             }
82              
83             sub items {
84             my $self = shift;
85             return wantarray ? @{$self->{_rss}->{items}} : $self->{_rss}->{items};
86             }
87              
88             1;
89