File Coverage

blib/lib/AnyEvent/Superfeedr/Notification.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package AnyEvent::Superfeedr::Notification;
2 1     1   7 use strict;
  1         1  
  1         52  
3 1     1   6 use warnings;
  1         2  
  1         38  
4 1     1   797 use XML::Atom::Entry;
  0            
  0            
5             use XML::Atom::Feed;
6             use AnyEvent::Superfeedr();
7              
8             use Object::Tiny qw{
9             http_status
10             next_fetch
11             feed_uri
12             items
13             title
14             _node_entries
15             _atom_entries
16             };
17              
18             sub node_entries {
19             my $notification = shift;
20             my $node_entries = $notification->_node_entries;
21             return @$node_entries if $node_entries;
22              
23             my @node_entries;
24             for my $item (@{ $notification->items }) {
25             ## each item as one entry
26             my ($node_entry) = $item->nodes;
27             push @node_entries, $node_entry;
28             }
29             $notification->{items} = undef;
30             $notification->{_node_entries} = \@node_entries;
31             return @node_entries;
32             }
33              
34             sub entries {
35             my $notification = shift;
36             my $atom_entries = $notification->_atom_entries;
37             return @$atom_entries if $atom_entries;
38              
39             my @atom_entries;
40             for my $node_entry ($notification->node_entries) {
41             my $str = $node_entry->as_string;
42             my $atom_entry = XML::Atom::Entry->new(Stream => \$str);
43             push @atom_entries, $atom_entry;
44             }
45             return @{$notification->{_atom_entries} } = @atom_entries;
46             }
47              
48             sub as_atom_feed {
49             my $notification = shift;
50             my $feed = XML::Atom::Feed->new;
51             for ($notification->entries) {
52             $feed->add_entry($_);
53             }
54             return $feed;
55             }
56              
57             sub as_xml {
58             my $notification = shift;
59             my $id = $notification->tagify;
60             my $feed_uri = _xml_encode($notification->feed_uri);
61             my $title = _xml_encode($notification->title);
62             my $now = _now_as_w3c();
63             my $feed = <
64            
65            
66             $id
67             $title
68             $now
69            
70             EOX
71             for my $node_entry ($notification->node_entries) {
72             $feed .= $node_entry->as_string;
73             }
74             $feed .= "";
75             return $feed;
76             }
77              
78             sub _now_as_w3c {
79             my @time = gmtime;
80             sprintf '%4d-%02d-%02dT%02d:%02d:%02dZ',
81             $time[5]+1900, $time[4]+1, @time[3,2,1,0];
82             }
83              
84             my %enc = ('&' => '&', '"' => '"', '<' => '<', '>' => '>', '\'' => ''');
85              
86             sub _xml_encode {
87             local $_ = shift;
88             s/([&"\'<>])/$enc{$1}/g;
89             $_;
90             }
91              
92             sub tagify {
93             my $notification = shift;
94              
95             ## date is based on current time
96             my (undef, undef, undef, $mday, $mon, $year) = gmtime();
97             $year +=1900;
98              
99             ## specific is based on superfeedr's feed:status
100             my $specific = $notification->feed_uri || "";
101             $specific =~ s{^\w+://}{};
102             $specific =~ tr{#}{/};
103              
104             return sprintf "tag:%s,%4d-%02d-%02d:%s",
105             $AnyEvent::Superfeedr::SERVICE,
106             $year, $mon, $mday,
107             $specific;
108             }
109              
110             1;