Spinn3r/Synced.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 110 CC 3 SCAR 13.5
CC
3
Cov
0%
next
12.0
new
2.0
feed_begin
2.0
CRAP 3.9
1 package WWW::Spinn3r::Synced;
2  
3 use WWW::Spinn3r;
4 use DateTime;
5 use DateTime::Duration;
6 use DateTime::Format::ISO8601;
7 use Data::Dumper;
8  
9 use DB_File;
10 use Fcntl;
11 use File::Temp qw(tempfile);
12 use Storable qw(freeze thaw);
13  
14 my $iso8601 = DateTime::Format::ISO8601->new;
15  
16 sub new {
17  
18 my $p = shift;
19  
20 my %param = @_;
21 exists $param{api} and die "don't pass api to WWW::Spinn3r::Synced, use WWW::Spinn3r instead if you want a specific api";
22 delete $param{api};
23  
24 my $match_range = delete($param{match_range}) || 30;
25  
26 my %pparam = %param;
27  
28 my $self = bless {
29 permalink => WWW::Spinn3r->new(api => 'permalink3.getDelta', %pparam),
30 fi => { },
31 last_ftime => undef,
32 match_range => $match_range,
33 orig_param => \%param,
34 }, $p;
35  
36 $self
37 }
38  
39 sub feed_begin {
40  
41 my ($self, $after) = @_;
42 my %param = %{$self->{orig_param}};
43 $param{params}->{after} = $after;
44 $self->{feed} = WWW::Spinn3r->new(api => 'feed3.getDelta', %param);
45 $self->{feed}->next();
46  
47 }
48  
49 sub feed_next {
50 my $self = shift;
51 $self->{feed}->next(@_);
52 }
53  
54 sub permalink_next {
55 my $self = shift;
56 $self->{permalink}->next(@_);
57 }
58  
59 sub next {
60 my $self = shift;
61 my $fi = $self->{fi};
62 my $match_range = $self->{match_range};
63  
64 my $pitem = $self->permalink_next;
65 return unless $pitem;
66 my $ptime = $pitem->{t} = $iso8601->parse_datetime($pitem->{'post:date_found'})->epoch;
67 my $min_t = $ptime - $match_range;
68 my $max_t = $ptime + $match_range;
69 my $link = $$pitem{link};
70  
71 unless ($self->{feed}) {
72 $self->feed_begin($iso8601->parse_datetime($pitem->{'post:date_found'}) - DateTime::Duration->new(seconds => $match_range));
73 }
74  
75 my $feed_count = 0;
76 while ($self->{last_ftime} <= $max_t and $feed_count < 1024) {
77 my $fitem = $self->feed_next;
78 my $ftime = $fitem->{t} = $iso8601->parse_datetime($fitem->{'post:date_found'})->epoch;
79 $$fi{$$fitem{link}} = $fitem;
80 $self->{last_ftime} = $ftime;
81 $feed_count++;
82 }
83  
84 # this feed item, if defined, goes with this permalink
85 my $fitem = delete $$fi{$link};
86  
87 # remove feed items which don't refer to permalink items
88 for my $flink (keys %$fi) {
89 my $fitem = $$fi{$flink};
90 my $ftime = $$fitem{t};
91 $ftime < $min_t and delete $$fi{$flink};
92 }
93  
94 return [$pitem, $fitem];
95 }
96  
97  
98 1;
99  
100 =head1 NAME
101  
102 WWW::Spinn3r::Synced - A interface that provides synced access to
103 permalink and feed APIs.
104  
105 =head1 SYNOPOSIS
106  
107 use WWW::Spinn3r::Synced;
108 use DateTime;
109  
110 my $API = {
111 vendor => 'acme', # required
112 limit => 5,
113 after => DateTime->now()->subtract(hours => 48),
114 };
115  
116 my $spnr = new WWW::Spinn3r::Synced (params => $API, debug => 1);
117  
118 while(1) {
119 my ($p, $f) = @{ $spnr->next };
120 if ($p and $f) {
121 print "both! $$p{link}\n"
122 } else {
123 print "permalink only! $$p{link}\n"
124 }
125 }
126  
127 =head1 DESCRIPTION
128  
129 WWW::Spinn3r::Synced synchronizes the C<permalink3.getDelta> and
130 C<feed3.getDelta> APIs provided by Spinn3r such that next() returns both
131 the permalink and feed items associated with a link.
132  
133 =head1 B<new()>
134  
135 The same as new() in WWW::Spinn3r but doesn't accept an C<api> key, since
136 both APIs are used internally by this module.
137  
138 =head1 next()
139  
140 Returns an arrayref, the first element is the permalink item and the
141 second element is the feed item. When a feed item is not available,
142 the permalink is returned as first item, and the second item is set
143 to undef.
144  
145 =head1 SEE ALSO
146  
147 examples/synced.pl, WWW::Spinn3r
148  
149 =head1 AUTHOR
150  
151 Dan Brumleve <opensource@slaant.com>
152  
153 =cut