Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 2 changed files with 80 additions and 18 deletions. Show diff stats Hide diff stats

  1. +24 12 lib/Google/Plus.pm
  2. +56 6 t/01-plus.t
36 lib/Google/Plus.pm
@@ -68,16 +68,18 @@ sub new {
68 68 }
69 69
70 70 sub person {
71   - my ($self, $user_id) = @_;
  71 + my ($self, $user_id, $fields) = @_;
72 72
73 73 croak 'user ID required' unless $user_id;
74 74 croak 'Invalid user ID' unless $user_id =~ /[0-9]+/;
75 75
76   - $self->_request($API{person} => $user_id);
  76 + $fields
  77 + ? $self->_request($API{person} => $user_id, {fields => $fields})
  78 + : $self->_request($API{person} => $user_id);
77 79 }
78 80
79 81 sub activities {
80   - my ($self, $user_id, $collection, $next) = @_;
  82 + my ($self, $user_id, $collection, $next, $fields) = @_;
81 83
82 84 croak 'user ID required' unless $user_id;
83 85 croak 'Invalid user ID' unless $user_id =~ /[0-9]+/;
@@ -85,18 +87,21 @@ sub activities {
85 87 $collection //= 'public';
86 88
87 89 my %args = (collection => $collection);
88   - $args{pageToken} = $next if $next;
  90 + $args{pageToken} = $next if $next;
  91 + $args{fields} = $fields if $fields;
89 92
90 93 $self->_request($API{activities} => $user_id, \%args);
91 94 }
92 95
93 96 sub activity {
94   - my ($self, $activity_id) = @_;
  97 + my ($self, $activity_id, $fields) = @_;
95 98
96 99 croak 'activity ID required' unless $activity_id;
97 100 croak 'Invalid activity ID' unless $activity_id =~ /\w+/;
98 101
99   - $self->_request($API{activity} => $activity_id);
  102 + $fields
  103 + ? $self->_request($API{activity} => $activity_id, {fields => $fields})
  104 + : $self->_request($API{activity} => $activity_id);
100 105 }
101 106
102 107 "Inspired by tempire's Google::Voice :3";
@@ -176,32 +181,39 @@ which you can get at L<https://code.google.com/apis/console>.
176 181 =head2 C<person>
177 182
178 183 my $person = $plus->person('userId');
  184 + my $person = $plus->person('userId', 'fields');
179 185
180 186 Get a Google+ person's public profile. Returns a L<Mojo::JSON> decoded
181 187 hashref describing the person's profile in L<Portable
182   -Contacts|http://portablecontacts.net/draft-spec.html> format.
  188 +Contacts|http://portablecontacts.net/draft-spec.html> format. If
  189 +C<fields> is given, limit response to the specified fields; see the
  190 +Partial Responses section of L<https://developers.google.com/+/api>.
183 191
184 192 =head2 C<activities>
185 193
186 194 my $acts = $plus->activities('userId');
187 195 my $acts = $plus->activities('userId', 'collection');
188   - my $acts = $plus->activities('userId', 'collection', nextPageToken');
  196 + my $acts = $plus->activities('userId', 'collection', nextPage');
  197 + my $acts = $plus->activities('userId', 'collection', nextPage', 'fields');
189 198
190 199 Get person's list of public activities, returning a L<Mojo::JSON>
191 200 decoded hashref describing the person's activities in L<Activity
192   -Streams|http://activitystrea.ms/specs/json/1.0> format. If
  201 +Streams|http://activitystrea.ms/specs/json/1.0> format; this method also
  202 +accepts requesting partial responses if C<fields> is given. If
193 203 C<collection> is given, use that as the collection of activities to
194   -list; the default is to list C<public> activities instead. If
195   -C<nextPageToken> is given, this method retrieves the next page of
  204 +list; the default is to list C<public> activities instead. If a
  205 +C<nextPage> token is given, this method retrieves the next page of
196 206 activities this person has.
197 207
198 208 =head2 C<activity>
199 209
200 210 my $post = $plus->activity('activityId')
  211 + my $post = $plus->activity('activityId', fields');
201 212
202 213 Get a specific activity/post. Returns a L<Mojo::JSON> decoded hashref
203 214 describing the activity in L<Activity
204   -Streams|http://activitystrea.ms/specs/json/1.0> format.
  215 +Streams|http://activitystrea.ms/specs/json/1.0> format. If C<fields> is
  216 +given, limit response to specified fields.
205 217
206 218 =head1 SEE ALSO
207 219
62 t/01-plus.t
@@ -37,7 +37,7 @@ subtest 'create object' => sub {
37 37
38 38 my $person;
39 39 subtest 'get person profile' => sub {
40   - plan tests => 6;
  40 + plan tests => 7;
41 41
42 42 can_ok $g => 'person';
43 43
@@ -56,11 +56,27 @@ subtest 'get person profile' => sub {
56 56 for qw/ aboutMe displayName gender id image organizations placesLived
57 57 tagline url urls /;
58 58 };
  59 +
  60 + subtest 'person profile partial response' => sub {
  61 + plan tests => 6;
  62 +
  63 + my @fields = qw(displayName gender aboutMe);
  64 +
  65 + my $partial = $g->person($user_id, join ',' => @fields);
  66 + isa_ok $partial => 'HASH', 'got partial response for person';
  67 +
  68 + ok $partial->{$_}, "$_ exists in partial response" for @fields;
  69 +
  70 + ok !exists $partial->{birthday}, "birthday should not be in response";
  71 +
  72 + throws_ok { $g->person($user_id, 'invalid,fields') } qr/Invalid field/,
  73 + "partial response using invalid field names";
  74 + };
59 75 };
60 76
61 77 my $activities;
62 78 subtest 'get person activities' => sub {
63   - plan tests => 7;
  79 + plan tests => 8;
64 80
65 81 can_ok $g => 'activities';
66 82
@@ -102,14 +118,30 @@ subtest 'get person activities' => sub {
102 118 ok $next->{$_}, "$_ in next activity list exists"
103 119 for qw/ id items nextLink nextPageToken selfLink title updated /;
104 120 };
  121 +
  122 + subtest 'activity list partial response' => sub {
  123 + plan tests => 5;
  124 +
  125 + my @fields = qw(selfLink nextLink);
  126 +
  127 + my $partial = $g->activities($user_id, undef, undef, join ',' => @fields);
  128 + isa_ok $partial => 'HASH', 'got partial response for activity list';
  129 +
  130 + ok $partial->{$_}, "$_ exists in partial response" for @fields;
  131 +
  132 + ok !exists $partial->{title}, "title should not be in response";
  133 +
  134 + throws_ok { $g->activities($user_id, undef, undef, 'invalid,fields') }
  135 + qr/Invalid field/, "partial response using invalid field names";
  136 + };
105 137 };
106 138
  139 +# Google+ is the _vehicle_, Google Hangouts is the _product_.
  140 +my $post = 'z13uxtsawqqwwbcjt04cdhsxcnfyir44xeg';
  141 +
107 142 my $activity;
108 143 subtest 'get activity' => sub {
109   - plan tests => 6;
110   -
111   - # Google+ is the _vehicle_, Google Hangouts is the _product_.
112   - my $post = 'z13uxtsawqqwwbcjt04cdhsxcnfyir44xeg';
  144 + plan tests => 7;
113 145
114 146 can_ok $g => 'activity';
115 147
@@ -128,4 +160,22 @@ subtest 'get activity' => sub {
128 160 for qw/ access actor annotation id object published title updated
129 161 url verb /;
130 162 };
  163 +
  164 + subtest 'activity detail partial response' => sub {
  165 + plan tests => 7;
  166 +
  167 + my @fields = qw(id title object url);
  168 +
  169 + my $partial = $g->activity($post, join ',' => @fields);
  170 + isa_ok $partial => 'HASH', 'got partial response for activity';
  171 +
  172 + ok $partial->{$_}, "$_ exists in partial response" for @fields;
  173 +
  174 + ok !exists $partial->{updated},
  175 + 'updated property should not be in response';
  176 +
  177 + # Google throws a 404 here unlike when requesting other partials
  178 + throws_ok { $g->activities($post, 'invalid,fields') } qr/Not Found/,
  179 + "partial response using invalid field names";
  180 + };
131 181 };

0 comments on commit 2ca490c

Please sign in to comment.
Something went wrong with that request. Please try again.