blib/lib/App/Maisha/Plugin/Twitter.pm

filter: Statementstmt 26.5% 39 / 147 Branchbran 11.7% 4 / 34 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 36.0% 9 / 25 Podpod 100% 17 / 17 totaltotal 28.6% 69 / 241 CC 24 SCAR 37.4
CC
24
Cov
22%
_build_users
110.0
login
66.8
api_reauthorize
30.0
CRAP 42.1
↑ Summary blib/lib/App/Maisha/Plugin/Identica.pm »
1 package App::Maisha::Plugin::Twitter;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.22';
7  
8 #----------------------------------------------------------------------------
9 # Library Modules
10  
11 7 ▶use base qw(App::Maisha::Plugin::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Net::Twitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #----------------------------------------------------------------------------
18 # Accessors
19  
20 7 __PACKAGE__->mk_accessors($_) for qw(api users config);
21  
22 #----------------------------------------------------------------------------
23 # Public API
24  
25 #Request token URL
26 #https://api.twitter.com/oauth/request_token
27  
28 #Access token URL
29 #https://api.twitter.com/oauth/access_token
30  
31 #Authorize URL
32 #https://api.twitter.com/oauth/authorize
33  
34 # http://dev.twitter.com/apps/347040
35  
36 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
37 1 my $class = shift;
38 1 my $self = {
39 consumer_key => 'ifCuNOQXA5KTnKVXVcZg',
40 consumer_secret => 'OXyHE1PSgfy66gbCu3QshXgP9RNA1fOVLdqv4afPDug',
41 };
42  
43 1 bless $self, $class;
44 1 return $self;
45 }
46  
47 1 ▶sub login {
Subroutine login: called CC=11 SCAR=42.0
Pod documented
48 1 my ($self,$config) = @_;
49 1 my $api;
50  
51 0 ▶ unless($config->{username}) { warn "No username supplied\n"; return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"username"}) 0 1 1
52  
53 1 eval {
54 $api = Net::Twitter->new(
55 traits => [qw/API::Search API::REST OAuth/],
56 consumer_key => $self->{consumer_key},
57 consumer_secret => $self->{consumer_secret},
58 1 ssl => 1
59 );
60 };
61  
62 1 ▶ unless($api) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($api) 0 1 1
63 0 warn "Unable to establish connection to Twitter API\n";
64 0 return 0;
65 }
66  
67 # for testing purposes we don't want to login
68 1 ▶ if(!$config->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"test"}) 0 1 1
69 0 eval {
70 0 my $datafile = $config->{home} . '/.maisha/twitter.dat';
71 0 ▶ my $access_tokens = eval { retrieve($datafile) } || {};
Condition0%0/2
eval { do { retrieve($datafile) } } || {}
l!l
00
MC/DC0%0/1
eval { do { retrieve($datafile) } } || {}
eval { do { retrieve($datafile) } }
Truth table0%0/2
A
A= eval { do { retrieve($datafile) } }
Aresult
11
00
72  
73 0 ▶ if ( $access_tokens && $access_tokens->{$config->{username}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($access_tokens and $$access_tokens{$config->{'username'}}) { } 0 0 0
Condition0%0/3
$access_tokens and $$access_tokens{$config->{"username"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$access_tokens and $$access_tokens{$config->{"username"}}
$access_tokens $$access_tokens{$config->{"username"}}
Truth table0%0/3
A and B
A= $access_tokensB= $$access_tokens{$config->{"username"}}
ABresult
0X0
100
111
74 0 $api->access_token($access_tokens->{$config->{username}}->[0]);
75 0 $api->access_token_secret($access_tokens->{$config->{username}}->[1]);
76 } else {
77 0 my $auth_url = $api->get_authorization_url;
78 0 print " Authorize this application at: $auth_url\nThen, enter the PIN# provided to continue: ";
79  
80 0 my $pin = <STDIN>; # wait for input
81 0 chomp $pin;
82  
83 0 ▶ unless($pin) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pin) 0 0 0
84 0 warn "No PIN provided, Maisha will not be able to access Twitter account until authorized to do so\n";
85 0 return 0;
86 }
87  
88 # request_access_token stores the tokens in $nt AND returns them
89 0 my $access_tokens = {};
90 0 my @access_tokens;
91 0 eval { @access_tokens = $api->request_access_token(verifier => $pin) };
92 0 ▶ unless(@access_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@access_tokens) 0 0 0
93 0 warn "Invalid PIN provided, Maisha will not be able to access your Twitter account until authorized to do so\n";
94 0 return 0;
95 }
96 0 $access_tokens->{$config->{username}} = \@access_tokens;
97  
98 0 mkpath( $config->{home} . '/.maisha' );
99  
100 # save the access tokens
101 0 store $access_tokens, $datafile;
102 0 chmod 0640, $datafile; # make sure it has reasonable permissions
103 }
104 };
105  
106 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
107 0 warn "Unable to login to Twitter\n";
108 0 return 0;
109 }
110 }
111  
112 1 $self->api($api);
113 1 $self->config($config);
114  
115 1 ▶ if(!$config->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"test"}) 0 1 1
116 0 print "...building user cache for Twitter\n";
117 0 $self->_build_users();
118 }
119  
120 1 return 1;
121 }
122  
123 0 ▶sub _build_users {
Subroutine _build_users: not called CC=10 SCAR=47.0
124 0 my $self = shift;
125 0 my %users;
126  
127 0 eval {
128 0 my $f = $self->api->friends();
129 0 ▶ if($f && @$f) { for(@$f) { next unless($_); $users{$_->{screen_name}} = 1 } }
Branch: 2 branches
BranchTrueFalseTotal
if ($f and @$f) 0 0 0
unless $_ 0 0 0
Condition0%0/3
$f and @$f
!ll&&!rl&&r
000
MC/DC0%0/2
$f and @$f
$f @$f
Truth table0%0/3
A and B
A= $fB= @$f
ABresult
0X0
100
111
130 0 $f = $self->api->followers();
131 0 ▶ if($f && @$f) { for(@$f) { next unless($_); $users{$_->{screen_name}} = 1 } }
Branch: 2 branches
BranchTrueFalseTotal
if ($f and @$f) 0 0 0
unless $_ 0 0 0
Condition0%0/3
$f and @$f
!ll&&!rl&&r
000
MC/DC0%0/2
$f and @$f
$f @$f
Truth table0%0/3
A and B
A= $fB= @$f
ABresult
0X0
100
111
132 };
133  
134 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
135 0 warn "Error retrieving friends/followers from Twitter: $@\n";
136 0 return;
137 }
138  
139 0 $self->users(\%users);
140 }
141  
142 0 ▶sub api_reauthorize {
Subroutine api_reauthorize: not called CC=5 SCAR=34.0
Pod documented
143 0 my $self = shift;
144 0 my $config = $self->config;
145 0 my $api = $self->api;
146  
147 # for testing purposes we don't want to login
148 0 ▶ if(!$config->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"test"}) 0 0 0
149 0 eval {
150 0 my $datafile = $config->{home} . '/.maisha/twitter.dat';
151  
152 0 my $auth_url = $api->get_authorization_url;
153 0 print "Please re-authorize this application at: $auth_url\nThen, enter the PIN# provided to continue: ";
154  
155 0 my $pin = <STDIN>; # wait for input
156 0 chomp $pin;
157  
158 0 ▶ unless($pin) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pin) 0 0 0
159 0 warn "No PIN provided, Maisha will not be able to access direct messages until reauthorization is completed\n";
160 0 return 0;
161 }
162  
163 # request_access_token stores the tokens in $nt AND returns them
164 0 my $access_tokens = {};
165 0 my @access_tokens;
166 0 eval { @access_tokens = $api->request_access_token(verifier => $pin) };
167 0 ▶ unless(@access_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@access_tokens) 0 0 0
168 0 warn "Invalid PIN provided, Maisha will not be able to access direct messages until reauthorization is completed\n";
169 0 return 0;
170 }
171 0 $access_tokens->{$config->{username}} = \@access_tokens;
172  
173 0 unlink $datafile;
174 0 mkpath( $config->{home} . '/.maisha' );
175  
176 # save the access tokens
177 0 store $access_tokens, $datafile;
178 0 chmod 0640, $datafile; # make sure it has reasonable permissions
179 };
180  
181 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
182 0 warn "Unable to login to Twitter\n";
183 0 return 0;
184 }
185 }
186  
187 0 return 1;
188 }
189  
190 0 ▶sub api_follow {
Subroutine api_follow: not called CC=1 SCAR=6.9
Pod documented
191 0 my $self = shift;
192 0 $self->api->create_friend(@_);
193 }
194  
195 0 ▶sub api_unfollow {
Subroutine api_unfollow: not called CC=1 SCAR=6.9
Pod documented
196 0 my $self = shift;
197 0 $self->api->destroy_friend(@_);
198 }
199  
200 0 ▶sub api_user {
Subroutine api_user: not called CC=1 SCAR=6.9
Pod documented
201 0 my $self = shift;
202 0 $self->api->show_user(@_);
203 }
204  
205 0 ▶sub api_user_timeline {
Subroutine api_user_timeline: not called CC=1 SCAR=6.9
Pod documented
206 0 my $self = shift;
207 0 $self->api->user_timeline(@_);
208 }
209  
210 0 ▶sub api_friends {
Subroutine api_friends: not called CC=1 SCAR=6.9
Pod documented
211 0 my $self = shift;
212 0 $self->api->following(@_);
213 }
214  
215 0 ▶sub api_friends_timeline {
Subroutine api_friends_timeline: not called CC=1 SCAR=6.9
Pod documented
216 0 my $self = shift;
217 0 $self->api->following_timeline(@_);
218 }
219  
220 0 ▶sub api_public_timeline {
Subroutine api_public_timeline: not called CC=1 SCAR=6.9
Pod documented
221 0 my $self = shift;
222 0 $self->api->public_timeline(@_);
223 }
224  
225 0 ▶sub api_followers {
Subroutine api_followers: not called CC=1 SCAR=6.9
Pod documented
226 0 my $self = shift;
227 0 $self->api->followers(@_);
228  
229 # below is meant to be the same as the above, but it isn't :(
230 #$self->api->lookup_users( { user_id => $self->api->followers_ids() } );
231 }
232  
233 0 ▶sub api_update {
Subroutine api_update: not called CC=1 SCAR=6.9
Pod documented
234 0 my $self = shift;
235 0 $self->api->update(@_);
236 }
237  
238 0 ▶sub api_replies {
Subroutine api_replies: not called CC=1 SCAR=6.9
Pod documented
239 0 my $self = shift;
240 0 $self->api->replies(@_);
241 }
242  
243 0 ▶sub api_send_message {
Subroutine api_send_message: not called CC=1 SCAR=6.9
Pod documented
244 0 my $self = shift;
245 0 $self->api->new_direct_message(@_);
246 }
247  
248 0 ▶sub api_direct_messages_to {
Subroutine api_direct_messages_to: not called CC=1 SCAR=6.9
Pod documented
249 0 my $self = shift;
250 0 $self->api->direct_messages(@_);
251 }
252  
253 0 ▶sub api_direct_messages_from {
Subroutine api_direct_messages_from: not called CC=1 SCAR=6.9
Pod documented
254 0 my $self = shift;
255 0 $self->api->sent_direct_messages(@_);
256 }
257  
258 0 ▶sub api_search {
Subroutine api_search: not called CC=1 SCAR=6.9
Pod documented
259 0 my $self = shift;
260 0 $self->api->search(@_);
261 }
262  
263 7 1;
264  
265 __END__
↑ Summary blib/lib/App/Maisha/Plugin/Identica.pm »