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

filter: Statementstmt 27.3% 38 / 139 Branchbran 12.5% 4 / 32 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 39.1% 9 / 23 Podpod 100% 15 / 15 totaltotal 29.0% 66 / 227 CC 23 SCAR 36.6
CC
23
Cov
23%
_build_users
90.0
login
69.4
api_reauthorize
30.0
CRAP 39.0
« blib/lib/App/Maisha/Plugin/Twitter.pm ↑ Summary blib/lib/App/Maisha/Shell.pm »
1 package App::Maisha::Plugin::Identica;
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);
21  
22 #----------------------------------------------------------------------------
23 # Public API
24  
25 #http://identi.ca/settings/oauthapps/show/185
26  
27 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
28 1 my $class = shift;
29 1 my $self = {
30 consumer_key => 'fb8163dcdb32e6c8e60734e961187553',
31 consumer_secret => '8251970fb08ea39a90f6b494e3a2aabc',
32 };
33  
34 1 bless $self, $class;
35 1 return $self;
36 }
37  
38 1 ▶sub login {
Subroutine login: called CC=11 SCAR=42.4
Pod documented
39 1 my ($self,$config) = @_;
40 1 my $api;
41  
42 0 ▶ unless($config->{username}) { warn "No username supplied\n"; return }
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"username"}) 0 1 1
43  
44 1 eval {
45 $api = Net::Twitter->new(
46 traits => [qw/API::Search API::REST OAuth/],
47 consumer_key => $self->{consumer_key},
48 consumer_secret => $self->{consumer_secret},
49 1 identica => 1,
50 ssl => 1
51 );
52 };
53  
54 1 ▶ unless($api) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($api) 0 1 1
55 0 warn "Unable to establish connection to Identica API\n";
56 0 return 0;
57 }
58  
59 # for testing purposes we don't want to login
60 1 ▶ if(!$config->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"test"}) 0 1 1
61 0 eval {
62 0 my $datafile = $config->{home} . '/.maisha/identica.dat';
63 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
64  
65 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
66 0 $api->access_token($access_tokens->{$config->{username}}->[0]);
67 0 $api->access_token_secret($access_tokens->{$config->{username}}->[1]);
68 } else {
69 0 my $auth_url = $api->get_authorization_url;
70 0 print " Authorize this application at: $auth_url\nThen, enter the PIN# provided to continue: ";
71  
72 0 my $pin = <STDIN>; # wait for input
73 0 chomp $pin;
74  
75 0 ▶ unless($pin) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pin) 0 0 0
76 0 warn "No PIN provided, Maisha will not be able to access Identica account until authorized to do so\n";
77 0 return 0;
78 }
79  
80 # request_access_token stores the tokens in $nt AND returns them
81 0 my $access_tokens = {};
82 0 my @access_tokens;
83 0 eval { @access_tokens = $api->request_access_token(verifier => $pin) };
84 0 ▶ unless(@access_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@access_tokens) 0 0 0
85 0 warn "Invalid PIN provided, Maisha will not be able to access your Identica account until authorized to do so\n";
86 0 return 0;
87 }
88 0 $access_tokens->{$config->{username}} = \@access_tokens;
89  
90 0 mkpath( $config->{home} . '/.maisha' );
91  
92 # save the access tokens
93 0 store $access_tokens, $datafile;
94 0 chmod 0640, $datafile; # make sure it has reasonable permissions
95 }
96 };
97  
98 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
99 0 warn "Unable to login to Identica\n";
100 0 return 0;
101 }
102 }
103  
104 1 $self->api($api);
105  
106 1 ▶ if(!$config->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"test"}) 0 1 1
107 0 print "...building user cache for Identica\n";
108 0 $self->_build_users();
109 }
110  
111 1 return 1;
112 }
113  
114 0 ▶sub _build_users {
Subroutine _build_users: not called CC=9 SCAR=45.0
115 0 my $self = shift;
116 0 my %users;
117  
118 0 eval {
119 0 my $f = $self->api->friends();
120 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
121 0 $f = $self->api->followers();
122 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
123 };
124  
125 0 $self->users(\%users);
126 }
127  
128 0 ▶sub api_reauthorize {
Subroutine api_reauthorize: not called CC=5 SCAR=34.0
Pod documented
129 0 my $self = shift;
130 0 my $config = $self->config;
131 0 my $api = $self->api;
132  
133 # for testing purposes we don't want to login
134 0 ▶ if(!$config->{test}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"test"}) 0 0 0
135 0 eval {
136 0 my $datafile = $config->{home} . '/.maisha/identica.dat';
137  
138 0 my $auth_url = $api->get_authorization_url;
139 0 print "Please re-authorize this application at: $auth_url\nThen, enter the PIN# provided to continue: ";
140  
141 0 my $pin = <STDIN>; # wait for input
142 0 chomp $pin;
143  
144 0 ▶ unless($pin) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pin) 0 0 0
145 0 warn "No PIN provided, Maisha will not be able to access direct messages until reauthorization is completed\n";
146 0 return 0;
147 }
148  
149 # request_access_token stores the tokens in $nt AND returns them
150 0 my $access_tokens = {};
151 0 my @access_tokens;
152 0 eval { @access_tokens = $api->request_access_token(verifier => $pin) };
153 0 ▶ unless(@access_tokens) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@access_tokens) 0 0 0
154 0 warn "Invalid PIN provided, Maisha will not be able to access direct messages until reauthorization is completed\n";
155 0 return 0;
156 }
157 0 $access_tokens->{$config->{username}} = \@access_tokens;
158  
159 0 unlink $datafile;
160 0 mkpath( $config->{home} . '/.maisha' );
161  
162 # save the access tokens
163 0 store $access_tokens, $datafile;
164 0 chmod 0640, $datafile; # make sure it has reasonable permissions
165 };
166  
167 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
168 0 warn "Unable to login to Identica\n";
169 0 return 0;
170 }
171 }
172  
173 0 return 1;
174 }
175  
176 0 ▶sub api_user {
Subroutine api_user: not called CC=1 SCAR=6.9
Pod documented
177 0 my $self = shift;
178 0 $self->api->show_user(@_);
179 }
180  
181 0 ▶sub api_user_timeline {
Subroutine api_user_timeline: not called CC=1 SCAR=6.9
Pod documented
182 0 my $self = shift;
183 0 $self->api->user_timeline(@_);
184 }
185  
186 0 ▶sub api_friends {
Subroutine api_friends: not called CC=1 SCAR=6.9
Pod documented
187 0 my $self = shift;
188 0 $self->api->following(@_);
189 }
190  
191 0 ▶sub api_friends_timeline {
Subroutine api_friends_timeline: not called CC=1 SCAR=6.9
Pod documented
192 0 my $self = shift;
193 0 $self->api->following_timeline(@_);
194 }
195  
196 0 ▶sub api_public_timeline {
Subroutine api_public_timeline: not called CC=1 SCAR=6.9
Pod documented
197 0 my $self = shift;
198 0 $self->api->public_timeline(@_);
199 }
200  
201 0 ▶sub api_followers {
Subroutine api_followers: not called CC=1 SCAR=6.9
Pod documented
202 0 my $self = shift;
203 0 $self->api->followers(@_);
204 }
205  
206 0 ▶sub api_update {
Subroutine api_update: not called CC=1 SCAR=6.9
Pod documented
207 0 my $self = shift;
208 0 $self->api->update(@_);
209 }
210  
211 0 ▶sub api_replies {
Subroutine api_replies: not called CC=1 SCAR=6.9
Pod documented
212 0 my $self = shift;
213 0 $self->api->replies(@_);
214 }
215  
216 0 ▶sub api_send_message {
Subroutine api_send_message: not called CC=1 SCAR=6.9
Pod documented
217 0 my $self = shift;
218 0 $self->api->new_direct_message(@_);
219 }
220  
221 0 ▶sub api_direct_messages_to {
Subroutine api_direct_messages_to: not called CC=1 SCAR=6.9
Pod documented
222 0 my $self = shift;
223 0 $self->api->direct_messages(@_);
224 }
225  
226 0 ▶sub api_direct_messages_from {
Subroutine api_direct_messages_from: not called CC=1 SCAR=6.9
Pod documented
227 0 my $self = shift;
228 0 $self->api->sent_direct_messages(@_);
229 }
230  
231 0 ▶sub api_search {
Subroutine api_search: not called CC=1 SCAR=6.9
Pod documented
232 0 my $self = shift;
233 0 $self->api->search(@_);
234 }
235  
236 7 1;
237  
238 __END__
« blib/lib/App/Maisha/Plugin/Twitter.pm ↑ Summary blib/lib/App/Maisha/Shell.pm »