File Coverage

blib/lib/WebService/JotForm.pm
Criterion Covered Total %
statement 21 147 14.2
branch 0 40 0.0
condition 0 26 0.0
subroutine 7 42 16.6
pod 31 32 96.8
total 59 287 20.5


line stmt bran cond sub pod time code
1             package WebService::JotForm;
2              
3 2     2   33699 use strict;
  2         3  
  2         74  
4 2     2   8 use warnings FATAL => 'all';
  2         1  
  2         69  
5 2     2   1049 use Moo;
  2         23367  
  2         12  
6 2     2   3728 use JSON::Any;
  2         24319  
  2         11  
7 2     2   14247 use LWP::UserAgent;
  2         75859  
  2         85  
8 2     2   16 use URI::Escape qw(uri_escape);
  2         3  
  2         129  
9 2     2   8 use Carp qw(croak);
  2         3  
  2         3376  
10              
11             =head1 NAME
12              
13             WebService::JotForm - Perl interface to JotForm's API -- currently only the read operations are fully supported.
14              
15             Support for create, update, and delete operations are beginning to be added in this and future releases.
16              
17             =head1 VERSION
18              
19             Version 0.018
20              
21             =head1 SYNOPSIS
22            
23             my $jotform = WebService::JotForm->new( apiKey => $apiKey);
24              
25             my $forms = $jotform->get_user_forms();
26              
27             # Show form details associated with our account
28              
29             foreach my $form (@{$forms->{content}}) {
30             print "Form $form->{id} - $form->{title} - $form->{url} - $form->{last_submission}\n";
31             }
32              
33             my $form_id = "42";
34              
35             my $submissions = $jotform->get_form_submissions($form_id);
36              
37             # Loop through all submissions to our form and print out submission created_at and ip
38              
39             foreach my $sub(@{$submissions->{content}}) {
40             print "$sub->{created_at} $sub->{ip}\n";
41             }
42              
43             =head1 DESCRIPTION
44              
45             This is a thin wrapper around the JotForm API. All results are what's returned by the JotForm API,
46             with the JSON being converted into Perl data structures.
47              
48             You need a JotForm API key to use this module. The easiest way to get an apiKey is just to
49             login to L and then go to L.
50             From there create a token (or use an existing one). You can set whether it's a read-only or full-access token.
51              
52             More information on tokens is available in the L
53              
54             =cut
55              
56             our $VERSION = '0.018';
57              
58             has 'apiKey' => ( is => 'ro', required => 1);
59             has 'apiBase' => ( is => 'ro', default => 'https://api.jotform.com');
60             has 'apiVersion' => ( is => 'ro', default => 'v1');
61             has 'agent' => ( is => 'rw'); # Must act like LWP::UserAgent
62              
63             my $json = JSON::Any->new;
64              
65             sub BUILD {
66 0     0 0   my ($self) = @_;
67            
68 0 0         if(not $self->agent) {
69 0           $self->agent(LWP::UserAgent->new(agent => "perl/$], WebService::JotForm/" . $self->VERSION));
70             }
71              
72 0           my $resp = $self->agent->get($self->apiBase . "/" . $self->apiVersion . "/user?apiKey=" . $self->apiKey);
73              
74 0           return;
75             }
76              
77             =head1 METHODS
78              
79             =head2 new(%params)
80              
81             Create a new WebService::JotForm object with hash parameter
82              
83             my $jotform = WebService::JotForm->new(
84             apiKey => '1234567890abcdef'
85             );
86              
87             Accepts the following parameters:
88              
89             =over 4
90              
91             =item * apiKey
92              
93             Required parameter. JotForm apiKey
94              
95             =item * apiBase
96              
97             Optional parameter - defaults to: 'https://api.jotform.com'
98              
99             =item * apiVersion
100              
101             Optional parameter - defaults to 'v1'
102              
103             =item * agent
104              
105             Agent that acts like LWP::UserAgent used for making requests -- module defaults to creating its own if none is provide
106              
107             =back
108              
109             =cut
110              
111             =head2 get_user()
112              
113             $jotform->get_user();
114              
115             Get user account details for this JotForm user. Including user account type, avatar URL, name, email, website URL and account limits.
116              
117             my $user = $jotform->get_user();
118              
119             =cut
120              
121              
122             sub get_user {
123 0     0 1   my $self = shift;
124 0           return $self->_get("user");
125             }
126              
127             =head2 get_user_usage()
128              
129             $jotform->get_user_usage();
130              
131             Get number of form submissions received this month. Also, get number of SSL form submissions, payment form submissions and upload space used by user.
132              
133             =cut
134              
135             sub get_user_usage {
136 0     0 1   my $self = shift;
137 0           return $self->_get("user/usage");
138             }
139              
140              
141             =head2 get_user_submissions($params)
142              
143             $jotform->get_user_submissions($params);
144              
145             Get a list of all submissions for all forms on this account. The answers array has the submission data. Created_at is the date of the submission.
146             Optional paramaters
147              
148             =over 4
149              
150             =item offset
151              
152             Example: 20
153              
154             =item limit
155              
156             Example: 20
157              
158             =item filter
159              
160             Example: {"new":"1"} or {"created_at:gt":"2013-01-01 00:00:00"}
161              
162             =item orderby
163              
164             Example: created_at
165              
166             =back
167              
168             =cut
169              
170             sub get_user_submissions {
171 0     0 1   my ($self, $params) = @_;
172 0   0       $params ||= {};
173 0           return $self->_get("user/submissions", $params);
174             }
175              
176             =head2 get_user_subusers()
177              
178             $jotform->get_user_subusers();
179              
180             Get a list of sub users for this accounts and list of forms and form folders with access privileges.
181             =cut
182              
183             sub get_user_subusers {
184 0     0 1   my $self = shift;
185 0           return $self->_get("user/subusers");
186             }
187              
188             =head2 get_user_folders()
189              
190             $jotform->get_user_folders();
191              
192             Get a list of form folders for this account. Returns name of the folder and owner of the folder for shared folders.
193              
194             =cut
195              
196             sub get_user_folders {
197 0     0 1   my $self = shift;
198 0           return $self->_get("user/folders");
199             }
200              
201             =head2 get_user_reports()
202            
203             $jotform->get_user_reports();
204              
205             List of URLS for reports in this account. Includes reports for all of the forms. ie. Excel, CSV, printable charts, embeddable HTML tables.
206              
207             =cut
208             sub get_user_reports {
209 0     0 1   my $self = shift;
210 0           return $self->_get("user/reports");
211             }
212              
213             =head2 register_user()
214              
215             $jotform->register_user($params);
216              
217             $jotform->register_user({ username => $username, password => $pw, email => $email });
218              
219             Register a new JotForm account with username, password and email
220              
221             =cut
222              
223             sub register_user {
224 0     0 1   my $self = shift;
225 0           my $params = shift;
226 0   0       $params ||= {};
227 0           return $self->_post("user/register", $params);
228             }
229              
230             =head2 login_user()
231              
232             $jotform->register_user($params);
233              
234             $jotform->register_user({ username => $username, password => $pw });
235              
236             Login user with given credentials - accepts username, password, and optionally appName, and access
237              
238             =cut
239              
240              
241              
242             sub login_user {
243 0     0 1   my $self = shift;
244 0           my $params = shift;
245 0   0       $params ||= {};
246 0           return $self->_post("user/login", $params);
247             }
248              
249             =head2 get_user_logout()
250              
251             $jotform->get_user_logout();
252              
253             Logout user
254              
255             =cut
256             sub get_user_logout {
257 0     0 1   my $self = shift;
258 0           return $self->_get("user/logout");
259             }
260              
261             =head2 get_user_settings()
262              
263             $jotform->get_user_settings();
264              
265             Get user's time zone and language.
266              
267             =cut
268             sub get_user_settings {
269 0     0 1   my $self = shift;
270 0           return $self->_get("user/settings");
271             }
272              
273             =head2 update_user_settings()
274              
275             $jotform->update_user_settings($params);
276            
277             $jotform->update_user_settings({ email => $updated_email });
278              
279             Update user's settings like time zone and language. Optional fields: name, email, website, time_zone, company, securityQuestion, securityAnswer, industry
280              
281             =cut
282             sub update_user_settings {
283 0     0 1   my $self = shift;
284 0           my $params = shift;
285 0   0       $params ||= {};
286            
287 0           return $self->_post("user/settings", $params);
288             }
289              
290              
291              
292              
293              
294              
295              
296              
297             =head2 get_user_history()
298              
299             $jotform->get_user_history();
300              
301             User activity log about things like forms created/modified/deleted, account logins and other operations.
302              
303              
304             =cut
305              
306             sub get_user_history {
307 0     0 1   my ($self, $params) = @_;
308 0           return $self->_get("user/history", $params);
309             }
310              
311             =head2 get_user_forms($params)
312              
313             $jotform->get_user_forms($params);
314              
315              
316             Get a list of forms for this account. Includes basic details such as title of the form, when it was created, number of new and total submissions.
317              
318             TODO -- document additionsal optional params
319              
320             =cut
321              
322             sub get_user_forms {
323 0     0 1   my ($self, $params) = @_;
324 0           return $self->_get("user/forms", $params);
325             }
326              
327             =head2 create_forms($params);
328              
329             $jotform->create_forms($params);
330              
331             Add new forms with questions, properties and email settings.
332              
333              
334             =cut
335              
336             sub create_forms {
337 0     0 1   my $self = shift;
338 0           my $params = shift;
339 0   0       $params ||= {};
340              
341 0           return $self->_post("user/forms", $params);
342             }
343              
344             =head2 create_form($params);
345              
346             $jotform->create_form($params);
347              
348             Add new form with questions, properties and email settings.
349              
350              
351             =cut
352              
353             sub create_form {
354 0     0 1   my $self = shift;
355 0           my $params = shift;
356 0   0       $params ||= {};
357              
358 0           return $self->_post("/form", $params);
359             }
360              
361              
362             =head2 get_form($id)
363              
364             $jotform->get_form($id);
365              
366             Get basic information about a form. Use get_form_questions($id) to get the list of questions.
367              
368             =cut
369              
370             sub get_form {
371 0     0 1   my ($self, $form_id) = @_;
372 0 0         croak "No form id provided to get_form" if !$form_id;
373 0           return $self->_get("form/$form_id");
374             }
375              
376             =head2 clone_form($id)
377            
378             $jotform->clone_form($id);
379              
380             Clone a given form
381             =cut
382              
383             sub clone_form {
384 0     0 1   my ($self, $form_id) = @_;
385 0 0         croak "No form id provided to clone_form" if !$form_id;
386 0           return $self->_post("/form/$form_id/clone");
387             }
388              
389             =head2 create_form_question($id, $question)
390              
391             $jotform->create_form_question($id, $question)
392              
393             Add a new question to a form, takes an id for the form as a parameter, and then a hasref of key/values for the question fields
394              
395             =cut
396              
397             sub create_form_question {
398 0     0 1   my ($self, $form_id, $question) = @_;
399            
400 0 0         croak "No form id provided to create_form_question" if !$form_id;
401            
402 0   0       $question ||= {};
403 0           my $params = {};
404              
405 0           foreach (keys %$question) {
406 0           $params->{"question[$_]"} = $question->{$_};
407             }
408              
409 0           return $self->_post("/form/$form_id/questions", $params);
410             }
411              
412              
413             =head2 edit_form_question($form_id, $qid, $question);
414              
415             $jotform->edit_form_question($form_id, $qid, $question);
416              
417             Edit a question property or add a new one. Form questions might have various properties. Examples: Is it required? Are there any validations such as 'numeric only'?
418              
419             =cut
420              
421             sub edit_form_question {
422 0     0 1   my ($self, $form_id, $qid, $question) = @_;
423 0 0 0       croak "edit_form_question requires both a form_id and question id" if !$form_id && $qid;
424              
425 0   0       $question ||= {};
426 0           my $params = {};
427            
428 0           foreach (keys %$question) {
429 0           $params->{"question[$_]"} = $question->{$_};
430             }
431            
432 0           return $self->_post("form/$form_id/question/$qid", $params);
433             }
434              
435             =head2 set_form_properties($form_id, $params)
436              
437             $jotform->set_form_properties($form_id, $params);
438            
439             $jotform->set_form_properties($form_id, { formWidth => 555 });
440              
441             Add or edit properties of a specific form
442              
443             =cut
444             sub set_form_properties {
445 0     0 1   my($self, $form_id, $params) = @_;
446 0 0         croak "set_form_properties requires a form_id" if !$form_id;
447 0   0       $params ||= {};
448              
449 0           my $props = {};
450              
451 0           foreach(keys %$params) {
452 0           $props->{"properties[$_]"} = $params->{$_};
453             }
454 0           return $self->_post("/form/$form_id/properties", $props);
455             }
456              
457             =head2 get_form_questions($id)
458            
459             $jotform->get_form_questions($id);
460              
461             Get a list of all questions on a form. Type describes question field type. Order is the question order in the form. Text field is the question label.
462              
463             =cut
464              
465             sub get_form_questions {
466 0     0 1   my ($self, $form_id) = @_;
467 0 0         croak "No form id provided to get_form_questions" if !$form_id;
468 0           return $self->_get("form/$form_id/questions");
469             }
470              
471             =head2 get_form_question($form_id, $qid)
472            
473             $jotform->get_form_question($form_id, $qid);
474              
475             Get Details About a Question
476              
477             =cut
478              
479             sub get_form_question {
480 0     0 1   my ($self, $form_id, $qid) = @_;
481 0 0 0       croak "Get_form_question requires both a form_id and question id" if !$form_id && $qid;
482 0           return $self->_get("form/$form_id/question/$qid");
483             }
484              
485             =head2 get_form_properties($id,$key)
486              
487             $jotform->get_form_properties($id);
488            
489             $jotform->get_form_properties($id,$key);
490              
491             Get a list of all properties on a form.
492              
493             =cut
494              
495             sub get_form_properties {
496 0     0 1   my ($self, $form_id, $key) = @_;
497 0 0         croak "Get_form_properties requires a form_id" if !$form_id;
498            
499 0 0         if($key) {
500 0           return $self->_get("form/$form_id/properties/$key");
501             } else {
502 0           return $self->_get("form/$form_id/properties");
503             }
504             }
505              
506             =head2 get_form_reports($id)
507              
508             $jotform->getFormReports($id);
509              
510             Get all the reports of a specific form.
511              
512             =cut
513              
514             sub get_form_reports {
515 0     0 1   my ($self, $form_id) = @_;
516 0 0         croak "No form id provided to get_form_reports" if !$form_id;
517 0           return $self->_get("form/$form_id/reports");
518             }
519              
520             =head2 get_form_files($id)
521              
522             $jotform->get_form_files($id);
523              
524             List of files uploaded on a form. Here is how you can access a particular file: http://www.jotform.com/uploads/{username}/{form-id}/{submission-id}/{file-name}. Size and file type is also included.
525              
526             =cut
527              
528              
529             sub get_form_files {
530 0     0 1   my ($self, $form_id) = @_;
531 0 0         croak "No form id provided to get_form_files" if !$form_id;
532 0           return $self->_get("form/$form_id/files");
533             }
534              
535             =head2 get_form_webhooks($id)
536              
537             $jotform->get_form_webhooks($id)
538              
539             Webhooks can be used to send form submission data as an instant notification. Returns list of webhooks for this form.
540              
541             =cut
542              
543              
544             sub get_form_webhooks {
545 0     0 1   my ($self, $form_id) = @_;
546 0 0         croak "No form id provided to get_form_webhooks" if !$form_id;
547 0           return $self->_get("form/$form_id/webhooks");
548             }
549              
550             =head2 get_form_submissions($id)
551              
552             $jotform->get_form_submissions($id, $params);
553              
554             List of form reponses. Fields array has the submitted data. Created_at is the date of the submission.
555              
556             =cut
557              
558              
559             sub get_form_submissions {
560 0     0 1   my ($self, $form_id, $params) = @_;
561 0 0         croak "No form id provided to get_form_submissions" if !$form_id;
562 0           return $self->_get("form/$form_id/submissions");
563             }
564              
565             =head2 get_submission($id)
566              
567             $jotform->get_submission($id);
568              
569             Similar to get_form_submissions($id) But only get a single submission, based on submission id
570              
571             =cut
572              
573              
574             sub get_submission {
575 0     0 1   my ($self, $sub_id) = @_;
576 0 0         croak "No submission id provided to get_submission" if !$sub_id;
577 0           return $self->_get("submission/$sub_id");
578             }
579              
580             =head2 get_report($id)
581              
582             $jotform->get_report($id);
583              
584             Get more information about a data report.
585             =cut
586              
587              
588             sub get_report {
589 0     0 1   my ($self, $rep_id) = @_;
590 0 0         croak "No report id provided to get_report" if !$rep_id;
591 0           return $self->_get("report/$rep_id");
592             }
593              
594             =head2 get_folder($id)
595              
596             $jotform->get_folder($id)
597              
598             Get a list of forms in a folder, and other details about the form such as folder color.
599             =cut
600              
601              
602             sub get_folder {
603 0     0 1   my ($self, $fol_id) = @_;
604 0 0         croak "No folder id provided to get_folder" if !$fol_id;
605 0           return $self->_get("folder/$fol_id");
606             }
607              
608             =head2 get_system_plan($plan_name)
609              
610             $jotform->get_system_plan($plan_name)
611              
612             Get limit and prices of a plan.
613             =cut
614              
615              
616             sub get_system_plan {
617 0     0 1   my ($self, $plan_name) = @_;
618 0 0         croak "No plan name provided to get_system_plan" if !$plan_name;
619 0           return $self->_get("system/plan/$plan_name");
620              
621             }
622              
623              
624             sub _get {
625 0     0     my ($self, $path, $params) = @_;
626 0           my $url = $self->_gen_request_url($path, $params);
627 0           my $resp = $self->agent->get($url);
628              
629 0 0         unless ($resp->is_success) {
630 0           croak "Failed to fetch $url - ".$resp->status_line;
631             }
632 0           return $json->decode($resp->content);
633             }
634              
635             sub _post {
636 0     0     my ($self, $path, $params) = @_;
637 0   0       $params ||= {};
638 0           my $url = join("/", $self->apiBase, $self->apiVersion, $path) . "?apiKey=" .$self->apiKey;
639 0           my $resp = $self->agent->post($url, $params);
640 0 0         unless ($resp->is_success) {
641 0           croak "Failed to fetch $url - ".$resp->status_line;
642             }
643 0           return $json->decode($resp->content);
644             }
645              
646             sub _gen_request_url {
647 0     0     my ($self, $path, $params) = @_;
648 0           my $url = join("/", $self->apiBase, $self->apiVersion, $path) . "?apiKey=" .$self->apiKey;
649 0           foreach my $param (keys %$params) {
650 0           $url .= "&".uri_escape($param) ."=". uri_escape($params->{$param});
651             }
652 0           return $url;
653             }
654              
655              
656             =head1 AUTHOR
657              
658             Tim Vroom, C<< >>
659              
660             =head1 BUGS
661              
662             Please report any bugs or feature requests to C, or through
663             the web interface at L. I will be notified, and then you'll
664             automatically be notified of progress on your bug as I make changes.
665              
666              
667              
668              
669             =head1 SUPPORT
670              
671             You can find documentation for this module with the perldoc command.
672              
673             perldoc WebService::JotForm
674              
675              
676             You can also look for information at:
677              
678             =over 4
679              
680             =item * RT: CPAN's request tracker (report bugs here)
681              
682             L
683              
684             =item * AnnoCPAN: Annotated CPAN documentation
685              
686             L
687              
688             =item * CPAN Ratings
689              
690             L
691              
692             =item * Search CPAN
693              
694             L
695              
696             =back
697              
698              
699             =head1 ACKNOWLEDGEMENTS
700              
701              
702             =head1 LICENSE AND COPYRIGHT
703              
704             Copyright 2014 Tim Vroom.
705              
706             This program is free software; you can redistribute it and/or modify it
707             under the terms of the the Artistic License (2.0). You may obtain a
708             copy of the full license at:
709              
710             L
711              
712             Any use, modification, and distribution of the Standard or Modified
713             Versions is governed by this Artistic License. By using, modifying or
714             distributing the Package, you accept this license. Do not use, modify,
715             or distribute the Package, if you do not accept this license.
716              
717             If your Modified Version has been derived from a Modified Version made
718             by someone other than you, you are nevertheless required to ensure that
719             your Modified Version complies with the requirements of this license.
720              
721             This license does not grant you the right to use any trademark, service
722             mark, tradename, or logo of the Copyright Holder.
723              
724             This license includes the non-exclusive, worldwide, free-of-charge
725             patent license to make, have made, use, offer to sell, sell, import and
726             otherwise transfer the Package with respect to any patent claims
727             licensable by the Copyright Holder that are necessarily infringed by the
728             Package. If you institute patent litigation (including a cross-claim or
729             counterclaim) against any party alleging that the Package constitutes
730             direct or contributory patent infringement, then this Artistic License
731             to you shall terminate on the date that such litigation is filed.
732              
733             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
734             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
735             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
736             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
737             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
738             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
739             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
740             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
741              
742              
743             =cut
744              
745             1; # End of WebService::JotForm