File Coverage

blib/lib/Net/Social/Service/Twitter.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Net::Social::Service::Twitter;
2              
3 1     1   696 use strict;
  1         2  
  1         56  
4              
5             =head1 NAME
6              
7             Net::Social::Service::Twitter - handle friends from Twitter for Net::Social
8              
9              
10             =cut
11              
12             our $VERSION = '0.1';
13              
14 1     1   4 use base qw(Net::Social::Service);
  1         3  
  1         3458  
15 1     1   384 use Carp qw(cluck);
  1         15  
  1         69  
16 1     1   822 use Net::Twitter;
  0            
  0            
17             use Net::Social qw(:all);
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             sub params {(
40             '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             sub friends {
58             my $self = shift;
59              
60             my $twit = $self->_make_twit() || return ();
61             my %friends = ();
62             my @constants = (FRIENDED, FRIENDED_BY);
63             foreach my $what (qw(following followers)){
64             my $type = shift @constants;
65             my $people = eval { $twit->$what() };
66             if ($@ or !$people) {
67             cluck "There was a problem getting the '$what' list from Twitter";
68             return ();
69             }
70             foreach my $person (@{$people}) {
71             my $user = $person->{screen_name};
72             my $name = $person->{name};
73             my $info = $friends{$name} || { username => $user, name => $name };
74             $info->{type} |= $type;
75             $friends{$name} = $info;
76             }
77             }
78             return values %friends;
79             }
80              
81             sub _make_twit {
82             my $self = shift;
83              
84             return undef unless $self->{_logged_in};
85             my $user = $self->{_details}->{username};
86             my $pass = $self->{_details}->{password};
87              
88             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             sub add_friend {
102             my $self = shift;
103             my $twit = $self->_make_twit || return 0;
104             my $friend = shift || return 0;
105             return 0 unless defined $friend->{username};
106             my $res = $twit->follow($friend->{username});
107             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             sub delete_friend {
121             my $self = shift;
122             my $twit = $self->_make_twit || return 0;
123             my $friend = shift || return 0;
124             return 0 unless defined $friend->{username};
125             my $res = $twit->stop_following($friend->{username});
126             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             23;