Permalink
Browse files

Allow collection-specific activity lists

In future version of the API there will probably be support for getting
a list of activities per 'collection' (probably a circle or something
similar) so prepare for that.  This also bumps the module formally
requiring Perl 5.10.1.

Signed-off-by: Zak B. Elep <zakame@cpan.org>
  • Loading branch information...
1 parent fceb8d8 commit f6612a0da1815a6d53d250bfb23c623753e24f52 @zakame committed Oct 4, 2011
Showing with 39 additions and 12 deletions.
  1. +22 −9 lib/Google/Plus.pm
  2. +17 −3 t/01-plus.t
View
@@ -1,5 +1,6 @@
package Google::Plus;
use Mojo::Base -base;
+use v5.10.1;
use Mojo::URL;
use Mojo::UserAgent;
@@ -12,7 +13,7 @@ our $service = 'https://www.googleapis.com/plus/v1';
our %API = (
person => '/people/ID',
- activities => '/people/ID/activities/public',
+ activities => '/people/ID/activities/COLLECTION',
activity => '/activities/ID'
);
@@ -29,7 +30,13 @@ sub _request {
# $args is a hashref corresponding to optional query parameters (such
# as nextPageToken)
if (ref $args eq 'HASH') {
- while (my ($k, $v) = each %$args) {
+ PARAM: while (my ($k, $v) = each %$args) {
+ $k eq 'collection' and do {
+ my $p = $url->path->to_string;
+ $p =~ s/COLLECTION/$v/;
+ $url = $url->path($p);
+ next PARAM;
+ };
$url = $url->query({$k => $v});
}
}
@@ -64,14 +71,17 @@ sub person {
}
sub activities {
- my ($self, $user_id, $next) = @_;
+ my ($self, $user_id, $collection, $next) = @_;
croak 'user ID required' unless $user_id;
croak 'Invalid user ID' unless $user_id =~ /[0-9]+/;
- $next
- ? $self->_request($API{activities} => $user_id, {pageToken => $next})
- : $self->_request($API{activities} => $user_id);
+ $collection //= 'public';
+
+ my %args = (collection => $collection);
+ $args{pageToken} = $next if $next;
+
+ $self->_request($API{activities} => $user_id, \%args);
}
sub activity {
@@ -168,11 +178,14 @@ Contacts|http://portablecontacts.net/draft-spec.html> format.
=head2 C<activities>
my $acts = $plus->activities('userId');
- my $acts = $plus->activities('userId', 'nextPageToken');
+ my $acts = $plus->activities('userId', 'collection');
+ my $acts = $plus->activities('userId', 'collection', nextPageToken');
-Get person's list of public activities. Returns a L<Mojo::JSON> decoded
-hashref describing the person's activities in L<Activity
+Get person's list of public activities, returning a L<Mojo::JSON>
+decoded hashref describing the person's activities in L<Activity
Streams|http://activitystrea.ms/specs/json/1.0> format. If
+C<collection> is given, use that as the collection of activities to
+list; the default is to list C<public> activities instead. If
C<nextPageToken> is given, this method retrieves the next page of
activities this person has.
View
@@ -69,8 +69,21 @@ subtest 'get person activities' => sub {
throws_ok { $g->activities('00000000000000000000') } qr/unable to map/,
'bad person';
- $activities = $g->activities($user_id);
- isa_ok $activities => 'HASH';
+ subtest 'get person activities per collection' => sub {
+ plan tests => 3;
+
+ $activities = $g->activities($user_id);
+ isa_ok $activities => 'HASH', 'get activities (default public)';
+
+ $activities = $g->activities($user_id => 'public');
+ isa_ok $activities => 'HASH', 'get activities (explicit public)';
+
+ SKIP: {
+ skip 'no custom collections for activities/list yet', 1;
+ $activities = $g->activities($user_id => 'cats');
+ isa_ok $activities => 'HASH', 'get activities (custom collection)';
+ }
+ };
subtest 'activity list properties' => sub {
plan tests => 7;
@@ -82,7 +95,8 @@ subtest 'get person activities' => sub {
subtest 'next activity list' => sub {
plan tests => 8;
- my $next = $g->activities($user_id, $activities->{nextPageToken});
+ my $next =
+ $g->activities($user_id => 'public', $activities->{nextPageToken});
isnt $next->{nextPageToken}, $activities->{nextPageToken},
'got new activity list (the next page)';
ok $next->{$_}, "$_ in next activity list exists"

0 comments on commit f6612a0

Please sign in to comment.