File Coverage

blib/lib/Etherpad/API.pm
Criterion Covered Total %
statement 38 887 4.2
branch 5 342 1.4
condition 5 9 55.5
subroutine 11 56 19.6
pod 48 48 100.0
total 107 1342 7.9


line stmt bran cond sub pod time code
1             package Etherpad::API;
2 1     1   12687 use strict;
  1         2  
  1         29  
3 1     1   35083 use LWP::UserAgent;
  1         52690  
  1         26  
4 1     1   497 use URI::Encode qw(uri_encode);
  1         7558  
  1         48  
5 1     1   478 use JSON::XS;
  1         3365  
  1         43  
6 1     1   4 use Carp qw(carp);
  1         1  
  1         31  
7              
8              
9             BEGIN {
10 1     1   3 use Exporter ();
  1         1  
  1         11  
11 1     1   3 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         0  
  1         68  
12 1     1   1 $VERSION = '1.2.12.0';
13 1         5 @ISA = qw(Exporter);
14             #Give a hoot don't pollute, do not export more than needed by default
15 1         1 @EXPORT = qw();
16 1         1 @EXPORT_OK = qw();
17 1         5121 %EXPORT_TAGS = ();
18             }
19              
20             #################### main pod documentation begin ###################
21              
22              
23             =head1 NAME
24              
25             Etherpad::API - Access Etherpad Lite API easily
26              
27             =head1 SYNOPSIS
28              
29             use Etherpad::API;
30             my $ec = Etherpad::API->new({
31             url => "http://pad.example.com",
32             apikey => "teirnausetsraunieti",
33             user => "apiuser",
34             password => "apipassword"
35             });
36              
37             $ec->create_pad('new_pad_name');
38              
39             =head1 DESCRIPTION
40              
41             This is a client for the Etherpad Lite HTTP API.
42              
43             The Etherpad API currently supported is the 1.2.12 (Etherpad version: 1.5.6)
44              
45             Please note that this module now uses the Etherpad API version number for versioning with a revision number (for bugfixes).
46              
47             =head1 USAGE
48              
49             =cut
50              
51             #################### main pod documentation end ###################
52              
53              
54              
55              
56             #################### subroutine header begin ####################
57              
58             =head2 new
59              
60             Usage : my $ec = Etherpad::API->new({ url => "http://pad.example.com", apikey => "secretapikey" });
61             Purpose : Constructor
62             Returns : An Etherpad::API object
63             Argument : An mandatory hash reference, containing at least two keys:
64             url : mandatory, the epl URL (trailing slashes will be removed)
65             apikey : mandatory, the epl API key
66             user : optional, the user for epl authentication
67             password : optional, the password for epl authentication
68             proxy : optional, hash reference which can contain two keys: http and https.
69             These are the settings if you are behind a http(s) proxy
70              
71             =cut
72              
73             #################### subroutine header end ####################
74              
75             sub new {
76 4     4 1 441 my ($class, $parameters) = @_;
77 4 100 100     23 return undef unless (defined($parameters->{url}) && defined($parameters->{apikey}));
78              
79 1 50       5 $parameters->{url} =~ s#/+$## if (defined($parameters->{url}));
80 1 50 33     4 if (defined($parameters->{user}) && defined($parameters->{password})) {
81 0         0 $parameters->{url} =~ s#^(https?://)#$1$parameters->{user}:$parameters->{password}@#;
82             }
83              
84 1         7 $parameters->{ua} = LWP::UserAgent->new;
85              
86 1 50       3367 if (defined($parameters->{proxy})) {
87 0 0       0 $parameters->{ua}->proxy('http', $parameters->{proxy}->{http}) if (defined($parameters->{proxy}->{http}));
88 0 0       0 $parameters->{ua}->proxy('https', $parameters->{proxy}->{https}) if (defined($parameters->{proxy}->{https}));
89             }
90              
91 1   33     8 my $self = bless ($parameters, ref ($class) || $class);
92 1         4 return $self;
93             }
94              
95              
96             #################### subroutine header begin ####################
97              
98             =head2 apikey
99              
100             Usage : $ec->apikey();
101             Purpose : Get the apikey
102             Returns : The apikey
103              
104             =cut
105              
106             #################### subroutine header end ####################
107              
108             sub apikey {
109 1     1 1 2 my $self = shift;
110 1         4 return $self->{apikey};
111             }
112              
113              
114             #################### subroutine header begin ####################
115              
116             =head2 url
117              
118             Usage : $ec->url();
119             Purpose : Get the epl URL
120             Returns : The epl URL
121              
122             =cut
123              
124             #################### subroutine header end ####################
125              
126             sub url {
127 1     1 1 360 my $self = shift;
128 1         8 return $self->{url};
129             }
130              
131              
132             #################################################################
133             =head2 Groups
134              
135             Pads can belong to a group. The padID of grouppads is starting with a groupID like g.asdfasdfasdfasdf$test
136              
137             See http://etherpad.org/doc/v1.5.1/#index_groups
138              
139             =cut
140             #################################################################
141              
142             #################### subroutine header begin ####################
143              
144             =head3 create_group
145              
146             Usage : $ec->create_group();
147             Purpose : Creates a new group
148             Returns : The new group ID
149             Argument : None
150             See : http://etherpad.org/doc/v1.5.1/#index_creategroup
151              
152             =cut
153              
154             #################### subroutine header end ####################
155              
156             sub create_group {
157 0     0 1   my $self = shift;
158              
159 0           my $api = '1';
160 0           my $method = 'createGroup';
161              
162 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
163 0           my $response = $self->{ua}->get($request);
164 0 0         if ($response->is_success) {
165 0           my $hash = decode_json $response->decoded_content;
166 0 0         if ($hash->{code} == 0) {
167 0           return $hash->{data}->{groupID};
168             } else {
169 0           carp $hash->{message};
170 0           return undef;
171             }
172             } else {
173 0           carp $response->status_line;
174 0           return undef;
175             }
176             }
177              
178              
179             #################### subroutine header begin ####################
180              
181             =head3 create_group_if_not_exists_for
182              
183             Usage : $ec->create_group_if_not_exists_for('groupMapper');
184             Purpose : This functions helps you to map your application group ids to epl group ids
185             Returns : The epl group id
186             Argument : Your application group id
187             See : http://etherpad.org/doc/v1.5.1/#index_creategroupifnotexistsfor_groupmapper
188              
189             =cut
190              
191             #################### subroutine header end ####################
192              
193              
194             sub create_group_if_not_exists_for {
195 0     0 1   my $self = shift;
196 0           my $group_mapper = shift;
197              
198 0 0         unless (defined($group_mapper)) {
199 0           carp 'Please provide a group id';
200 0           return undef;
201             }
202              
203 0           my $api = '1';
204 0           my $method = 'createGroupIfNotExistsFor';
205              
206 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
207 0           $request .= '&groupMapper=' . uri_encode($group_mapper, { encode_reserved => 1 });
208 0           my $response = $self->{ua}->get($request);
209 0 0         if ($response->is_success) {
210 0           my $hash = decode_json $response->decoded_content;
211 0 0         if ($hash->{code} == 0) {
212 0           return $hash->{data}->{groupID};
213             } else {
214 0           carp $hash->{message};
215 0           return undef;
216             }
217             }
218             else {
219 0           carp $response->status_line;
220 0           return undef;
221             }
222             }
223              
224              
225             #################### subroutine header begin ####################
226              
227             =head3 delete_group
228              
229             Usage : $ec->delete_group('groupId');
230             Purpose : Deletes a group
231             Returns : 1 if it succeeds
232             Argument : The id of the group you want to delete
233             See : http://etherpad.org/doc/v1.5.1/#index_deletegroup_groupid
234              
235             =cut
236              
237             #################### subroutine header end ####################
238              
239              
240             sub delete_group {
241 0     0 1   my $self = shift;
242 0           my $group_id = shift;
243              
244 0 0         unless (defined($group_id)) {
245 0           carp 'Please provide a group id';
246 0           return undef;
247             }
248              
249 0           my $api = '1';
250 0           my $method = 'deleteGroup';
251              
252 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
253 0           $request .= '&groupID=' . uri_encode($group_id, { encode_reserved => 1 });
254 0           my $response = $self->{ua}->get($request);
255 0 0         if ($response->is_success) {
256 0           my $hash = decode_json $response->decoded_content;
257 0 0         if ($hash->{code} == 0) {
258 0           return 1;
259             } else {
260 0           carp $hash->{message};
261 0           return undef;
262             }
263             }
264             else {
265 0           carp $response->status_line;
266 0           return undef;
267             }
268             }
269              
270              
271             #################### subroutine header begin ####################
272              
273             =head3 list_pads
274              
275             Usage : $ec->list_pads('groupId');
276             Purpose : Returns all pads of this group
277             Returns : An array or an array reference (depending on the context) which contains the pad ids
278             Argument : The id of the group from which you want the pads
279             See : http://etherpad.org/doc/v1.5.1/#index_listpads_groupid
280              
281             =cut
282              
283             #################### subroutine header end ####################
284              
285              
286             sub list_pads {
287 0     0 1   my $self = shift;
288 0           my $group_id = shift;
289              
290 0 0         unless (defined($group_id)) {
291 0           carp 'Please provide a group id';
292 0           return undef;
293             }
294              
295 0           my $api = '1';
296 0           my $method = 'listPads';
297              
298 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
299 0           $request .= '&groupID=' . uri_encode($group_id, { encode_reserved => 1 });
300 0           my $response = $self->{ua}->get($request);
301 0 0         if ($response->is_success) {
302 0           my $hash = decode_json $response->decoded_content;
303 0 0         if ($hash->{code} == 0) {
304 0 0         return (wantarray) ? @{$hash->{data}->{padIDs}}: $hash->{data}->{padIDs};
  0            
305             } else {
306 0           carp $hash->{message};
307 0           return undef;
308             }
309             }
310             else {
311 0           carp $response->status_line;
312 0           return undef;
313             }
314             }
315              
316              
317             #################### subroutine header begin ####################
318              
319             =head3 create_group_pad
320              
321             Usage : $ec->create_group_pad('groupID', 'padName' [, 'text'])
322             Purpose : Creates a new pad in this group
323             Returns : 1 if it succeeds
324             Argument : The group id, the pad name, optionally takes the pad's initial text
325             See : http://etherpad.org/doc/v1.5.1/#index_creategrouppad_groupid_padname_text
326              
327             =cut
328              
329             #################### subroutine header end ####################
330              
331              
332             sub create_group_pad {
333 0     0 1   my $self = shift;
334 0           my $group_id = shift;
335 0           my $pad_name = shift;
336 0           my $text = shift;
337              
338 0 0         unless (defined($pad_name)) {
339 0           carp 'Please provide at least 2 arguments : the group id and the pad name';
340             return undef
341 0           }
342              
343 0           my $api = '1';
344 0           my $method = 'createGroupPad';
345              
346 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
347 0           $request .= '&groupID=' . uri_encode($group_id, { encode_reserved => 1 }) . '&padName=' . uri_encode($pad_name, { encode_reserved => 1 });
348 0 0         $request .= '&text=' . uri_encode($text, {encode_reserved => 1}) if (defined($text));
349 0           my $response = $self->{ua}->get($request);
350 0 0         if ($response->is_success) {
351 0           my $hash = decode_json $response->decoded_content;
352 0 0         if ($hash->{code} == 0) {
353 0           return 1;
354             } else {
355 0           carp $hash->{message};
356 0           return undef;
357             }
358             }
359             else {
360 0           carp $response->status_line;
361 0           return undef;
362             }
363             }
364              
365              
366             #################### subroutine header begin ####################
367              
368             =head3 list_all_groups
369              
370             Usage : $ec->list_all_groups()
371             Purpose : Lists all existing groups
372             Returns : An array or an array reference (depending on the context) which contains the groups ids
373             Argument : None
374             See : http://etherpad.org/doc/v1.5.1/#index_listallgroups
375              
376             =cut
377              
378             #################### subroutine header end ####################
379              
380              
381             sub list_all_groups {
382 0     0 1   my $self = shift;
383              
384 0           my $api = '1.1';
385 0           my $method = 'listAllGroups';
386              
387 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
388 0           my $response = $self->{ua}->get($request);
389 0 0         if ($response->is_success) {
390 0           my $hash = decode_json $response->decoded_content;
391 0 0         if ($hash->{code} == 0) {
392 0 0         return (wantarray) ? @{$hash->{data}->{groupIDs}}: $hash->{data}->{groupIDs};
  0            
393             } else {
394 0           carp $hash->{message};
395 0           return undef;
396             }
397             }
398             else {
399 0           carp $response->status_line;
400 0           return undef;
401             }
402             }
403              
404              
405             #################################################################
406             =head2 Author
407              
408             These authors are bound to the attributes the users choose (color and name).
409              
410             See http://etherpad.org/doc/v1.5.1/#index_author
411              
412             =cut
413             #################################################################
414              
415             #################### subroutine header begin ####################
416              
417             =head3 create_author
418              
419             Usage : $ec->create_author(['name'])
420             Purpose : Creates a new author
421             Returns : The new author ID
422             Argument : Optionally takes a string as argument : the new author's name
423             See : http://etherpad.org/doc/v1.5.1/#index_createauthor_name
424              
425             =cut
426              
427             #################### subroutine header end ####################
428              
429              
430             sub create_author {
431 0     0 1   my $self = shift;
432 0           my $name = shift;
433              
434 0           my $api = '1';
435 0           my $method = 'createAuthor';
436              
437 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
438 0 0         $request .= '&name=' . uri_encode($name, { encode_reserved => 1 }) if (defined($name));
439 0           my $response = $self->{ua}->get($request);
440 0 0         if ($response->is_success) {
441 0           my $hash = decode_json $response->decoded_content;
442 0 0         if ($hash->{code} == 0) {
443 0           return $hash->{data}->{authorID};
444             } else {
445 0           carp $hash->{message};
446 0           return undef;
447             }
448             }
449             else {
450 0           carp $response->status_line;
451 0           return undef;
452             }
453             }
454              
455              
456             #################### subroutine header begin ####################
457              
458             =head3 create_author_if_not_exists_for
459              
460             Usage : $ec->create_author_if_not_exists_for(authorMapper [, name])
461             Purpose : This functions helps you to map your application author ids to epl author ids
462             Returns : The epl author ID
463             Argument : Your application author ID (mandatory) and optionally the epl author name
464             See : http://etherpad.org/doc/v1.5.1/#index_createauthorifnotexistsfor_authormapper_name
465              
466             =cut
467              
468             #################### subroutine header end ####################
469              
470              
471             sub create_author_if_not_exists_for {
472 0     0 1   my $self = shift;
473 0           my $author_mapper = shift;
474 0           my $name = shift;
475              
476 0 0         unless (defined($author_mapper)) {
477 0           carp 'Please provide your application author id';
478 0           return undef;
479             }
480              
481 0           my $api = '1';
482 0           my $method = 'createAuthorIfNotExistsFor';
483              
484 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
485 0           $request .= '&authorMapper=' . uri_encode($author_mapper, { encode_reserved => 1 });
486 0 0         $request .= '&name=' . uri_encode($name, { encode_reserved => 1 }) if (defined($name));
487 0           my $response = $self->{ua}->get($request);
488 0 0         if ($response->is_success) {
489 0           my $hash = decode_json $response->decoded_content;
490 0 0         if ($hash->{code} == 0) {
491 0           return $hash->{data}->{authorID};
492             } else {
493 0           carp $hash->{message};
494 0           return undef;
495             }
496             }
497             else {
498 0           carp $response->status_line;
499 0           return undef;
500             }
501             }
502              
503              
504             #################### subroutine header begin ####################
505              
506             =head3 list_pads_of_author
507              
508             Usage : $ec->list_pads_of_author('authorID')
509             Purpose : Returns an array of all pads this author contributed to
510             Returns : An array or an array reference depending on the context, containing the pads names
511             Argument : An epl author ID
512             See : http://etherpad.org/doc/v1.5.1/#index_listpadsofauthor_authorid
513              
514             =cut
515              
516             #################### subroutine header end ####################
517              
518              
519             sub list_pads_of_author {
520 0     0 1   my $self = shift;
521 0           my $author_id = shift;
522              
523 0 0         unless (defined($author_id)) {
524 0           carp 'Please provide an author id';
525 0           return undef;
526             }
527              
528 0           my $api = '1';
529 0           my $method = 'listPadsOfAuthor';
530              
531 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
532 0           $request .= '&authorID=' . uri_encode($author_id, { encode_reserved => 1 });
533 0           my $response = $self->{ua}->get($request);
534 0 0         if ($response->is_success) {
535 0           my $hash = decode_json $response->decoded_content;
536 0 0         if ($hash->{code} == 0) {
537 0 0         return (wantarray) ? @{$hash->{data}->{padIDs}}: $hash->{data}->{padIDs};
  0            
538             } else {
539 0           carp $hash->{message};
540 0           return undef;
541             }
542             }
543             else {
544 0           carp $response->status_line;
545 0           return undef;
546             }
547             }
548              
549              
550             #################### subroutine header begin ####################
551              
552             =head3 get_author_name
553              
554             Usage : $ec->get_author_name('authorID')
555             Purpose : Returns the Author Name of the author
556             Returns : The author name
557             Argument : The epl author ID
558             See : http://etherpad.org/doc/v1.5.1/#index_getauthorname_authorid
559              
560             =cut
561              
562             #################### subroutine header end ####################
563              
564              
565             sub get_author_name {
566 0     0 1   my $self = shift;
567 0           my $author_id = shift;
568              
569 0 0         unless (defined($author_id)) {
570 0           carp 'Please provide an author id';
571 0           return undef;
572             }
573              
574 0           my $api = '1.1';
575 0           my $method = 'getAuthorName';
576              
577 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
578 0           $request .= '&authorID=' . uri_encode($author_id, { encode_reserved => 1 });
579 0           my $response = $self->{ua}->get($request);
580 0 0         if ($response->is_success) {
581 0           my $hash = decode_json $response->decoded_content;
582 0 0         if ($hash->{code} == 0) {
583             # Change in the API, without documentation
584 0 0         my $data = (ref($hash->{data}) eq 'HASH') ? $hash->{data}->{authorName} : $hash->{data};
585 0           return $data;
586             } else {
587 0           carp $hash->{message};
588 0           return undef;
589             }
590             } else {
591 0           carp $response->status_line;
592 0           return undef;
593             }
594             }
595              
596              
597             #################################################################
598             =head2 Session
599              
600             Sessions can be created between a group and an author. This allows an author to access more than one group. The sessionID will be set as a cookie to the client and is valid until a certain date. The session cookie can also contain multiple comma-seperated sessionIDs, allowing a user to edit pads in different groups at the same time. Only users with a valid session for this group, can access group pads. You can create a session after you authenticated the user at your web application, to give them access to the pads. You should save the sessionID of this session and delete it after the user logged out.
601              
602             See http://etherpad.org/doc/v1.5.1/#index_session
603              
604             =cut
605             #################################################################
606              
607             #################### subroutine header begin ####################
608              
609             =head3 create_session
610              
611             Usage : $ec->create_session('groupID', 'authorID', 'validUntil')
612             Purpose : Creates a new session. validUntil is an unix timestamp in seconds
613             Returns : The epl session ID
614             Argument : An epl group ID, an epl author ID and an valid unix timestamp (the session validity end date)
615             See : http://etherpad.org/doc/v1.5.1/#index_createsession_groupid_authorid_validuntil
616              
617             =cut
618              
619             #################### subroutine header end ####################
620              
621              
622             sub create_session {
623 0     0 1   my $self = shift;
624 0           my $group_id = shift;
625 0           my $author_id = shift;
626 0           my $valid_until = shift;
627              
628 0 0         unless (defined($valid_until)) {
629 0           carp 'Please provide 3 arguments : the group id, the author id and a valid unix timestamp';
630 0           return undef;
631             }
632 0 0         unless ($valid_until =~ m/\d+/) {
633 0           carp 'Please provide a *valid* unix timestamp as third argument';
634 0           return undef;
635             }
636              
637 0           my $api = '1';
638 0           my $method = 'createSession';
639              
640 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
641 0           $request .= '&groupID=' . uri_encode($group_id, { encode_reserved => 1 }) . '&authorID=' . uri_encode($author_id, { encode_reserved => 1 }) . '&validUntil=' . $valid_until;
642 0           my $response = $self->{ua}->get($request);
643 0 0         if ($response->is_success) {
644 0           my $hash = decode_json $response->decoded_content;
645 0 0         if ($hash->{code} == 0) {
646 0           return $hash->{data}->{sessionID};
647             } else {
648 0           carp $hash->{message};
649 0           return undef;
650             }
651             }
652             else {
653 0           carp $response->status_line;
654 0           return undef;
655             }
656             }
657              
658              
659             #################### subroutine header begin ####################
660              
661             =head3 delete_session
662              
663             Usage : $ec->delete_session('sessionID')
664             Purpose : Deletes a session
665             Returns : 1 if it succeeds
666             Argument : An epl session ID
667             See : http://etherpad.org/doc/v1.5.1/#index_deletesession_sessionid
668              
669             =cut
670              
671             #################### subroutine header end ####################
672              
673              
674             sub delete_session {
675 0     0 1   my $self = shift;
676 0           my $session_id = shift;
677              
678 0 0         unless (defined($session_id)) {
679 0           carp 'Please provide a session id';
680 0           return undef;
681             }
682              
683 0           my $api = '1';
684 0           my $method = 'deleteSession';
685              
686 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
687 0           $request .= '&sessionID=' . $session_id;
688 0           my $response = $self->{ua}->get($request);
689 0 0         if ($response->is_success) {
690 0           my $hash = decode_json $response->decoded_content;
691 0 0         if ($hash->{code} == 0) {
692 0           return 1;
693             } else {
694 0           carp $hash->{message};
695 0           return undef;
696             }
697             }
698             else {
699 0           carp $response->status_line;
700 0           return undef;
701             }
702             }
703              
704              
705             #################### subroutine header begin ####################
706              
707             =head3 get_session_info
708              
709             Usage : $ec->get_session_info('sessionID')
710             Purpose : Returns informations about a session
711             Returns : A hash reference, containing 3 keys : authorID, groupID and validUntil
712             Argument : An epl session ID
713             See : http://etherpad.org/doc/v1.5.1/#index_getsessioninfo_sessionid
714              
715             =cut
716              
717             #################### subroutine header end ####################
718              
719              
720             sub get_session_info {
721 0     0 1   my $self = shift;
722 0           my $session_id = shift;
723              
724 0 0         unless (defined($session_id)) {
725 0           carp 'Please provide a session id';
726 0           return undef;
727             }
728              
729 0           my $api = '1';
730 0           my $method = 'getSessionInfo';
731              
732 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
733 0           $request .= '&sessionID=' . $session_id;
734 0           my $response = $self->{ua}->get($request);
735 0 0         if ($response->is_success) {
736 0           my $hash = decode_json $response->decoded_content;
737 0 0         if ($hash->{code} == 0) {
738 0           return $hash->{data};
739             } else {
740 0           carp $hash->{message};
741 0           return undef;
742             }
743             }
744             else {
745 0           carp $response->status_line;
746 0           return undef;
747             }
748             }
749              
750              
751             #################### subroutine header begin ####################
752              
753             =head3 list_sessions_of_group
754              
755             Usage : $ec->list_sessions_of_group('groupID')
756             Purpose : Returns all sessions of a group
757             Returns : Returns a hash reference, which keys are sessions ID and values are sessions infos (see get_session_info)
758             Argument : An epl group ID
759             See : http://etherpad.org/doc/v1.5.1/#index_listsessionsofgroup_groupid
760              
761             =cut
762              
763             #################### subroutine header end ####################
764              
765              
766             sub list_sessions_of_group {
767 0     0 1   my $self = shift;
768 0           my $group_id = shift;
769              
770 0 0         unless (defined($group_id)) {
771 0           carp 'Please provide a group id';
772 0           return undef;
773             }
774              
775 0           my $api = '1';
776 0           my $method = 'listSessionsOfGroup';
777              
778 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
779 0           $request .= '&groupID=' . uri_encode($group_id, { encode_reserved => 1 });
780 0           my $response = $self->{ua}->get($request);
781 0 0         if ($response->is_success) {
782 0           my $hash = decode_json $response->decoded_content;
783 0 0         if ($hash->{code} == 0) {
784 0           return $hash->{data};
785             } else {
786 0           carp $hash->{message};
787 0           return undef;
788             }
789             }
790             else {
791 0           carp $response->status_line;
792 0           return undef;
793             }
794             }
795              
796              
797             #################### subroutine header begin ####################
798              
799             =head3 list_sessions_of_author
800              
801             Usage : $ec->list_sessions_of_author('authorID')
802             Purpose : Returns all sessions of an author
803             Returns : Returns a hash reference, which keys are sessions ID and values are sessions infos (see get_session_info)
804             Argument : An epl group ID
805             See : http://etherpad.org/doc/v1.5.1/#index_listsessionsofauthor_authorid
806              
807             =cut
808              
809             #################### subroutine header end ####################
810              
811              
812             sub list_sessions_of_author {
813 0     0 1   my $self = shift;
814 0           my $author_id = shift;
815              
816 0 0         unless (defined($author_id)) {
817 0           carp 'Please provide an author id';
818 0           return undef;
819             }
820              
821 0           my $api = '1';
822 0           my $method = 'listSessionsOfAuthor';
823              
824 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
825 0           $request .= '&authorID=' . uri_encode($author_id, { encode_reserved => 1 });
826 0           my $response = $self->{ua}->get($request);
827 0 0         if ($response->is_success) {
828 0           my $hash = decode_json $response->decoded_content;
829 0 0         if ($hash->{code} == 0) {
830 0           return $hash->{data};
831             } else {
832 0           carp $hash->{message};
833 0           return undef;
834             }
835             }
836             else {
837 0           carp $response->status_line;
838 0           return undef;
839             }
840             }
841              
842              
843             #################################################################
844             =head2 Pad Content
845              
846             Pad content can be updated and retrieved through the API.
847              
848             See http://etherpad.org/doc/v1.5.1/#index_pad_content
849              
850             =cut
851             #################################################################
852              
853             #################### subroutine header begin ####################
854              
855             =head3 get_text
856              
857             Usage : $ec->get_text('padID', ['rev'])
858             Purpose : Returns the text of a pad
859             Returns : A string, containing the text of the pad
860             Argument : Takes a pad ID (mandatory) and optionally a revision number
861             See : http://etherpad.org/doc/v1.5.1/#index_gettext_padid_rev
862              
863             =cut
864              
865             #################### subroutine header end ####################
866              
867              
868             sub get_text {
869 0     0 1   my $self = shift;
870 0           my $pad_id = shift;
871 0           my $rev = shift;
872              
873 0 0         unless (defined($pad_id)) {
874 0           carp 'Please provide at least a pad id';
875 0           return undef;
876             }
877              
878 0           my $api = '1';
879 0           my $method = 'getText';
880              
881 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
882 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
883 0 0         $request .= '&rev=' . $rev if (defined($rev));
884 0           my $response = $self->{ua}->get($request);
885 0 0         if ($response->is_success) {
886 0           my $hash = decode_json $response->decoded_content;
887 0 0         if ($hash->{code} == 0) {
888 0           return $hash->{data}->{text};
889             } else {
890 0           carp $hash->{message};
891 0           return undef;
892             }
893             }
894             else {
895 0           carp $response->status_line;
896 0           return undef;
897             }
898             }
899              
900              
901             #################### subroutine header begin ####################
902              
903             =head3 set_text
904              
905             Usage : $ec->set_text('padID', 'text')
906             Purpose : Sets the text of a pad
907             Returns : 1 if it succeeds
908             Argument : Takes a pad ID and the text you want to set (both mandatory)
909             See : http://etherpad.org/doc/v1.5.1/#index_settext_padid_text
910              
911             =cut
912              
913             #################### subroutine header end ####################
914              
915              
916             sub set_text {
917 0     0 1   my $self = shift;
918 0           my $pad_id = shift;
919 0           my $text = shift;
920              
921 0 0         unless (defined($text)) {
922 0           carp 'Please provide 2 arguments : a pad id and a text';
923 0           return undef;
924             }
925              
926 0           my $api = '1';
927 0           my $method = 'setText';
928              
929 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
930 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1}) . '&text=' . uri_encode($text, {encode_reserved => 1});
931 0           my $response = $self->{ua}->get($request);
932 0 0         if ($response->is_success) {
933 0           my $hash = decode_json $response->decoded_content;
934 0 0         if ($hash->{code} == 0) {
935 0           return 1;
936             } else {
937 0           carp $hash->{message};
938 0           return undef;
939             }
940             }
941             else {
942 0           carp $response->status_line;
943 0           return undef;
944             }
945             }
946              
947              
948             #################### subroutine header begin ####################
949              
950             =head3 get_html
951              
952             Usage : $ec->get_html('padID', ['rev'])
953             Purpose : Returns the text of a pad formatted as html
954             Returns : A string, containing the text of the pad formatted as html
955             Argument : Takes a pad ID (mandatory) and optionally a revision number
956             See : http://etherpad.org/doc/v1.5.1/#index_gethtml_padid_rev
957              
958             =cut
959              
960             #################### subroutine header end ####################
961              
962              
963             sub get_html {
964 0     0 1   my $self = shift;
965 0           my $pad_id = shift;
966 0           my $rev = shift;
967              
968 0 0         unless (defined($pad_id)) {
969 0           carp 'Please provide at least a pad id';
970 0           return undef;
971             }
972              
973 0           my $api = '1';
974 0           my $method = 'getHTML';
975              
976 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
977 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
978 0 0         $request .= '&rev=' . $rev if (defined($rev));
979 0           my $response = $self->{ua}->get($request);
980 0 0         if ($response->is_success) {
981 0           my $hash = decode_json $response->decoded_content;
982 0 0         if ($hash->{code} == 0) {
983 0           return $hash->{data}->{html};
984             } else {
985 0           carp $hash->{message};
986 0           return undef;
987             }
988             }
989             else {
990 0           carp $response->status_line;
991 0           return undef;
992             }
993             }
994              
995              
996             #################### subroutine header begin ####################
997              
998             =head3 set_html
999              
1000             Usage : $ec->set_html('padID', 'html')
1001             Purpose : Sets the text of a pad based on HTML, HTML must be well formed.
1002             Returns : 1 if it succeeds
1003             Argument : Takes a pad ID and the HTML code you want to set (both mandatory)
1004             See : http://etherpad.org/doc/v1.5.1/#index_sethtml_padid_html
1005              
1006             =cut
1007              
1008             #################### subroutine header end ####################
1009              
1010              
1011             sub set_html {
1012 0     0 1   my $self = shift;
1013 0           my $pad_id = shift;
1014 0           my $html = shift;
1015              
1016 0 0         unless (defined($html)) {
1017 0           carp 'Please provide 2 arguments : a pad id and a HTML code';
1018 0           return undef;
1019             }
1020              
1021 0           my $api = '1';
1022 0           my $method = 'setHTML';
1023              
1024 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1025 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1}) . '&html=' . uri_encode($html, {encode_reserved => 1});
1026 0           my $response = $self->{ua}->get($request);
1027 0 0         if ($response->is_success) {
1028 0           my $hash = decode_json $response->decoded_content;
1029 0 0         if ($hash->{code} == 0) {
1030 0           return 1;
1031             } else {
1032 0           carp $hash->{message};
1033 0           return undef;
1034             }
1035             }
1036             else {
1037 0           carp $response->status_line;
1038 0           return undef;
1039             }
1040             }
1041              
1042              
1043             #################### subroutine header begin ####################
1044              
1045             =head3 get_attribute_pool
1046              
1047             Usage : $ec->get_attribute_pool('padID')
1048             Purpose : Returns the attribute pool of a pad
1049             Returns : A hash reference, containing 3 keys
1050             * numToAttrib, containing a hash reference, which keys are integers and contents are array references
1051             * attribToNum, containing a hash reference, which keys are string and contents are integers
1052             * nextNum, which content is an integer
1053             Argument : Takes a pad ID (mandatory)
1054             See : http://etherpad.org/doc/v1.5.1/#index_getattributepool_padid
1055              
1056             =cut
1057              
1058             #################### subroutine header end ####################
1059              
1060              
1061             sub get_attribute_pool {
1062 0     0 1   my $self = shift;
1063 0           my $pad_id = shift;
1064              
1065 0 0         unless (defined($pad_id)) {
1066 0           carp 'Please provide a pad id';
1067 0           return undef;
1068             }
1069              
1070 0           my $api = '1.2.8';
1071 0           my $method = 'getAttributePool';
1072              
1073 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1074 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1075 0           my $response = $self->{ua}->get($request);
1076 0 0         if ($response->is_success) {
1077 0           my $hash = decode_json $response->decoded_content;
1078 0 0         if ($hash->{code} == 0) {
1079 0           return $hash->{data}->{pool};
1080             } else {
1081 0           carp $hash->{message};
1082 0           return undef;
1083             }
1084             }
1085             else {
1086 0           carp $response->status_line;
1087 0           return undef;
1088             }
1089             }
1090              
1091              
1092             #################### subroutine header begin ####################
1093              
1094             =head3 get_revision_changeset
1095              
1096             Usage : $ec->get_revision_changeset
1097             Purpose : Get the changeset at a given revision, or last revision if 'rev' is not defined
1098             Returns : A string, representing an etherpad changeset
1099             Argument : Takes a pad ID (mandatory) and optionally a revision number
1100             See : http://etherpad.org/doc/v1.5.1/#index_getrevisionchangeset_padid_rev
1101              
1102             =cut
1103              
1104             #################### subroutine header end ####################
1105              
1106              
1107             sub get_revision_changeset {
1108 0     0 1   my $self = shift;
1109 0           my $pad_id = shift;
1110 0           my $rev = shift;
1111              
1112 0 0         unless (defined($pad_id)) {
1113 0           carp 'Please provide at least a pad id';
1114 0           return undef;
1115             }
1116              
1117 0           my $api = '1.2.8';
1118 0           my $method = 'getRevisionChangeset';
1119              
1120 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1121 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1122 0 0         $request .= '&rev=' . $rev if (defined($rev));
1123 0           my $response = $self->{ua}->get($request);
1124 0 0         if ($response->is_success) {
1125 0           my $hash = decode_json $response->decoded_content;
1126 0 0         if ($hash->{code} == 0) {
1127 0           return $hash->{data};
1128             } else {
1129 0           carp $hash->{message};
1130 0           return undef;
1131             }
1132             }
1133             else {
1134 0           carp $response->status_line;
1135 0           return undef;
1136             }
1137             }
1138              
1139              
1140             #################### subroutine header begin ####################
1141              
1142             =head3 create_diff_html
1143              
1144             Usage : $ec->create_diff_html
1145             Purpose : Returns an object of diffs from 2 points in a pad
1146             Returns : A hash reference which keys are
1147             * html, which content is a string representing the diff between the two revisions
1148             * authors, which content is an array reference of authors
1149             Argument : Takes a pad ID, a revision number to start and a revision number to end. All arguments are mandatory
1150             See : http://etherpad.org/doc/v1.5.1/#index_creatediffhtml_padid_startrev_endrev
1151              
1152             =cut
1153              
1154             #################### subroutine header end ####################
1155              
1156              
1157             sub create_diff_html {
1158 0     0 1   my $self = shift;
1159 0           my $pad_id = shift;
1160 0           my $start_rev = shift;
1161 0           my $end_rev = shift;
1162              
1163 0 0         unless (defined($pad_id)) {
1164 0           carp 'Please provide a pad id, a start_rev and an end_rev';
1165 0           return undef;
1166             }
1167              
1168 0 0         unless (defined($start_rev)) {
1169 0           carp 'Please provide a start_rev and an end_rev';
1170 0           return undef;
1171             }
1172              
1173 0 0         unless (defined($end_rev)) {
1174 0           carp 'Please provide an end_rev';
1175 0           return undef;
1176             }
1177              
1178 0           my $api = '1.2.7';
1179 0           my $method = 'createDiffHTML';
1180              
1181 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1182 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1}) . '&startRev=' . $start_rev . '&endRev=' . $end_rev;
1183 0           my $response = $self->{ua}->get($request);
1184 0 0         if ($response->is_success) {
1185 0           my $hash = decode_json $response->decoded_content;
1186 0 0         if ($hash->{code} == 0) {
1187 0           return $hash->{data};
1188             } else {
1189 0           carp $hash->{message};
1190 0           return undef;
1191             }
1192             }
1193             else {
1194 0           carp $response->status_line;
1195 0           return undef;
1196             }
1197             }
1198              
1199              
1200             #################################################################
1201             =head2 Chat
1202              
1203             See http://etherpad.org/doc/v1.5.1/#index_chat
1204              
1205             =cut
1206             #################################################################
1207              
1208             #################### subroutine header begin ####################
1209              
1210             =head3 get_chat_history
1211              
1212             Usage : $ec->get_chat_history('padID' [, start, end])
1213             Purpose : Returns
1214             - a part of the chat history, when start and end are given
1215             - the whole chat history, when no extra parameters are given
1216             Returns : An array or an array reference, depending of the context, containing hash references with 4 keys :
1217             - text => text of the message
1218             - userId => epl user id
1219             - time => unix timestamp of the message
1220             - userName => epl user name
1221             Argument : Takes a pad ID (mandatory) and optionally the start and the end numbers of the messages you want.
1222             The start number can't be higher than or equal to the current chatHead. The first chat message is number 0.
1223             If you specify a start but not an end, all messages will be returned.
1224             See : http://etherpad.org/doc/v1.5.1/#index_getchathistory_padid_start_end
1225              
1226             =cut
1227              
1228             #################### subroutine header end ####################
1229              
1230              
1231             sub get_chat_history {
1232 0     0 1   my $self = shift;
1233 0           my $pad_id = shift;
1234 0           my $start = shift;
1235 0           my $end = shift;
1236              
1237 0 0         unless (defined($pad_id)) {
1238 0           carp 'Please provide at least a pad id';
1239 0           return undef;
1240             }
1241              
1242 0           my $api = '1.2.7';
1243 0           my $method = 'getChatHistory';
1244              
1245 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1246 0           $request .= '&padID=' . uri_encode($pad_id, { encode_reserved => 1 });
1247 0 0         $request .= '&start=' . $start if (defined($start));
1248 0 0         $request .= '&end=' . $end if (defined($end));
1249 0           my $response = $self->{ua}->get($request);
1250 0 0         if ($response->is_success) {
1251 0           my $hash = decode_json $response->decoded_content;
1252 0 0         if ($hash->{code} == 0) {
1253 0 0         return (wantarray) ? @{$hash->{data}->{messages}}: $hash->{data}->{messages};
  0            
1254             } else {
1255 0           carp $hash->{message};
1256 0           return undef;
1257             }
1258             }
1259             else {
1260 0           carp $response->status_line;
1261 0           return undef;
1262             }
1263             }
1264              
1265              
1266             #################### subroutine header begin ####################
1267              
1268             =head3 get_chat_head
1269              
1270             Usage : $ec->get_chat_head('padID')
1271             Purpose : Returns the chatHead (last number of the last chat-message) of the pad
1272             Returns : The last chat-message number. -1 if there is no chat message
1273             Argument : A pad ID
1274             See : http://etherpad.org/doc/v1.5.1/#index_getchathead_padid
1275              
1276             =cut
1277              
1278             #################### subroutine header end ####################
1279              
1280              
1281             sub get_chat_head {
1282 0     0 1   my $self = shift;
1283 0           my $pad_id = shift;
1284              
1285 0 0         unless (defined($pad_id)) {
1286 0           carp 'Please provide a pad id';
1287 0           return undef;
1288             }
1289              
1290 0           my $api = '1.2.7';
1291 0           my $method = 'getChatHead';
1292              
1293 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1294 0           $request .= '&padID=' . uri_encode($pad_id, { encode_reserved => 1 });
1295 0           my $response = $self->{ua}->get($request);
1296 0 0         if ($response->is_success) {
1297 0           my $hash = decode_json $response->decoded_content;
1298 0 0         if ($hash->{code} == 0) {
1299 0           return $hash->{data}->{chatHead};
1300             } else {
1301 0           carp $hash->{message};
1302 0           return undef;
1303             }
1304             }
1305             else {
1306 0           carp $response->status_line;
1307 0           return undef;
1308             }
1309             }
1310              
1311              
1312             #################################################################
1313             =head2 Pad
1314              
1315             Group pads are normal pads, but with the name schema GROUPID$PADNAME. A security manager controls access of them and its forbidden for normal pads to include a $ in the name.
1316              
1317             See http://etherpad.org/doc/v1.5.1/#index_pad
1318              
1319             =cut
1320             #################################################################
1321              
1322             #################### subroutine header begin ####################
1323              
1324             =head3 create_pad
1325              
1326             Usage : $ec->create_pad('padID' [, 'text'])
1327             Purpose : Creates a new (non-group) pad. Note that if you need to create a group Pad, you should call create_group_pad.
1328             Returns : 1 if it succeeds
1329             Argument : Takes a pad ID (mandatory) and optionally a text to fill the pad
1330             See : http://etherpad.org/doc/v1.5.1/#index_createpad_padid_text
1331              
1332             =cut
1333              
1334             #################### subroutine header end ####################
1335              
1336              
1337             sub create_pad {
1338 0     0 1   my $self = shift;
1339 0           my $pad_id = shift;
1340 0           my $text = shift;
1341              
1342 0 0         unless (defined($pad_id)) {
1343 0           carp 'Please provide at least a pad id';
1344 0           return undef;
1345             }
1346              
1347 0           my $api = '1';
1348 0           my $method = 'createPad';
1349              
1350 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1351 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1352 0 0         $request .= '&text=' . uri_encode($text, {encode_reserved => 1}) if (defined($text));
1353 0           my $response = $self->{ua}->get($request);
1354 0 0         if ($response->is_success) {
1355 0           my $hash = decode_json $response->decoded_content;
1356 0 0         if ($hash->{code} == 0) {
1357 0           return 1;
1358             } else {
1359 0           carp $hash->{message};
1360 0           return undef;
1361             }
1362             }
1363             else {
1364 0           carp $response->status_line;
1365 0           return undef;
1366             }
1367             }
1368              
1369              
1370             #################### subroutine header begin ####################
1371              
1372             =head3 get_revisions_count
1373              
1374             Usage : $ec->get_revisions_count('padID')
1375             Purpose : Returns the number of revisions of this pad
1376             Returns : The number of revisions
1377             Argument : A pad ID
1378             See : http://etherpad.org/doc/v1.5.1/#index_getrevisionscount_padid
1379              
1380             =cut
1381              
1382             #################### subroutine header end ####################
1383              
1384              
1385             sub get_revisions_count {
1386             my $self = shift;
1387             my $pad_id = shift;
1388              
1389             unless (defined($pad_id)) {
1390             carp 'Please provide a pad id';
1391             return undef;
1392             }
1393              
1394             my $api = '1';
1395             my $method = 'getRevisionsCount';
1396              
1397             my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1398             $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1399             my $response = $self->{ua}->get($request);
1400             if ($response->is_success) {
1401             my $hash = decode_json $response->decoded_content;
1402             if ($hash->{code} == 0) {
1403             return $hash->{data}->{revisions};
1404             } else {
1405             carp $hash->{message};
1406             return undef;
1407             }
1408             }
1409             else {
1410             carp $response->status_line;
1411             return undef;
1412             }
1413             }
1414              
1415              
1416             #################### subroutine header begin ####################
1417              
1418             =head3 get_saved_revisions_count
1419              
1420             Usage : $ec->get_saved_revisions_count('padID')
1421             Purpose : Returns the number of saved revisions of this pad
1422             Returns : The number of saved revisions
1423             Argument : A pad ID
1424             See : http://etherpad.org/doc/v1.5.6/#index_getsavedrevisionscount_padid
1425              
1426             =cut
1427              
1428             #################### subroutine header end ####################
1429              
1430              
1431             sub get_saved_revisions_count {
1432 0     0 1   my $self = shift;
1433 0           my $pad_id = shift;
1434              
1435 0 0         unless (defined($pad_id)) {
1436 0           carp 'Please provide a pad id';
1437 0           return undef;
1438             }
1439              
1440 0           my $api = '1.2.11';
1441 0           my $method = 'getSavedRevisionsCount';
1442              
1443 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1444 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1445 0           my $response = $self->{ua}->get($request);
1446 0 0         if ($response->is_success) {
1447 0           my $hash = decode_json $response->decoded_content;
1448 0 0         if ($hash->{code} == 0) {
1449 0           return $hash->{data}->{savedRevisions};
1450             } else {
1451 0           carp $hash->{message};
1452 0           return undef;
1453             }
1454             }
1455             else {
1456 0           carp $response->status_line;
1457 0           return undef;
1458             }
1459             }
1460              
1461              
1462             #################### subroutine header begin ####################
1463              
1464             =head3 list_saved_revisions
1465              
1466             Usage : $ec->list_saved_revisions('padID')
1467             Purpose : Returns the list of saved revisions of this pad
1468             Returns : An array or an array reference, depending of the context, containing the saved revisions numbers
1469             Argument : A pad ID
1470             See : http://etherpad.org/doc/v1.5.6/#index_listsavedrevisions_padid
1471              
1472             =cut
1473              
1474             #################### subroutine header end ####################
1475              
1476              
1477             sub list_saved_revisions {
1478 0     0 1   my $self = shift;
1479 0           my $pad_id = shift;
1480              
1481 0 0         unless (defined($pad_id)) {
1482 0           carp 'Please provide a pad id';
1483 0           return undef;
1484             }
1485              
1486 0           my $api = '1.2.11';
1487 0           my $method = 'listSavedRevisions';
1488              
1489 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1490 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1491 0           my $response = $self->{ua}->get($request);
1492 0 0         if ($response->is_success) {
1493 0           my $hash = decode_json $response->decoded_content;
1494 0 0         if ($hash->{code} == 0) {
1495 0 0         return (wantarray) ? @{$hash->{data}->{savedRevisions}} : $hash->{data}->{savedRevisions};
  0            
1496             } else {
1497 0           carp $hash->{message};
1498 0           return undef;
1499             }
1500             }
1501             else {
1502 0           carp $response->status_line;
1503 0           return undef;
1504             }
1505             }
1506              
1507              
1508             #################### subroutine header begin ####################
1509              
1510             =head3 save_revision
1511              
1512             Usage : $ec->save_revision('padID')
1513             Purpose : Saves a revision
1514             Returns : 1 if it succeeds
1515             Argument : Takes a pad ID (mandatory) and optionally a revision number
1516             See : http://etherpad.org/doc/v1.5.6/#index_saverevision_padid_rev
1517              
1518             =cut
1519              
1520             #################### subroutine header end ####################
1521              
1522              
1523             sub get_revisions_count {
1524 0     0 1   my $self = shift;
1525 0           my $pad_id = shift;
1526 0           my $rev = shift;
1527              
1528 0 0         unless (defined($pad_id)) {
1529 0           carp 'Please provide a pad id';
1530 0           return undef;
1531             }
1532              
1533 0           my $api = '1.2.11';
1534 0           my $method = 'saveRevision';
1535              
1536 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1537 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1538 0 0         $request .= '&rev=' . $rev if (defined($rev));
1539 0           my $response = $self->{ua}->get($request);
1540 0 0         if ($response->is_success) {
1541 0           my $hash = decode_json $response->decoded_content;
1542 0 0         if ($hash->{code} == 0) {
1543 0           return 1;
1544             } else {
1545 0           carp $hash->{message};
1546 0           return undef;
1547             }
1548             }
1549             else {
1550 0           carp $response->status_line;
1551 0           return undef;
1552             }
1553             }
1554              
1555              
1556             #################### subroutine header begin ####################
1557              
1558             =head3 get_users_count
1559              
1560             Usage : $ec->get_users_count('padID')
1561             Purpose : Returns the number of user that are currently editing this pad
1562             Returns : The number of users
1563             Argument : A pad ID
1564             See : http://etherpad.org/doc/v1.5.1/#index_paduserscount_padid
1565              
1566             =cut
1567              
1568             #################### subroutine header end ####################
1569              
1570              
1571             sub get_users_count {
1572 0     0 1   my $self = shift;
1573 0           my $pad_id = shift;
1574              
1575 0 0         unless (defined($pad_id)) {
1576 0           carp 'Please provide a pad id';
1577 0           return undef;
1578             }
1579              
1580 0           my $api = '1';
1581 0           my $method = 'padUsersCount';
1582 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1583 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1584 0           my $response = $self->{ua}->get($request);
1585 0 0         if ($response->is_success) {
1586 0           my $hash = decode_json $response->decoded_content;
1587 0 0         if ($hash->{code} == 0) {
1588 0           return $hash->{data}->{padUsersCount};
1589             } else {
1590 0           carp $hash->{message};
1591 0           return undef;
1592             }
1593             }
1594             else {
1595 0           carp $response->status_line;
1596 0           return undef;
1597             }
1598             }
1599              
1600              
1601             #################### subroutine header begin ####################
1602              
1603             =head3 pad_users
1604              
1605             Usage : $ec->pad_users('padID')
1606             Purpose : Returns the list of users that are currently editing this pad
1607             Returns : An array or an array reference, depending of the context, containing hash references with 3 keys : colorId, name and timestamp
1608             Argument : A pad ID
1609             See : http://etherpad.org/doc/v1.5.1/#index_padusers_padid
1610              
1611             =cut
1612              
1613             #################### subroutine header end ####################
1614              
1615              
1616             sub pad_users {
1617 0     0 1   my $self = shift;
1618 0           my $pad_id = shift;
1619              
1620 0 0         unless (defined($pad_id)) {
1621 0           carp 'Please provide a pad id';
1622 0           return undef;
1623             }
1624              
1625 0           my $api = '1.1';
1626 0           my $method = 'padUsers';
1627              
1628 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1629 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1630 0           my $response = $self->{ua}->get($request);
1631 0 0         if ($response->is_success) {
1632 0           my $hash = decode_json $response->decoded_content;
1633 0 0         if ($hash->{code} == 0) {
1634 0 0         return (wantarray) ? @{$hash->{data}->{padUsers}} : $hash->{data}->{padUsers};
  0            
1635             } else {
1636 0           carp $hash->{message};
1637 0           return undef;
1638             }
1639             }
1640             else {
1641 0           carp $response->status_line;
1642 0           return undef;
1643             }
1644             }
1645              
1646              
1647             #################### subroutine header begin ####################
1648              
1649             =head3 delete_pad
1650              
1651             Usage : $ec->delete_pad('padID')
1652             Purpose : Deletes a pad
1653             Returns : 1 if it succeeds
1654             Argument : A pad ID
1655             See : http://etherpad.org/doc/v1.5.1/#index_deletepad_padid
1656              
1657             =cut
1658              
1659             #################### subroutine header end ####################
1660              
1661              
1662             sub delete_pad {
1663 0     0 1   my $self = shift;
1664 0           my $pad_id = shift;
1665              
1666 0 0         unless (defined($pad_id)) {
1667 0           carp 'Please provide a pad id';
1668 0           return undef;
1669             }
1670              
1671 0           my $api = '1';
1672 0           my $method = 'deletePad';
1673              
1674 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1675 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1676 0           my $response = $self->{ua}->get($request);
1677 0 0         if ($response->is_success) {
1678 0           my $hash = decode_json $response->decoded_content;
1679 0 0         if ($hash->{code} == 0) {
1680 0           return 1;
1681             } else {
1682 0           carp $hash->{message};
1683 0           return undef;
1684             }
1685             }
1686             else {
1687 0           carp $response->status_line;
1688 0           return undef;
1689             }
1690             }
1691              
1692              
1693             #################### subroutine header begin ####################
1694              
1695             =head3 copy_pad
1696              
1697             Usage : $ec->copy_pad('sourceID', 'destinationID' [, 1])
1698             Purpose : Copies a pad with full history and chat. If force is true and the destination pad exists, it will be overwritten
1699             Returns : 1 if it succeeds
1700             Argument : A source pad ID
1701             A destination pad ID
1702             A force flag : a value which is true or false, in perl interpretation (for example; 0 and '' are false, 1, 2 and 'foo' are true)
1703             See : http://etherpad.org/doc/v1.5.1/#index_copypad_sourceid_destinationid_force_false
1704              
1705             =cut
1706              
1707             #################### subroutine header end ####################
1708              
1709              
1710             sub copy_pad {
1711 0     0 1   my $self = shift;
1712 0           my $source_id = shift;
1713 0           my $destination_id = shift;
1714 0           my $force = shift;
1715              
1716 0 0         unless (defined($source_id)) {
1717 0           carp 'Please provide a source pad id and a destination pad id';
1718 0           return undef;
1719             }
1720              
1721 0 0         unless (defined($destination_id)) {
1722 0           carp 'Please provide a destination pad id';
1723 0           return undef;
1724             }
1725              
1726 0 0         $force = ($force) ? 'true' : 'false' if (defined($force));
    0          
1727              
1728 0           my $api = '1.2.8';
1729 0           my $method = 'copyPad';
1730              
1731 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1732 0           $request .= '&sourceID=' . uri_encode($source_id, {encode_reserved => 1}) . '&destinationID=' . uri_encode($destination_id, {encode_reserved => 1});
1733 0 0         $request .= '&force=' . $force if (defined($force));
1734 0           my $response = $self->{ua}->get($request);
1735 0 0         if ($response->is_success) {
1736 0           my $hash = decode_json $response->decoded_content;
1737 0 0         if ($hash->{code} == 0) {
1738 0           return 1;
1739             } else {
1740 0           carp $hash->{message};
1741 0           return undef;
1742             }
1743             }
1744             else {
1745 0           carp $response->status_line;
1746 0           return undef;
1747             }
1748             }
1749              
1750              
1751             #################### subroutine header begin ####################
1752              
1753             =head3 move_pad
1754              
1755             Usage : $ec->move_pad('sourceID', 'destinationID' [, 1])
1756             Purpose : Moves a pad. If force is true and the destination pad exists, it will be overwritten
1757             Returns : 1 if it succeeds
1758             Argument : A source pad ID
1759             A destination pad ID
1760             A force flag : a value which is true or false, in perl interpretation (for example; 0 and '' are false, 1, 2 and 'foo' are true)
1761             See : http://etherpad.org/doc/v1.5.1/#index_movepad_sourceid_destinationid_force_false
1762              
1763             =cut
1764              
1765             #################### subroutine header end ####################
1766              
1767              
1768             sub move_pad {
1769 0     0 1   my $self = shift;
1770 0           my $source_id = shift;
1771 0           my $destination_id = shift;
1772 0           my $force = shift;
1773              
1774 0 0         unless (defined($source_id)) {
1775 0           carp 'Please provide a source pad id and a destination pad id';
1776 0           return undef;
1777             }
1778              
1779 0 0         unless (defined($destination_id)) {
1780 0           carp 'Please provide a destination pad id';
1781 0           return undef;
1782             }
1783              
1784 0 0         $force = ($force) ? 'true' : 'false' if (defined($force));
    0          
1785              
1786 0           my $api = '1.2.8';
1787 0           my $method = 'movePad';
1788              
1789 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1790 0           $request .= '&sourceID=' . uri_encode($source_id, {encode_reserved => 1}) . '&destinationID=' . uri_encode($destination_id, {encode_reserved => 1});
1791 0 0         $request .= '&force=' . $force if (defined($force));
1792 0           my $response = $self->{ua}->get($request);
1793 0 0         if ($response->is_success) {
1794 0           my $hash = decode_json $response->decoded_content;
1795 0 0         if ($hash->{code} == 0) {
1796 0           return 1;
1797             } else {
1798 0           carp $hash->{message};
1799 0           return undef;
1800             }
1801             }
1802             else {
1803 0           carp $response->status_line;
1804 0           return undef;
1805             }
1806             }
1807              
1808              
1809             #################### subroutine header begin ####################
1810              
1811             =head3 get_read_only_id
1812              
1813             Usage : $ec->get_read_only_id('padID')
1814             Purpose : Returns the read only link of a pad
1815             Returns : A string
1816             Argument : A pad ID
1817             See : http://etherpad.org/doc/v1.5.1/#index_getreadonlyid_padid
1818              
1819             =cut
1820              
1821             #################### subroutine header end ####################
1822              
1823              
1824             sub get_read_only_id {
1825 0     0 1   my $self = shift;
1826 0           my $pad_id = shift;
1827              
1828 0 0         unless (defined($pad_id)) {
1829 0           carp 'Please provide a pad id';
1830 0           return undef;
1831             }
1832              
1833 0           my $api = '1';
1834 0           my $method = 'getReadOnlyID';
1835              
1836 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1837 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1838 0           my $response = $self->{ua}->get($request);
1839 0 0         if ($response->is_success) {
1840 0           my $hash = decode_json $response->decoded_content;
1841 0 0         if ($hash->{code} == 0) {
1842 0           return $hash->{data}->{readOnlyID};
1843             } else {
1844 0           carp $hash->{message};
1845 0           return undef;
1846             }
1847             }
1848             else {
1849 0           carp $response->status_line;
1850 0           return undef;
1851             }
1852             }
1853              
1854              
1855             #################### subroutine header begin ####################
1856              
1857             =head3 get_pad_id
1858              
1859             Usage : $ec->get_pad_id('readOnlyID')
1860             Purpose : Returns the id of a pad which is assigned to the readOnlyID
1861             Returns : A string
1862             Argument : A read only ID
1863             See : http://etherpad.org/doc/v1.5.1/#index_getpadid_readonlyid
1864              
1865             =cut
1866              
1867             #################### subroutine header end ####################
1868              
1869              
1870             sub get_pad_id {
1871 0     0 1   my $self = shift;
1872 0           my $read_only_id = shift;
1873              
1874 0 0         unless (defined($read_only_id)) {
1875 0           carp 'Please provide a read only id';
1876 0           return undef;
1877             }
1878              
1879 0           my $api = '1.2.10';
1880 0           my $method = 'getPadID';
1881              
1882 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1883 0           $request .= '&padID=' . uri_encode($read_only_id, {encode_reserved => 1});
1884 0           my $response = $self->{ua}->get($request);
1885 0 0         if ($response->is_success) {
1886 0           my $hash = decode_json $response->decoded_content;
1887 0 0         if ($hash->{code} == 0) {
1888 0           return $hash->{data}->{padID};
1889             } else {
1890 0           carp $hash->{message};
1891 0           return undef;
1892             }
1893             }
1894             else {
1895 0           carp $response->status_line;
1896 0           return undef;
1897             }
1898             }
1899              
1900              
1901             #################### subroutine header begin ####################
1902              
1903             =head3 set_public_status
1904              
1905             Usage : $ec->set_public_status('padID', 'publicStatus')
1906             Purpose : Sets a boolean for the public status of a pad
1907             Returns : 1 if it succeeds
1908             Argument : A pad ID and the public status you want to set : 1 or 0
1909             See : http://etherpad.org/doc/v1.5.1/#index_setpublicstatus_padid_publicstatus
1910              
1911             =cut
1912              
1913             #################### subroutine header end ####################
1914              
1915              
1916             sub set_public_status {
1917 0     0 1   my $self = shift;
1918 0           my $pad_id = shift;
1919 0           my $public_status = shift;
1920              
1921 0 0         unless (defined($public_status)) {
1922 0           carp 'Please provide 2 arguments : a pad id and a public status (1 or 0)';
1923 0           return undef;
1924             }
1925              
1926 0 0         $public_status = ($public_status) ? JSON::XS::true : JSON::XS::false;
1927              
1928 0           my $api = '1';
1929 0           my $method = 'setPublicStatus';
1930              
1931 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1932 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1}) . '&publicStatus=' . $public_status;
1933 0           my $response = $self->{ua}->get($request);
1934 0 0         if ($response->is_success) {
1935 0           my $hash = decode_json $response->decoded_content;
1936 0 0         if ($hash->{code} == 0) {
1937 0           return 1;
1938             } else {
1939 0           carp $hash->{message};
1940 0           return undef;
1941             }
1942             }
1943             else {
1944 0           carp $response->status_line;
1945 0           return undef;
1946             }
1947             }
1948              
1949              
1950             #################### subroutine header begin ####################
1951              
1952             =head3 get_public_status
1953              
1954             Usage : $ec->get_public_status('padID')
1955             Purpose : Return true of false
1956             Returns : 1 or 0
1957             Argument : A pad ID
1958             See : http://etherpad.org/doc/v1.5.1/#index_getpublicstatus_padid
1959              
1960             =cut
1961              
1962             #################### subroutine header end ####################
1963              
1964              
1965             sub get_public_status {
1966 0     0 1   my $self = shift;
1967 0           my $pad_id = shift;
1968              
1969 0 0         unless (defined($pad_id)) {
1970 0           carp 'Please provide a pad id';
1971 0           return undef;
1972             }
1973              
1974 0           my $api = '1';
1975 0           my $method = 'getPublicStatus';
1976              
1977 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1978 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
1979 0           my $response = $self->{ua}->get($request);
1980 0 0         if ($response->is_success) {
1981 0           my $hash = decode_json $response->decoded_content;
1982 0 0         if ($hash->{code} == 0) {
1983 0 0         return ($hash->{data}->{publicStatus}) ? 1 : 0;
1984             } else {
1985 0           carp $hash->{message};
1986 0           return undef;
1987             }
1988             }
1989             else {
1990 0           carp $response->status_line;
1991 0           return undef;
1992             }
1993             }
1994              
1995              
1996             #################### subroutine header begin ####################
1997              
1998             =head3 set_password
1999              
2000             Usage : $ec->set_password('padID', 'password')
2001             Purpose : Returns ok or a error message
2002             Returns : 1 if it succeeds
2003             Argument : A pad ID and a password
2004             See : http://etherpad.org/doc/v1.5.1/#index_setpassword_padid_password
2005              
2006             =cut
2007              
2008             #################### subroutine header end ####################
2009              
2010              
2011             sub set_password {
2012 0     0 1   my $self = shift;
2013 0           my $pad_id = shift;
2014 0           my $password = shift;
2015              
2016 0 0         unless (defined($password)) {
2017 0           carp 'Please provide 2 arguments : a pad id and a password';
2018 0           return undef;
2019             }
2020              
2021 0           my $api = '1';
2022 0           my $method = 'setPassword';
2023              
2024 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2025 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1}) . '&password=' . uri_encode($password, {encode_reserved => 1});
2026 0           my $response = $self->{ua}->get($request);
2027 0 0         if ($response->is_success) {
2028 0           my $hash = decode_json $response->decoded_content;
2029 0 0         if ($hash->{code} == 0) {
2030 0           return 1;
2031             } else {
2032 0           carp $hash->{message};
2033 0           return undef;
2034             }
2035             }
2036             else {
2037 0           carp $response->status_line;
2038 0           return undef;
2039             }
2040             }
2041              
2042              
2043             #################### subroutine header begin ####################
2044              
2045             =head3 is_password_protected
2046              
2047             Usage : $ec->is_password_protected('padID')
2048             Purpose : Returns true or false
2049             Returns : 1 or 0
2050             Argument : A pad ID
2051             See : http://etherpad.org/doc/v1.5.1/#index_ispasswordprotected_padid
2052              
2053             =cut
2054              
2055             #################### subroutine header end ####################
2056              
2057              
2058             sub is_password_protected {
2059 0     0 1   my $self = shift;
2060 0           my $pad_id = shift;
2061              
2062 0 0         unless (defined($pad_id)) {
2063 0           carp 'Please provide a pad id';
2064 0           return undef;
2065             }
2066              
2067 0           my $api = '1';
2068 0           my $method = 'isPasswordProtected';
2069              
2070 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2071 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
2072 0           my $response = $self->{ua}->get($request);
2073 0 0         if ($response->is_success) {
2074 0           my $hash = decode_json $response->decoded_content;
2075 0 0         if ($hash->{code} == 0) {
2076 0 0         return ($hash->{data}->{passwordProtection}) ? 1 : 0;
2077             } else {
2078 0           carp $hash->{message};
2079 0           return undef;
2080             }
2081             }
2082             else {
2083 0           carp $response->status_line;
2084 0           return undef;
2085             }
2086             }
2087              
2088              
2089             #################### subroutine header begin ####################
2090              
2091             =head3 list_authors_of_pad
2092              
2093             Usage : $ec->list_authors_of_pad('padID')
2094             Purpose : Returns an array of authors who contributed to this pad
2095             Returns : An array or an array reference depending on the context, containing the epl authors IDs
2096             Argument : A pad ID
2097             See : http://etherpad.org/doc/v1.5.1/#index_listauthorsofpad_padid
2098              
2099             =cut
2100              
2101             #################### subroutine header end ####################
2102              
2103              
2104             sub list_authors_of_pad {
2105 0     0 1   my $self = shift;
2106 0           my $pad_id = shift;
2107              
2108 0 0         unless (defined($pad_id)) {
2109 0           carp 'Please provide a pad id';
2110 0           return undef;
2111             }
2112              
2113 0           my $api = '1';
2114 0           my $method = 'listAuthorsOfPad';
2115              
2116 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2117 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
2118 0           my $response = $self->{ua}->get($request);
2119 0 0         if ($response->is_success) {
2120 0           my $hash = decode_json $response->decoded_content;
2121 0 0         if ($hash->{code} == 0) {
2122 0 0         return (wantarray) ? @{$hash->{data}->{authorIDs}} : $hash->{data}->{authorIDs};
  0            
2123             } else {
2124 0           carp $hash->{message};
2125 0           return undef;
2126             }
2127             }
2128             else {
2129 0           carp $response->status_line;
2130 0           return undef;
2131             }
2132             }
2133              
2134              
2135             #################### subroutine header begin ####################
2136              
2137             =head3 list_names_of_authors_of_pad
2138              
2139             Usage : $ec->list_names_authors_of_pad('padID')
2140             Returns : Returns an array of the names of the authors who contributed to this pad in list context
2141             Returns an array reference in scalar context
2142             Argument : The pad ID
2143             See : This is not part of the Etherpad API but a facility offered by this module
2144              
2145             =cut
2146              
2147             #################### subroutine header end ####################
2148              
2149              
2150             sub list_names_of_authors_of_pad {
2151 0     0 1   my $self = shift;
2152 0           my $pad_id = shift;
2153              
2154 0 0         unless (defined($pad_id)) {
2155 0           carp 'Please provide a pad id';
2156 0           return undef;
2157             }
2158              
2159 0           my @names;
2160 0           my $anonymous = 0;
2161              
2162 0           my @authors = $self->list_authors_of_pad($pad_id);
2163 0           for my $author (@authors) {
2164 0           my $name = $self->get_author_name($author);
2165 0 0         if (defined($name)) {
2166 0           push @names, $name;
2167             } else {
2168 0           $anonymous++;
2169             }
2170             }
2171 0           @names = sort(@names);
2172 0 0         push @names, $anonymous . ' anonymous' if ($anonymous);
2173              
2174 0 0         return (wantarray) ? @names : \@names;
2175             }
2176              
2177              
2178             #################### subroutine header begin ####################
2179              
2180             =head3 get_last_edited
2181              
2182             Usage : $ec->get_last_edited('padID')
2183             Purpose : Returns the timestamp of the last revision of the pad
2184             Returns : A unix timestamp
2185             Argument : A pad ID
2186             See : http://etherpad.org/doc/v1.5.1/#index_getlastedited_padid
2187              
2188             =cut
2189              
2190             #################### subroutine header end ####################
2191              
2192              
2193             sub get_last_edited {
2194 0     0 1   my $self = shift;
2195 0           my $pad_id = shift;
2196              
2197 0 0         unless (defined($pad_id)) {
2198 0           carp 'Please provide at least a pad id';
2199 0           return undef;
2200             }
2201              
2202 0           my $api = '1';
2203 0           my $method = 'getLastEdited';
2204              
2205 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2206 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1});
2207 0           my $response = $self->{ua}->get($request);
2208 0 0         if ($response->is_success) {
2209 0           my $hash = decode_json $response->decoded_content;
2210 0 0         if ($hash->{code} == 0) {
2211 0           return $hash->{data}->{lastEdited};
2212             } else {
2213 0           carp $hash->{message};
2214 0           return undef;
2215             }
2216             }
2217             else {
2218 0           carp $response->status_line;
2219 0           return undef;
2220             }
2221             }
2222              
2223              
2224             #################### subroutine header begin ####################
2225              
2226             =head3 send_clients_message
2227              
2228             Usage : $ec->send_clients_message('padID', 'msg')
2229             Purpose : Sends a custom message of type msg to the pad
2230             Returns : 1 if it succeeds
2231             Argument : A pad ID and the message you want to send
2232             See : http://etherpad.org/doc/v1.5.1/#index_sendclientsmessage_padid_msg
2233              
2234             =cut
2235              
2236             #################### subroutine header end ####################
2237              
2238              
2239             sub send_clients_message {
2240 0     0 1   my $self = shift;
2241 0           my $pad_id = shift;
2242 0           my $msg = shift;
2243              
2244 0 0         unless (defined($msg)) {
2245 0           carp "Please provide 2 arguments : the pad id and a message";
2246 0           return undef;
2247             }
2248              
2249 0           my $api = '1.1';
2250 0           my $method = 'sendAPIsMessage';
2251              
2252 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2253 0           $request .= '&padID=' . uri_encode($pad_id, {encode_reserved => 1}) . '&msg=' . uri_encode($msg, {encode_reserved => 1});
2254 0           my $response = $self->{ua}->get($request);
2255 0 0         if ($response->is_success) {
2256 0           my $hash = decode_json $response->decoded_content;
2257 0 0         if ($hash->{code} == 0) {
2258 0           return 1;
2259             } else {
2260 0           carp $hash->{message};
2261 0           return undef;
2262             }
2263             }
2264             else {
2265 0           carp $response->status_line;
2266 0           return undef;
2267             }
2268             }
2269              
2270              
2271             #################### subroutine header begin ####################
2272              
2273             =head2 check_token
2274              
2275             Usage : $ec->check_token()
2276             Purpose : Returns ok when the current api token is valid
2277             Returns : 1 if the token is valid, 0 otherwise
2278             Argument : None
2279             See : http://etherpad.org/doc/v1.5.1/#index_checktoken
2280              
2281             =cut
2282              
2283             #################### subroutine header end ####################
2284              
2285              
2286             sub check_token {
2287 0     0 1   my $self = shift;
2288              
2289 0           my $api = '1.2';
2290 0           my $method = 'checkToken';
2291              
2292 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2293 0           my $response = $self->{ua}->get($request);
2294 0 0         if ($response->is_success) {
2295 0           my $hash = decode_json $response->decoded_content;
2296 0 0         if ($hash->{code} == 0) {
2297 0           return 1;
2298             } else {
2299 0           return 0;
2300             }
2301             }
2302             else {
2303 0           carp $response->status_line;
2304 0           return undef;
2305             }
2306             }
2307              
2308              
2309             #################################################################
2310             =head2 Pads
2311              
2312             See http://etherpad.org/doc/v1.5.1/#index_pads
2313              
2314             =cut
2315             #################################################################
2316              
2317             #################### subroutine header begin ####################
2318              
2319             =head3 list_all_pads
2320              
2321             Usage : $ec->list_all_pads()
2322             Purpose : Lists all pads on this epl instance
2323             Returns : An array or an array reference depending on the context, containing the pads names
2324             See : http://etherpad.org/doc/v1.5.1/#index_listallpads
2325              
2326             =cut
2327              
2328             #################### subroutine header end ####################
2329              
2330              
2331             sub list_all_pads {
2332 0     0 1   my $self = shift;
2333              
2334 0           my $api = '1.2.1';
2335 0           my $method = 'listAllPads';
2336              
2337 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2338 0           my $response = $self->{ua}->get($request);
2339 0 0         if ($response->is_success) {
2340 0           my $hash = decode_json $response->decoded_content;
2341 0 0         if ($hash->{code} == 0) {
2342             # Change in the API, without documentation
2343 0 0         my $data = (ref($hash->{data}) eq 'HASH') ? $hash->{data}->{padIDs} : $hash->{data};
2344 0 0         return (wantarray) ? @{$data} : $data;
  0            
2345             } else {
2346 0           carp $hash->{message};
2347 0           return undef;
2348             }
2349             }
2350             else {
2351 0           carp $response->status_line;
2352 0           return undef;
2353             }
2354             }
2355              
2356              
2357             #################### footer pod documentation begin ###################
2358             =head1 INSTALL
2359              
2360             perl Makefile.PL
2361             make
2362             make test
2363             make install
2364              
2365             If you are on a windows box you should use 'nmake' rather than 'make'.
2366              
2367             =head1 BUGS and SUPPORT
2368              
2369             You can find documentation for this module with the perldoc command.
2370              
2371             perldoc Etherpad::API
2372              
2373             Bugs and feature requests will be tracked at RT:
2374              
2375             http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Etherpad-API
2376             bug-etherpad-api at rt.cpan.org
2377              
2378             The latest source code can be browsed and fetched at:
2379              
2380             https://github.com/ldidry/etherpad-api
2381             git clone git://github.com/ldidry/etherpad-api.git
2382              
2383             You can also look for information at:
2384              
2385             RT: CPAN's request tracker
2386              
2387             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Etherpad-API
2388             AnnoCPAN: Annotated CPAN documentation
2389              
2390             http://annocpan.org/dist/Etherpad-API
2391             CPAN Ratings
2392              
2393             http://cpanratings.perl.org/d/Etherpad-API
2394             Search CPAN
2395              
2396             http://search.cpan.org/dist/Etherpad-API
2397              
2398              
2399             =head1 AUTHOR
2400              
2401             Luc DIDRY
2402             CPAN ID: LDIDRY
2403             ldidry@cpan.org
2404             https://fiat-tux.fr/
2405              
2406             =head1 COPYRIGHT
2407              
2408             This program is free software; you can redistribute
2409             it and/or modify it under the same terms as Perl itself.
2410              
2411             The full text of the license can be found in the
2412             LICENSE file included with this module.
2413              
2414              
2415             =head1 SEE ALSO
2416              
2417             perl(1).
2418              
2419             =cut
2420              
2421             #################### footer pod documentation end ###################
2422              
2423              
2424             1;
2425