Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed: AutoCursor

- 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...
commit 58e3c3292b2f5df6980db6e6df2b407fbd830cb5 1 parent 24c92a3
@semifor authored
Showing with 34 additions and 10 deletions.
  1. +4 −0 Changes
  2. +23 −6 lib/Net/Twitter/Role/AutoCursor.pm
  3. +7 −4 t/auto-cursor.t
View
4 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
View
29 lib/Net/Twitter/Role/AutoCursor.pm
@@ -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;
@@ -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};
}
@@ -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
@@ -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
@@ -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>
View
11 t/auto-cursor.t
@@ -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';
}
Please sign in to comment.
Something went wrong with that request. Please try again.