Agent/Twitter/Source.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 83 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Exobrain::Agent::Twitter::Source;
2 use v5.10.0;
3 use Moose;
4 use Method::Signatures;
5  
6 our $VERSION = '1.04'; # VERSION
7 # ABSTRACT: Agent for reading tweets
8  
9 with 'Exobrain::Agent::Poll';
10 with 'Exobrain::Agent::Twitter';
11  
12  
13 # Key used by the cache for our last Twitter ID.
14 # Called 'last_check' for historical reasons.
15 use constant CACHE_LAST_MENTION => 'last_mention';
16 use constant CACHE_LAST_DM => 'last_dm';
17 use constant DEBUG => 0;
18  
19 has last_mention => (
20 is => 'rw',
21 lazy => 1,
22 builder => '_build_last_mention',
23 trigger => \&_cache_last_mention,
24 );
25  
26 method _build_last_mention() {
27 return $self->cache->get( CACHE_LAST_MENTION ) || 0;
28 }
29  
30 method _cache_last_mention($new, $old?) {
31 print "Setting last mention to $new\n" if DEBUG;
32 $self->cache->set( CACHE_LAST_MENTION, $new );
33 return;
34 }
35  
36 has last_dm => (
37 is => 'rw',
38 lazy => 1,
39 builder => '_build_last_dm',
40 trigger => \&_cache_last_dm,
41 );
42  
43 method _build_last_dm() {
44 return $self->cache->get( CACHE_LAST_DM ) || 0;
45 }
46  
47 method _cache_last_dm($new, $old?) {
48 print "Setting last DM to $new\n" if DEBUG;
49 $self->cache->set( CACHE_LAST_DM, $new );
50 return;
51 }
52  
53 method poll() {
54 my $last_mention = $self->last_mention;
55  
56 my $statuses =
57 $last_mention ?
58 $self->twitter->mentions({ since_id => $last_mention }) :
59 $self->twitter->mentions()
60 ;
61  
62 for my $status ( @$statuses ) {
63  
64 my $text = $status->{text};
65 my @tags = $self->tags( $text );
66 my $epoch_time = $self->to_epoch( $status->{created_at} );
67  
68 print "[$status->{id}] $epoch_time <$status->{user}{screen_name}> $status->{text} (Tags: @tags)\n" if DEBUG;
69  
70 # TODO: Parse or figure out who this is to, for the 'to' attribute.
71  
72 $self->exobrain->measure('Tweet',
73 timestamp => $epoch_time,
74 from => $status->{user}{screen_name},
75 tags => \@tags,
76 to_me => 1, # Because we're only looking at replies
77 text => $status->{text},
78 raw => $status,
79 id => $status->{id},
80 platform => $self->component,
81 );
82  
83 # We explicitly check to see if we have a newer last_mention
84 # because we don't want to rely upon the ordering in which
85 # tweets are returned. In fact, they're almost always in the
86 # order we don't want.
87  
88 if ($status->{id} > $self->last_mention) {
89 $self->last_mention( $status->{id} );
90 }
91 }
92  
93 # Ugh. Repeated code for DMs. We should be able to combine
94 # These two loops.
95  
96 my $last_dm = $self->last_dm;
97  
98 my $dms =
99 $last_dm ?
100 $self->twitter->direct_messages({ since_id => $last_dm }) :
101 $self->twitter->direct_messages
102 ;
103  
104 foreach my $dm ( @$dms ) {
105 my $text = $dm->{text};
106 my $epoch_time = $self->to_epoch( $dm->{created_at} );
107 my $from = $dm->{sender_screen_name};
108  
109 $self->exobrain->measure('DirectMessage',
110 timestamp => $epoch_time,
111 from => $from,
112 text => $text,
113 summary => "[DM] \@$from: $text",
114 raw => $dm,
115 id => $dm->{id},
116 platform => $self->component,
117 );
118  
119 if ($dm->{id} > $self->last_dm ) {
120 $self->last_dm( $dm->{id} );
121 }
122 }
123 }
124  
125 1;
126  
127 __END__