File Coverage

blib/lib/Net/OpenSocial/Client.pm
Criterion Covered Total %
statement 27 90 30.0
branch 0 26 0.0
condition n/a
subroutine 9 17 52.9
pod 8 8 100.0
total 44 141 31.2


line stmt bran cond sub pod time code
1             package Net::OpenSocial::Client;
2              
3 1     1   13401 use Any::Moose;
  1         38949  
  1         7  
4 1     1   638 use Any::Moose 'X::AttributeHelpers';
  1         3  
  1         6  
5 1     1   2210 use Net::OpenSocial::Client::Protocol::Builder;
  1         4  
  1         13  
6              
7 1     1   705 use Net::OpenSocial::Client::Request::FetchPeople;
  1         5  
  1         12  
8 1     1   687 use Net::OpenSocial::Client::Request::FetchPerson;
  1         3  
  1         10  
9 1     1   657 use Net::OpenSocial::Client::Request::FetchFriends;
  1         2  
  1         9  
10 1     1   644 use Net::OpenSocial::Client::Request::FetchPersonAppData;
  1         3  
  1         13  
11 1     1   712 use Net::OpenSocial::Client::Request::FetchFriendsAppData;
  1         4  
  1         13  
12              
13             our $VERSION = '0.01_05';
14             our $DEFAULT_PROTOCOL_VERSION = '0.8.1';
15              
16             with 'Net::OpenSocial::Client::ErrorHandler';
17              
18             has 'protocol' => (
19             is => 'ro',
20             isa => 'Net::OpenSocial::Client::Protocol',
21             required => 1,
22             );
23              
24             has 'container' => (
25             is => 'ro',
26             isa => 'Net::OpenSocial::Client::Container',
27             required => 1,
28             );
29              
30             has '_requests' => (
31             is => 'ro',
32             isa => 'ArrayRef',
33             default => sub { [] },
34             metaclass => 'Collection::Array',
35             provides => { clear => 'clear_requests', },
36             );
37              
38             =head1 NAME
39              
40             Net::OpenSocial::Client - OpenSocial REST/RPC Client
41              
42             =head1 SYNOPSIS
43              
44              
45             use Net::OpenSocial::Client;
46             use Net::OpenSocial::Client::Container::OrkutSandbox;
47             use Net::OpenSocial::Client::Type::Protocol qw(REST RPC);
48             use Data::Dump qw(dump);
49              
50             my $client = Net::OpenSocial::Client->new(
51             container => Net::OpenSocial::Client::Container::OrkutSandbox->new,
52             consumer_key => q{orkut.com:623061448914},
53             consumer_secret => q{uynAeXiWTisflWX99KU1D2q5},
54             requestor => q{03067092798963641994},
55             protocol_type => RPC,
56             );
57              
58             my $friends = $client->get_friends('@me')
59             or die $client->errstr;
60             for my $person ( @{ $friends->items } ) {
61             say $person->get_field('id');
62             ...
63              
64             say dump($person->fields);
65             }
66              
67             =head1 DESCRIPTION
68              
69             OpenSocial provides API endpoints which called 'RESTful API' and 'RPC'.
70             This module allows you to handle it easily.
71             This provides you a same interface for both REST-API and RPC, so you
72             don't need to mind about the big differences between them.
73              
74             =head1 SIMPLE USAGE
75              
76             If you don't need to handle multiple requests at once for more effective performance (batch request),
77             this module provides you some simple methods that can handle resources easily.
78              
79             my $client = Net::OpenSocial::Client->new(...);
80              
81             my $user_id = '@me';
82             my $person = $client->get_person( $user_id )
83             or die $client->errstr;
84              
85             say $person->get_field('id');
86              
87             my $friends = $client->get_friends( $user_id )
88             or die $client->errstr;
89              
90             foreach my $friend ( @{ $friends->items } ) {
91             say $friend->get_field('id');
92             }
93              
94              
95             For more details, look at each methods' document.
96              
97             =over 4
98              
99             =item get_people
100             =item get_person
101             =item get_friends
102             =item get_person_appdata
103             =item get_friends_appdata
104              
105             =back
106              
107             =head1 RAW APIs AND BATCH REQUEST
108              
109             =head2 BUILD REQUEST
110              
111             You can build a request object by yourself or choose from preset.
112             See L.
113              
114             my $request = Net::OpenSocial::Client::Request->new(
115             service => PEOPLE,
116             operation => GET,
117             user_id => '@me',
118             group_id => '@friends',
119             params => {
120             itemsPerPage => 10,
121             startIndex => 10,
122             },
123             );
124              
125             or
126              
127             my $request = Net::OpenSocial::Client::Request::FetchFriends->new( '@me',
128             { itemsPerPage => 10, startIndex => 10 } );
129              
130             =head2 EXECUTE REQUEST
131              
132             You should add request to client with request-id.
133              
134             $client->add_request( req1 => $request1 );
135             $client->add_request( req2 => $request2 );
136              
137             Only execute 'add_request', you can't obtain a result corresponding to the requests.
138             To get them, you have to call 'send'.
139              
140             my $result_set = $client->send()
141              
142             Internally, it works apropriately according to the protocol type you choose.
143             If RPC is selected, multiple requests are send as one single http-request.
144             Or with REST, it send multiple http-request for each opensocial-request.
145             And it returns L object.
146              
147             =head2 PICK UP RESULT
148              
149             Pick up L object with
150             request-id you passed when invoking 'add_request'.
151              
152             my $result1 = $result_set->get_result('req1');
153             my $result2 = $result_set->get_result('req2');
154              
155             =head2 HANDLE DATA
156              
157             if ( $result1->is_error ) {
158             die sprintf q{%d: %s.},
159             $result->code, $result->message;
160             } else {
161             my $data = $result1->data;
162             ...
163             }
164              
165             the data may be L or
166             L subclass's object.
167             such like L
168             ,L
169             ,L
170             , or L.
171              
172             =head1 METHODS
173              
174             =head2 new
175              
176             use Net::OpenSocial::Client::Type::Auth qw(OAUTH ST);
177             use Net::OpenSocial::Client::Type::Format qw(JSON XML);
178             use Net::OpenSocial::Client::Type::Protocol qw(REST RPC);
179              
180             my $client = Net::OpenSocial::Client->new(
181             container => $container,
182             auty_type => OAUTH,
183             format_type => JSON,
184             protocol_type => REST,
185             );
186              
187             =head3 container
188              
189             First, you have to prepare L object.
190             You can build it with container-information you want to access as of now,
191             or choose from preset.
192             There exists container objects which support major provider.
193              
194             L
195             L
196             L
197             and etc.
198              
199              
200             my $container = Net::OpenSocial::Client::Container->new(...);
201              
202             or
203              
204             my $container = Net::OpenSocial::Client::Container::MySpace->new;
205              
206              
207             =head3 auth_type
208              
209             Authorization type.
210             You can choose from OAUTH and ST.
211             OAUTH means 2-legged or 3-legged OAuth.
212             When you choose OAUTH type, you have to pass both 'consumer_key' and 'consumer_secret'
213             parameter which you got on provider beforehand.
214              
215             my $client = Net::OpenSocial::Client->new(
216             auth_type => OAUTH, # you can omit this. OAUTH is set by default.
217             consumer_key => q{foobarbuz},
218             consumer_secret => q{foobarbuz},
219             ...
220             );
221              
222             And if you want to use 3-legged OAuth, you need to pass authorized access-token.
223             You should already have completed OAuth authorization process.
224             See L.
225              
226             my $access_token = OAuth::Lite::Token->new( token => q{aaa}, secret => {bbb} );
227              
228             my $client = Net::OpenSocial::Client->new(
229             auth_type => OAUTH, # you can omit this. OAUTH is set by default.
230             consumer_key => q{foobarbuz},
231             consumer_secret => q{foobarbuz},
232             token => $access_token,
233             ...
234             );
235              
236             Or in case you try 2-legged OAuth.
237             pass user-id of xoauth_request_id as 'requestor' parameter.
238              
239             my $client = Net::OpenSocial::Client->new(
240             auth_type => OAUTH, # you can omit this. OAUTH is set by default.
241             consumer_key => q{foobarbuz},
242             consumer_secret => q{foobarbuz},
243             requestor => q{myid},
244             ...
245             );
246              
247             ST means security-token.
248             You have pass to security-token as 'st' parameter.
249              
250             my $client = Net::OpenSocial::Client->new(
251             auth_type => ST,
252             st => q{foobarbuz},
253             ...
254             );
255              
256             =head3 format_type
257              
258             Now it only supports JSON format, so, it sets JSON by default.
259              
260             =head3 protocol_type
261              
262             REST or RPC.
263             REST is set by default.
264             Make sure that the container which you want to access supports
265             the protocol.
266              
267             If container supports RPC, and you choose it, you can execute
268             batch request.
269              
270             =head2 BUILDARGS
271              
272             See L, L.
273             You don't need to call this method directly.
274              
275             =cut
276              
277             sub BUILDARGS {
278 0     0 1   my ( $self, %args ) = @_;
279 0           my $params = {};
280 0           $params->{container} = delete $args{container};
281 0 0         unless ( exists $args{protocol} ) {
282 0           my $builder = Net::OpenSocial::Client::Protocol::Builder->new(%args);
283 0 0         my $protocol = $builder->build_protocol()
284             or die $builder->errstr;
285 0           $params->{protocol} = $protocol;
286             }
287 0           return $params;
288             }
289              
290             =head2 add_request( $request )
291              
292             Pass L or its subclass's object.
293             At this time, noghing happens. When you execute 'send' method,
294             You will get the result of this request.
295              
296             You should make ID string for the request arbitrarily,
297             and set it with the request.
298             Remember this ID to pick up a result you want from result-set
299             that you get after executing 'send' method.
300              
301             $client->add_request( $request_id => $request );
302              
303             =cut
304              
305             sub add_request {
306 0     0 1   my ( $self, $id, $req ) = @_;
307 0           $req->id($id);
308 0           push( @{ $self->_requests }, $req );
  0            
309             }
310              
311             =head2 send()
312              
313             This method send all requests that you added by 'add_request' before you call this.
314             If it's done successfuly, it returns a L object.
315              
316             To obtain a result you want, you need to invoke 'get_result' method of result-set object
317             with request-id you set when you do 'add_request'.
318             Then it returns a L object.
319              
320             my $result_set = $client->send()
321             or die $client->errstr;
322             my $result = $result_set->get_result($request_id);
323              
324             =cut
325              
326             sub send {
327 0     0 1   my $self = shift;
328 0           my @requests = @{ $self->_requests };
  0            
329 0           $self->clear_requests();
330 0 0         my $result_set = $self->protocol->execute( $self->container, \@requests )
331             or return $self->ERROR( $self->protocol->errstr );
332 0           return $result_set;
333             }
334              
335             =head2 get_people( $user_id, $group_id, $option )
336              
337             my $people = $client->get_people( '@me', '@friends', { itemsPerPage => 10 } );
338             say $people->count;
339             for my $person ( @{ $people->items } ) {
340             say $person->get_field('id');
341             }
342              
343             =cut
344              
345             sub get_people {
346 0     0 1   my ( $self, $user_id, $group_id, $params ) = @_;
347 0           $self->clear_requests();
348 0           my $req_id = 'get_people';
349 0           my $req = Net::OpenSocial::Client::Request::FetchPeople->new( $user_id,
350             $group_id, $params );
351 0           $self->add_request( $req_id => $req );
352 0 0         my $result_set = $self->send() or return;
353 0           my $result = $result_set->get_result($req_id);
354 0 0         return $self->ERROR( $result->message ) if $result->is_error;
355 0           return $result->data;
356             }
357              
358             =head2 get_person( $user_id )
359              
360             my $person = $client->get_person('@me');
361             say $person->get_field('id');
362              
363             =cut
364              
365             sub get_person {
366 0     0 1   my ( $self, $user_id ) = @_;
367 0           $self->clear_requests();
368 0           my $req_id = 'get_person';
369 0           my $req = Net::OpenSocial::Client::Request::FetchPerson->new($user_id);
370 0           $self->add_request( $req_id => $req );
371 0 0         my $result_set = $self->send() or return;
372 0           my $result = $result_set->get_result($req_id);
373 0 0         return $self->ERROR( $result->message ) if $result->is_error;
374 0           return $result->data->first;
375             }
376              
377             =head2 get_friends( $user_id, $option )
378              
379             my $people = $client->get_people( '@me', { itemsPerPage => 10 } );
380             say $people->count;
381             for my $person ( @{ $people->items } ) {
382             say $person->get_field('id');
383             }
384              
385             =cut
386              
387             sub get_friends {
388 0     0 1   my ( $self, $user_id, $params ) = @_;
389 0           $self->clear_requests();
390 0           my $req_id = 'get_friends';
391 0           my $req = Net::OpenSocial::Client::Request::FetchFriends->new( $user_id,
392             $params );
393 0           $self->add_request( $req_id => $req );
394 0 0         my $result_set = $self->send() or return;
395 0           my $result = $result_set->get_result($req_id);
396 0 0         return $self->ERROR( $result->message ) if $result->is_error;
397 0           return $result->data;
398             }
399              
400             =head2 get_person_appdata( $user_id, $option )
401              
402             returns L.
403              
404             my $appdata = $client->get_person_appdata('@me')
405             or die $client->errstr;
406             say $appdata->get_data($user_id, $key);
407              
408             =cut
409              
410             sub get_person_appdata {
411 0     0 1   my ( $self, $user_id, $params ) = @_;
412 0           $self->clear_requests();
413 0           my $req_id = 'get_person_appdata';
414 0           my $req
415             = Net::OpenSocial::Client::Request::FetchPersonAppData->new( $user_id, $params );
416 0           $self->add_request( $req_id => $req );
417 0 0         my $result_set = $self->send() or return;
418 0           my $result = $result_set->get_result($req_id);
419 0 0         return $self->ERROR( $result->message ) if $result->is_error;
420 0           return $result->data->first;
421             }
422              
423             =head2 get_friends_appdata( $user_id, $option )
424              
425             my $collection = $client->get_friends_appdata('@me')
426             or die $client->errstr;
427             for my $appdata ( @{ $collection->items } ) {
428             ...
429             }
430              
431             =cut
432              
433             sub get_friends_appdata {
434 0     0 1   my ( $self, $user_id, $params ) = @_;
435 0           $self->clear_requests();
436 0           my $req_id = 'get_friens_appdata';
437 0           my $req = Net::OpenSocial::Client::Request::FetchFriendsAppData->new(
438             $user_id, $params );
439 0           $self->add_request( $req_id => $req );
440 0 0         my $result_set = $self->send() or return;
441 0           my $result = $result_set->get_result($req_id);
442 0 0         return $self->ERROR( $result->message ) if $result->is_error;
443 0           return $result->data;
444             }
445              
446             =head1 DEVELOPMENT
447              
448             I'm woking on github.
449             http://github.com/lyokato/p5-net-opensocial-client/tree/master
450              
451             =head1 SEE ALSO
452              
453             L,
454             L,
455             L,
456             L
457             L
458              
459             =head1 AUTHOR
460              
461             Lyo Kato, Elyo.kato@gmail.comE
462              
463             =head1 COPYRIGHT AND LICENSE
464              
465             Copyright (C) 2009 by Lyo Kato
466              
467             This library is free software; you can redistribute it and/or modify
468             it under the same terms as Perl itself, either Perl version 5.8.8 or,
469             at your option, any later version of Perl 5 you may have available.
470              
471             =cut
472              
473 1     1   1190 no Any::Moose;
  1         3  
  1         5  
474             __PACKAGE__->meta->make_immutable;
475             1;
476