File Coverage

blib/lib/WebService/JotForm.pm
Criterion Covered Total %
statement 21 152 13.8
branch 0 44 0.0
condition 0 31 0.0
subroutine 7 43 16.2
pod 32 33 96.9
total 60 303 19.8


line stmt bran cond sub pod time code
1             package WebService::JotForm;
2              
3 2     2   32108 use strict;
  2         3  
  2         77  
4 2     2   10 use warnings FATAL => 'all';
  2         3  
  2         88  
5 2     2   1035 use Moo;
  2         23344  
  2         12  
6 2     2   3677 use JSON::Any;
  2         25472  
  2         9  
7 2     2   12404 use LWP::UserAgent;
  2         73966  
  2         65  
8 2     2   14 use URI::Escape qw(uri_escape);
  2         2  
  2         110  
9 2     2   8 use Carp qw(croak);
  2         2  
  2         3368  
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.019
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.019';
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 create_form_report($form_id, { title => $title, list_type => $list_type });
521              
522             $jotform->create_form_report($form_id, { title => $title, list_type => "csv" });
523             $jotform->create_form_report($form_id, { title => $title, list_type => "csv", "fields=ip,dt,1" });
524              
525             Create new report of a form with intended fields, type and title.
526              
527             =cut
528              
529             sub create_form_report{
530 0     0 1   my($self, $form_id, $params) = @_;
531 0   0       $params ||= {};
532 0 0         croak "No form id provided to create_form_report" if !$form_id;
533 0 0 0       croak "title and list_type required parameters to create_form_report" if !$params->{title} || !$params->{list_type};
534              
535 0           return $self->_post("form/$form_id/reports", $params);
536             }
537              
538             =head2 get_form_files($id)
539              
540             $jotform->get_form_files($id);
541              
542             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.
543              
544             =cut
545              
546              
547             sub get_form_files {
548 0     0 1   my ($self, $form_id) = @_;
549 0 0         croak "No form id provided to get_form_files" if !$form_id;
550 0           return $self->_get("form/$form_id/files");
551             }
552              
553             =head2 get_form_webhooks($id)
554              
555             $jotform->get_form_webhooks($id)
556              
557             Webhooks can be used to send form submission data as an instant notification. Returns list of webhooks for this form.
558              
559             =cut
560              
561              
562             sub get_form_webhooks {
563 0     0 1   my ($self, $form_id) = @_;
564 0 0         croak "No form id provided to get_form_webhooks" if !$form_id;
565 0           return $self->_get("form/$form_id/webhooks");
566             }
567              
568             =head2 get_form_submissions($id)
569              
570             $jotform->get_form_submissions($id, $params);
571              
572             List of form reponses. Fields array has the submitted data. Created_at is the date of the submission.
573              
574             =cut
575              
576              
577             sub get_form_submissions {
578 0     0 1   my ($self, $form_id, $params) = @_;
579 0 0         croak "No form id provided to get_form_submissions" if !$form_id;
580 0           return $self->_get("form/$form_id/submissions");
581             }
582              
583             =head2 get_submission($id)
584              
585             $jotform->get_submission($id);
586              
587             Similar to get_form_submissions($id) But only get a single submission, based on submission id
588              
589             =cut
590              
591              
592             sub get_submission {
593 0     0 1   my ($self, $sub_id) = @_;
594 0 0         croak "No submission id provided to get_submission" if !$sub_id;
595 0           return $self->_get("submission/$sub_id");
596             }
597              
598             =head2 get_report($id)
599              
600             $jotform->get_report($id);
601              
602             Get more information about a data report.
603             =cut
604              
605              
606             sub get_report {
607 0     0 1   my ($self, $rep_id) = @_;
608 0 0         croak "No report id provided to get_report" if !$rep_id;
609 0           return $self->_get("report/$rep_id");
610             }
611              
612             =head2 get_folder($id)
613              
614             $jotform->get_folder($id)
615              
616             Get a list of forms in a folder, and other details about the form such as folder color.
617             =cut
618              
619              
620             sub get_folder {
621 0     0 1   my ($self, $fol_id) = @_;
622 0 0         croak "No folder id provided to get_folder" if !$fol_id;
623 0           return $self->_get("folder/$fol_id");
624             }
625              
626             =head2 get_system_plan($plan_name)
627              
628             $jotform->get_system_plan($plan_name)
629              
630             Get limit and prices of a plan.
631             =cut
632              
633              
634             sub get_system_plan {
635 0     0 1   my ($self, $plan_name) = @_;
636 0 0         croak "No plan name provided to get_system_plan" if !$plan_name;
637 0           return $self->_get("system/plan/$plan_name");
638              
639             }
640              
641              
642             sub _get {
643 0     0     my ($self, $path, $params) = @_;
644 0           my $url = $self->_gen_request_url($path, $params);
645 0           my $resp = $self->agent->get($url);
646              
647 0 0         unless ($resp->is_success) {
648 0           croak "Failed to fetch $url - ".$resp->status_line;
649             }
650 0           return $json->decode($resp->content);
651             }
652              
653             sub _post {
654 0     0     my ($self, $path, $params) = @_;
655 0   0       $params ||= {};
656 0           my $url = join("/", $self->apiBase, $self->apiVersion, $path) . "?apiKey=" .$self->apiKey;
657 0           my $resp = $self->agent->post($url, $params);
658 0 0         unless ($resp->is_success) {
659 0           croak "Failed to fetch $url - ".$resp->status_line;
660             }
661 0           return $json->decode($resp->content);
662             }
663              
664             sub _gen_request_url {
665 0     0     my ($self, $path, $params) = @_;
666 0           my $url = join("/", $self->apiBase, $self->apiVersion, $path) . "?apiKey=" .$self->apiKey;
667 0           foreach my $param (keys %$params) {
668 0           $url .= "&".uri_escape($param) ."=". uri_escape($params->{$param});
669             }
670 0           return $url;
671             }
672              
673              
674             =head1 AUTHOR
675              
676             Tim Vroom, C<< >>
677              
678             =head1 BUGS
679              
680             Please report any bugs or feature requests to C, or through
681             the web interface at L. I will be notified, and then you'll
682             automatically be notified of progress on your bug as I make changes.
683              
684              
685              
686              
687             =head1 SUPPORT
688              
689             You can find documentation for this module with the perldoc command.
690              
691             perldoc WebService::JotForm
692              
693              
694             You can also look for information at:
695              
696             =over 4
697              
698             =item * RT: CPAN's request tracker (report bugs here)
699              
700             L
701              
702             =item * AnnoCPAN: Annotated CPAN documentation
703              
704             L
705              
706             =item * CPAN Ratings
707              
708             L
709              
710             =item * Search CPAN
711              
712             L
713              
714             =back
715              
716              
717             =head1 ACKNOWLEDGEMENTS
718              
719              
720             =head1 LICENSE AND COPYRIGHT
721              
722             Copyright 2014 Tim Vroom.
723              
724             This program is free software; you can redistribute it and/or modify it
725             under the terms of the the Artistic License (2.0). You may obtain a
726             copy of the full license at:
727              
728             L
729              
730             Any use, modification, and distribution of the Standard or Modified
731             Versions is governed by this Artistic License. By using, modifying or
732             distributing the Package, you accept this license. Do not use, modify,
733             or distribute the Package, if you do not accept this license.
734              
735             If your Modified Version has been derived from a Modified Version made
736             by someone other than you, you are nevertheless required to ensure that
737             your Modified Version complies with the requirements of this license.
738              
739             This license does not grant you the right to use any trademark, service
740             mark, tradename, or logo of the Copyright Holder.
741              
742             This license includes the non-exclusive, worldwide, free-of-charge
743             patent license to make, have made, use, offer to sell, sell, import and
744             otherwise transfer the Package with respect to any patent claims
745             licensable by the Copyright Holder that are necessarily infringed by the
746             Package. If you institute patent litigation (including a cross-claim or
747             counterclaim) against any party alleging that the Package constitutes
748             direct or contributory patent infringement, then this Artistic License
749             to you shall terminate on the date that such litigation is filed.
750              
751             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
752             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
753             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
754             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
755             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
756             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
757             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
758             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
759              
760              
761             =cut
762              
763             1; # End of WebService::JotForm