Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement requesting partial responses

After a bit of rest (got sick and all ;) add the feature of being able
to get partial responses.  Subtests are awesome!

Signed-off-by: Zak B. Elep <zakame@cpan.org>
  • Loading branch information...
commit 2ca490c34f5ffdb95a2624bb35ec6c0ecfed8bf5 1 parent d56c171
Zak B. Elep authored
Showing with 80 additions and 18 deletions.
  1. +24 −12 lib/Google/Plus.pm
  2. +56 −6 t/01-plus.t
36 lib/Google/Plus.pm
View
@@ -68,16 +68,18 @@ sub new {
}
sub person {
- my ($self, $user_id) = @_;
+ my ($self, $user_id, $fields) = @_;
croak 'user ID required' unless $user_id;
croak 'Invalid user ID' unless $user_id =~ /[0-9]+/;
- $self->_request($API{person} => $user_id);
+ $fields
+ ? $self->_request($API{person} => $user_id, {fields => $fields})
+ : $self->_request($API{person} => $user_id);
}
sub activities {
- my ($self, $user_id, $collection, $next) = @_;
+ my ($self, $user_id, $collection, $next, $fields) = @_;
croak 'user ID required' unless $user_id;
croak 'Invalid user ID' unless $user_id =~ /[0-9]+/;
@@ -85,18 +87,21 @@ sub activities {
$collection //= 'public';
my %args = (collection => $collection);
- $args{pageToken} = $next if $next;
+ $args{pageToken} = $next if $next;
+ $args{fields} = $fields if $fields;
$self->_request($API{activities} => $user_id, \%args);
}
sub activity {
- my ($self, $activity_id) = @_;
+ my ($self, $activity_id, $fields) = @_;
croak 'activity ID required' unless $activity_id;
croak 'Invalid activity ID' unless $activity_id =~ /\w+/;
- $self->_request($API{activity} => $activity_id);
+ $fields
+ ? $self->_request($API{activity} => $activity_id, {fields => $fields})
+ : $self->_request($API{activity} => $activity_id);
}
"Inspired by tempire's Google::Voice :3";
@@ -176,32 +181,39 @@ which you can get at L<https://code.google.com/apis/console>.
=head2 C<person>
my $person = $plus->person('userId');
+ my $person = $plus->person('userId', 'fields');
Get a Google+ person's public profile. Returns a L<Mojo::JSON> decoded
hashref describing the person's profile in L<Portable
-Contacts|http://portablecontacts.net/draft-spec.html> format.
+Contacts|http://portablecontacts.net/draft-spec.html> format. If
+C<fields> is given, limit response to the specified fields; see the
+Partial Responses section of L<https://developers.google.com/+/api>.
=head2 C<activities>
my $acts = $plus->activities('userId');
my $acts = $plus->activities('userId', 'collection');
- my $acts = $plus->activities('userId', 'collection', nextPageToken');
+ my $acts = $plus->activities('userId', 'collection', nextPage');
+ my $acts = $plus->activities('userId', 'collection', nextPage', 'fields');
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
+Streams|http://activitystrea.ms/specs/json/1.0> format; this method also
+accepts requesting partial responses if C<fields> is given. 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
+list; the default is to list C<public> activities instead. If a
+C<nextPage> token is given, this method retrieves the next page of
activities this person has.
=head2 C<activity>
my $post = $plus->activity('activityId')
+ my $post = $plus->activity('activityId', fields');
Get a specific activity/post. Returns a L<Mojo::JSON> decoded hashref
describing the activity in L<Activity
-Streams|http://activitystrea.ms/specs/json/1.0> format.
+Streams|http://activitystrea.ms/specs/json/1.0> format. If C<fields> is
+given, limit response to specified fields.
=head1 SEE ALSO
62 t/01-plus.t
View
@@ -37,7 +37,7 @@ subtest 'create object' => sub {
my $person;
subtest 'get person profile' => sub {
- plan tests => 6;
+ plan tests => 7;
can_ok $g => 'person';
@@ -56,11 +56,27 @@ subtest 'get person profile' => sub {
for qw/ aboutMe displayName gender id image organizations placesLived
tagline url urls /;
};
+
+ subtest 'person profile partial response' => sub {
+ plan tests => 6;
+
+ my @fields = qw(displayName gender aboutMe);
+
+ my $partial = $g->person($user_id, join ',' => @fields);
+ isa_ok $partial => 'HASH', 'got partial response for person';
+
+ ok $partial->{$_}, "$_ exists in partial response" for @fields;
+
+ ok !exists $partial->{birthday}, "birthday should not be in response";
+
+ throws_ok { $g->person($user_id, 'invalid,fields') } qr/Invalid field/,
+ "partial response using invalid field names";
+ };
};
my $activities;
subtest 'get person activities' => sub {
- plan tests => 7;
+ plan tests => 8;
can_ok $g => 'activities';
@@ -102,14 +118,30 @@ subtest 'get person activities' => sub {
ok $next->{$_}, "$_ in next activity list exists"
for qw/ id items nextLink nextPageToken selfLink title updated /;
};
+
+ subtest 'activity list partial response' => sub {
+ plan tests => 5;
+
+ my @fields = qw(selfLink nextLink);
+
+ my $partial = $g->activities($user_id, undef, undef, join ',' => @fields);
+ isa_ok $partial => 'HASH', 'got partial response for activity list';
+
+ ok $partial->{$_}, "$_ exists in partial response" for @fields;
+
+ ok !exists $partial->{title}, "title should not be in response";
+
+ throws_ok { $g->activities($user_id, undef, undef, 'invalid,fields') }
+ qr/Invalid field/, "partial response using invalid field names";
+ };
};
+# Google+ is the _vehicle_, Google Hangouts is the _product_.
+my $post = 'z13uxtsawqqwwbcjt04cdhsxcnfyir44xeg';
+
my $activity;
subtest 'get activity' => sub {
- plan tests => 6;
-
- # Google+ is the _vehicle_, Google Hangouts is the _product_.
- my $post = 'z13uxtsawqqwwbcjt04cdhsxcnfyir44xeg';
+ plan tests => 7;
can_ok $g => 'activity';
@@ -128,4 +160,22 @@ subtest 'get activity' => sub {
for qw/ access actor annotation id object published title updated
url verb /;
};
+
+ subtest 'activity detail partial response' => sub {
+ plan tests => 7;
+
+ my @fields = qw(id title object url);
+
+ my $partial = $g->activity($post, join ',' => @fields);
+ isa_ok $partial => 'HASH', 'got partial response for activity';
+
+ ok $partial->{$_}, "$_ exists in partial response" for @fields;
+
+ ok !exists $partial->{updated},
+ 'updated property should not be in response';
+
+ # Google throws a 404 here unlike when requesting other partials
+ throws_ok { $g->activities($post, 'invalid,fields') } qr/Not Found/,
+ "partial response using invalid field names";
+ };
};
Please sign in to comment.
Something went wrong with that request. Please try again.