File Coverage

blib/lib/WebService/JotForm.pm
Criterion Covered Total %
statement 21 156 13.4
branch 0 48 0.0
condition 0 31 0.0
subroutine 7 44 15.9
pod 33 34 97.0
total 61 313 19.4


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