1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
package Net::Twitter::Role::API::Lists;
use Moose::Role;
use Net::Twitter::API;
use DateTime::Format::Strptime;
use Try::Tiny;
=head1 NAME
Net::Twitter::Role::API::Lists - Twitter Lists API support for Net::Twitter
=head1 SYNOPSIS
use Net::Twitter;
my $nt = Net::Twitter->new(traits => ['API::Lists'], ...);
$list = $nt->create_list($owner, { name => $name, description => $desc });
$list = $nt->update_list($owner, $list_id, { description => $desc });
$lists = $nt->get_lists($owner);
$lists = $nt->list_lists($owner);
$list = $nt->get_list($owner, $list_id);
$list = $nt->delete_list($owner, $list_id);
$statuses = $nt->list_statuses($owner, $list_id);
$lists = $nt->list_memberships($owner);
$lists = $nt->list_subscriptions($owner);
$users = $nt->list_members($owner, $list_id);
$user_or_undef = $nt->list_members($owner, $list_id, { id => $user_id });
$user = $nt->add_list_member($owner, $list_id, $user_id);
$user = $nt->delete_list_member($owner, $list_id, $user_id);
$user = $nt->remove_list_member($owner, $list_id, $user_id);
$user_or_undef = $nt->is_list_member($owner, $list_id, $user_id);
$users = $nt->list_subscribers($owner, $list_id);
$list = $nt->subscribe_list($owner, $list_id);
$list = $nt->unsubscribe_list($owner, $list_id);
$user_or_undef = $nt->is_subscribed_list($owner, $list_id, $user_id);
$user_or_undef = $nt->is_list_subscriber($owner, $list_id, $user_id);
#############################
# With the cursor parameter #
#############################
$r = $nt->get_list($user, $list_id, { cursor => $cursor });
$lists = $r->{lists};
$r = $nt->list_memberships($user, { cursor => $cursor });
$lists = $r->{lists};
$r = $nt->list_subscriptions($user, { cursor => $cursor });
$lists = $r->{lists};
$r = $nt->list_members($owner, $list_id, { cursor => $cursor });
$users = $r->{users};
$r = $nt->list_subscribers($owner, $list_id, { cursor => $cursor });
$users = $r->{users};
=head1 DESCRIPTION
This module adds support to L<Net::Twitter> for the Twitter Lists API.
=cut
requires qw/username ua/;
=head1 DESCRIPTION
B<Net::Twitter::Role::API::Lists> provides a trait for the Twitter Lists API methods.
See L<Net::Twitter> for full documentation.
=cut
has lists_api_url => ( isa => 'Str', is => 'rw', default => 'http://api.twitter.com/1' );
base_url 'lists_api_url';
authenticate 1;
our $DATETIME_PARSER = DateTime::Format::Strptime->new(pattern => '%a %b %d %T %z %Y');
datetime_parser $DATETIME_PARSER;
after BUILD => sub {
my $self = shift;
$self->{lists_api_url} =~ s/^http:/https:/ if $self->ssl;
};
twitter_api_method create_list => (
path => ':user/lists',
method => 'POST',
params => [qw/user name mode description/],
required => [qw/user name/],
returns => 'HashRef',
description => <<'',
Creates a new list for the authenticated user. The C<mode> parameter may be
either C<public> or C<private>. If not specified, it defaults to C<public>.
);
twitter_api_method update_list => (
path => ':user/lists/:list_id',
method => 'POST',
params => [qw/user list_id name mode description/],
required => [qw/user list_id/],
returns => 'HashRef',
description => <<'',
Updates a list to change the name, mode, description, or any combination thereof.
);
twitter_api_method get_lists => (
path => ':user/lists',
method => 'GET',
params => [qw/user cursor/],
required => [qw/user/],
returns => 'ArrayRef[List]',
aliases => [qw/list_lists/],
description => <<'EOT',
Returns a reference to an array of lists owned by the specified user. If the
user is the authenticated user, it returns both public and private lists.
Otherwise, it only returns public lists.
When the C<cursor> parameter is used, a hash reference is returned; the lists
are returned in the C<lists> element of the hash.
EOT
);
twitter_api_method get_list => (
path => ':user/lists/:list_id',
method => 'GET',
params => [qw/user list_id/],
required => [qw/user list_id/],
returns => 'HashRef',
description => <<'',
Returns the specified list as a hash reference.
);
twitter_api_method delete_list => (
path => ':user/lists/:list_id',
method => 'DELETE',
params => [qw/user list_id/],
required => [qw/user list_id/],
description => <<'',
Deletes a list owned by the authenticating user. Returns the list as a hash
reference.
);
twitter_api_method list_statuses => (
path => ':user/lists/:list_id/statuses',
method => 'GET',
params => [qw/user list_id since_id max_id per_page page/],
required => [qw/user list_id/],
returns => 'ArrayRef[Status]',
description => <<'',
Returns a timeline of list member statuses as an array reference.
);
twitter_api_method list_memberships => (
path => ':user/lists/memberships',
method => 'GET',
params => [qw/user cursor/],
required => [qw/user/],
description => <<'EOT',
Returns the lists the specified user is a member of as an array reference.
When the C<cursor> parameter is used, a hash reference is returned; the lists
are returned in the C<lists> element of the hash.
EOT
);
twitter_api_method list_subscriptions => (
path => ':user/lists/subscriptions',
method => 'GET',
params => [qw/user cursor/],
required => [qw/user/],
description => <<'EOT',
Returns a lists to which the specified user is subscribed as an array reference.
When the C<cursor> parameter is used, a hash reference is returned; the lists
are returned in the C<lists> element of the hash.
EOT
);
twitter_api_method list_members => (
path => ':user/:list_id/members',
method => 'GET',
params => [qw/user list_id id cursor/],
required => [qw/user list_id/],
returns => 'ArrayRef[User]',
description => <<'EOT',
Returns the list members as an array reference.
The optional C<id> parameter can be used to determine if the user specified by
C<id> is a member of the list. If so, the user is returned as a hash
reference; if not, C<undef> is returned.
When the C<cursor> parameter is used, a hash reference is returned; the members
are returned in the C<users> element of the hash.
EOT
);
around list_members => sub {
my $orig = shift;
my $self = shift;
$self->_user_or_undef($orig, 'member', @_);
};
twitter_api_method is_list_member => (
path => ':user/:list_id/members/:id',
method => 'GET',
params => [qw/user list_id id/],
required => [qw/user list_id id/],
returns => 'ArrayRef[User]',
description => <<'EOT',
Returns the list member as a HASH reference if C<id> is a member of the list.
Otherwise, returns undef.
EOT
);
around is_list_member => sub {
my $orig = shift;
my $self = shift;
$self->_user_or_undef($orig, 'member', @_);
};
twitter_api_method add_list_member => (
path => ':user/:list_id/members',
method => 'POST',
returns => 'User',
params => [qw/user list_id id/],
required => [qw/user list_id id/],
description => <<'EOT',
Adds the user identified by C<id> to the list.
Returns a reference the added user as a hash reference.
EOT
);
twitter_api_method delete_list_member => (
path => ':user/:list_id/members',
method => 'DELETE',
params => [qw/user list_id id/],
required => [qw/user list_id id/],
aliases => [qw/remove_list_member/],
description => <<'EOT',
Deletes the user identified by C<id> from the specified list.
Returns the deleted user as a hash reference.
EOT
);
twitter_api_method list_subscribers => (
path => ':user/:list_id/subscribers',
method => 'GET',
params => [qw/user list_id id cursor/],
required => [qw/user list_id/],
returns => 'ArrayRef[User]',
description => <<'EOT',
Returns the subscribers to a list as an array reference.
When the C<cursor> parameter is used, a hash reference is returned; the subscribers
are returned in the C<users> element of the hash.
EOT
);
around list_subscribers => sub {
my $orig = shift;
my $self = shift;
$self->_user_or_undef($orig, 'subscriber', @_);
};
twitter_api_method is_list_subscriber => (
path => ':user/:list_id/subscribers/:id',
method => 'GET',
params => [qw/user list_id id/],
required => [qw/user list_id id/],
returns => 'ArrayRef[User]',
aliases => [qw/is_subscribed_list/],
description => <<'EOT',
Returns the subscriber as a HASH reference if C<id> is a subscriber to the list.
Otherwise, returns undef.
EOT
);
around is_list_subscriber => sub {
my $orig = shift;
my $self = shift;
$self->_user_or_undef($orig, 'subscriber', @_);
};
twitter_api_method subscribe_list => (
path => ':user/:list_id/subscribers',
method => 'POST',
returns => 'List',
params => [qw/user list_id/],
required => [qw/user list_id/],
description => <<'',
Subscribes the authenticated user to the specified list.
);
twitter_api_method unsubscribe_list => (
path => ':user/:list_id/subscribers',
method => 'DELETE',
returns => 'List',
params => [qw/user list_id/],
required => [qw/user list_id/],
description => <<'',
Unsubscribes the authenticated user from the specified list.
);
sub _user_or_undef {
my ( $self, $orig, $type, @rest ) = @_;
return try {
$orig->($self, @rest);
}
catch {
die $_ unless /The specified user is not a $type of this list/;
undef;
};
}
1;
__END__
=head1 SEE ALSO
L<Net::Twitter>
=head1 AUTHOR
Marc Mims <marc@questright.com>
=head1 COPYRIGHT
Copyright (c) 2009-2010 Marc Mims
=head1 LICENSE
This library is free software. You may redistribute and modify it under the
same terms as Perl itself.
=cut
|