Skip to content

Commit

Permalink
Fixed: AutoCursor
Browse files Browse the repository at this point in the history
- should not change behavior when user passes "cursor" arg
- use dash (-) prefix for synthetic args; back compat without
- works with InflateObjects trait
  • Loading branch information
semifor committed Sep 29, 2011
1 parent 24c92a3 commit 58e3c32
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,3 +1,7 @@
- fixed: AutoCursor:
- no behavior change when user passes "cursor" arg
- work with InflateObjects trait
- synthetic ags use (-) prefix: -authenticate, -since, -legacy_lists_api
- Replaced JSON::Any with JSON
- documented new arguments for friendship_exists

Expand Down
29 changes: 23 additions & 6 deletions lib/Net/Twitter/Role/AutoCursor.pm
Expand Up @@ -17,14 +17,20 @@ role {

my $args = ref $_[-1] eq ref {} ? pop : {};

# backwards compat: all synthetic args use dash (-) prefix, now
for ( qw/force_cursor max_calls/ ) {
$args->{"-$_"} = delete $args->{$_} if exists $args->{$_};
}

# no change in behavior if the user passed a cursor
return $self->$orig(@_, $args) if exists $args->{cursor};

$args->{id} = shift if @_;

$args->{cursor} = -1 if !exists $args->{cursor} && $p->force_cursor;
my $force_cursor = exists $args->{-force_cursor} ? $args->{-force_cursor} : $p->force_cursor;
$args->{cursor} = -1 if !exists $args->{cursor} && $force_cursor;

my $max_calls = delete $args->{max_calls} || $p->max_calls;
my $max_calls = exists $args->{-max_calls} ? $args->{-max_calls} : $p->max_calls;

my $calls = 0;
my $results;
Expand All @@ -40,7 +46,7 @@ role {
}

while ( $args->{cursor} && $calls++ < $max_calls ) {
my $r = $orig->($self, $args);
my $r = $self->$orig($args);
push @$results, @{$r->{$p->array_accessor}};
$args->{cursor} = $r->{next_cursor};
}
Expand Down Expand Up @@ -107,8 +113,8 @@ made (yielding 80,000 results). It returns an ARRAY reference to the combined
results.
If the C<cursor> parameter is passed to C<friends_ids> or C<followers_ids>,
C<Net::Twitter> uses cursored access from the start, i.e., it does not
attempt an initial non-cursored call.
C<Net::Twitter> assumes the user is handling cursoring and does not modify
behavior or results.
The C<AutoCursor> trait is parameterized, allowing it to work with any Twitter
API method that expects cursors, returning combined results for up to the
Expand All @@ -131,7 +137,8 @@ argument to the API method.
=item force_cursor
If true, when the caller does not provide a C<cursor> parameter, C<AutoCursor>
will add one with value -1. Default is 0.
will use up to C<max_calls> cursored calls rather than attempting an initial
non-cursored call. Default is 0.
=item array_accessor
Expand All @@ -145,6 +152,16 @@ C<AutoCursor> will be applied.
=back
=head1 METHOD CALLS
Synthetic parameter C<-max_calls> can be passed for individual method calls
to override the default:
$r = $nt->followers_ids({ -max_calls => 200 }); # get up to 1 million ids
Synthetic parameter C<-force_cursor> can be passed to override the
C<force_cursor> default.
=head1 AUTHOR
Marc Mims <marc@questright.com>
Expand Down
11 changes: 7 additions & 4 deletions t/auto-cursor.t
Expand Up @@ -30,24 +30,27 @@ sub mock_ua {
mock_ua($_) for $nt_with_max_calls_2, $nt_with_max_calls_4;

my $r = $nt_with_max_calls_2->friends_ids({ cursor => -1 });
is scalar @{$r->{ids}}, 1, 'behavior unmodified with "curosor" arg';

$r = $nt_with_max_calls_2->friends_ids({ -force_cursor => 1 });
is scalar @$r, 2, 'max_calls => 2';

$r = $nt_with_max_calls_4->friends_ids({ cursor => -1 });
$r = $nt_with_max_calls_4->friends_ids({ -force_cursor => 1 });
is scalar @$r, 4, 'max_calls => 4';

$r = $nt_with_max_calls_4->followers_ids({ cursor => -1, max_calls => 10 });
$r = $nt_with_max_calls_4->followers_ids({ -force_cursor => 1, max_calls => 10 });
is scalar @$r, 10, 'max_calls per call override';

my $nt = Net::Twitter->new(traits => ['API::REST', AutoCursor => { max_calls => 2 }]);
mock_ua($nt);
is ref $nt, $class_for_max_calls_2, 'clone max_calls => 2, class name';
$r = $nt->friends_ids({ cursor => -1 });
$r = $nt->friends_ids({ -force_cursor => 1 });
is scalar @$r, 2, 'cloned max_calls => 2';

$nt = Net::Twitter->new(traits => ['API::REST', AutoCursor => { max_calls => 4 }]);
mock_ua($nt);
is ref $nt, $class_for_max_calls_4, 'clone max_calls => 4, class name';
$r = $nt->friends_ids({ cursor => -1 });
$r = $nt->friends_ids({ -force_cursor => 1 });
is scalar @$r, 4, 'cloned max_calls => 4';
}

Expand Down

0 comments on commit 58e3c32

Please sign in to comment.