blib/lib/Net/Social/Service/Twitter.pm

filter: Statementstmt 31.4% 17 / 54 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 5 / 10 Podpod 100% 4 / 4 totaltotal 26.2% 26 / 99 CC 14 SCAR 32.1
CC
14
Cov
22%
friends
56.0
add_friend
20.0
delete_friend
20.0
CRAP 24.8
↑ Summary
1 package Net::Social::Service::Twitter;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Net::Social::Service::Twitter - handle friends from Twitter for Net::Social
8  
9  
10 =cut
11  
12 1 our $VERSION = '0.1';
13  
14 1 ▶use base qw(Net::Social::Service);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp qw(cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Net::Twitter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Net::Social qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 SYNOPSIS
20  
21 Net:Social::Service::Twitter returns a list of friends that you have on
22 the Twitter service.
23  
24 use Net::Social;
25  
26 my $site = Net::Social->site("Twitter");
27 $site->login(%params}) || die "Need username and password";
28 my @friends = $site-friends;
29  
30 =head1 FUNCTIONS
31  
32 =head2 params
33  
34 Describes the required parameters for logging in (for Twitter,
35 username and password).
36  
37 =cut
38  
39 0 ▶sub params {(
Subroutine params: not called CC=1 SCAR=6.9
Pod documented
40 0 'write' => {
41 "username" => { required => 1,
42 description => "Your Twitter username",
43 },
44 "password" => {
45 required => 1,
46 description => "Your Twitter password",
47 },
48 },
49 )}
50  
51 =head2 friends
52  
53 Returns your friends. It defines the keys C<username>, C<name> and C<type>.
54  
55 =cut
56  
57 0 ▶sub friends {
Subroutine friends: not called CC=7 SCAR=40.3
Pod documented
58 0 my $self = shift;
59  
60 0 ▶ my $twit = $self->_make_twit() || return ();
Condition0%0/2
$self->_make_twit || (return ())
l!l
00
MC/DC0%0/1
$self->_make_twit || (return ())
$self->_make_twit
Truth table0%0/2
A
A= $self->_make_twit
Aresult
11
00
61 0 my %friends = ();
62 0 my @constants = (FRIENDED, FRIENDED_BY);
63 0 foreach my $what (qw(following followers)){
64 0 my $type = shift @constants;
65 0 my $people = eval { $twit->$what() };
66 0 ▶ if ($@ or !$people) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $people) 0 0 0
Condition0%0/3
$@ or not $people
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $people
$@ not $people
Truth table0%0/3
A or not(B)
A= $@B= not $people
ABresult
1X1
011
000
67 0 cluck "There was a problem getting the '$what' list from Twitter";
68 0 return ();
69 }
70 0 foreach my $person (@{$people}) {
71 0 my $user = $person->{screen_name};
72 0 my $name = $person->{name};
73 0 ▶ my $info = $friends{$name} || { username => $user, name => $name };
Condition0%0/2
$friends{$name} || {'username', $user, 'name', $name}
l!l
00
MC/DC0%0/1
$friends{$name} || {'username', $user, 'name', $name}
$friends{$name}
Truth table0%0/2
A
A= $friends{$name}
Aresult
11
00
74 0 $info->{type} |= $type;
75 0 $friends{$name} = $info;
76 }
77 }
78 0 return values %friends;
79 }
80  
81 0 ▶sub _make_twit {
Subroutine _make_twit: not called CC=2 SCAR=17.9
82 0 my $self = shift;
83  
84 0 ▶ return undef unless $self->{_logged_in};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_logged_in"} 0 0 0
85 0 my $user = $self->{_details}->{username};
86 0 my $pass = $self->{_details}->{password};
87  
88 0 return Net::Twitter->new( username => $user, password => $pass );
89 }
90  
91 =head2 add_friend <friend>
92  
93 Add a friend.
94  
95 Takes a Twitter friend as returned by the C<friends()> method.
96  
97 Returns 1 on success or 0 on failure.
98  
99 =cut
100  
101 0 ▶sub add_friend {
Subroutine add_friend: not called CC=4 SCAR=30.0
Pod documented
102 0 my $self = shift;
103 0 ▶ my $twit = $self->_make_twit || return 0;
Condition0%0/2
$self->_make_twit || (return 0)
l!l
00
MC/DC0%0/1
$self->_make_twit || (return 0)
$self->_make_twit
Truth table0%0/2
A
A= $self->_make_twit
Aresult
11
00
104 0 ▶ my $friend = shift || return 0;
Condition0%0/2
shift() || (return 0)
l!l
00
MC/DC0%0/1
shift() || (return 0)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
105 0 ▶ return 0 unless defined $friend->{username};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $friend->{"username"} 0 0 0
106 0 my $res = $twit->follow($friend->{username});
107 0 return defined $res;
108 }
109  
110 =head2 delete_friend <friend>
111  
112 Delete a friend.
113  
114 Takes a Twitter friend as returned by the C<friends()> method.
115  
116 Returns 1 on success 0 on failure.
117  
118 =cut
119  
120 0 ▶sub delete_friend {
Subroutine delete_friend: not called CC=4 SCAR=30.0
Pod documented
121 0 my $self = shift;
122 0 ▶ my $twit = $self->_make_twit || return 0;
Condition0%0/2
$self->_make_twit || (return 0)
l!l
00
MC/DC0%0/1
$self->_make_twit || (return 0)
$self->_make_twit
Truth table0%0/2
A
A= $self->_make_twit
Aresult
11
00
123 0 ▶ my $friend = shift || return 0;
Condition0%0/2
shift() || (return 0)
l!l
00
MC/DC0%0/1
shift() || (return 0)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
124 0 ▶ return 0 unless defined $friend->{username};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $friend->{"username"} 0 0 0
125 0 my $res = $twit->stop_following($friend->{username});
126 0 return defined $res;
127 }
128  
129  
130  
131 =head1 PREREQUISITES
132  
133 This module uses Net::Twitter, so you'll need that (and JSON::Any).
134 You'll also need Net::Social.
135  
136 =head1 CAVEATS
137  
138 The 'friends' API method only returns the last 100 users to have updated.
139 In the event you're free and easy with friending users, there appears to
140 be no way of finding out who all of them are, at least not from the API.
141  
142 =head1 AUTHORS
143  
144 Paul Mison <cpan@husk.org>
145 Simon Wistow <simon@thegestalt.org>
146  
147 =head1 COPYRIGHT
148  
149 Copyright 2007, Paul Mison and Simon Wistow
150  
151 Released under the same terms as Perl itself.
152  
153 =cut
154  
155 1 23;
↑ Summary