forked from jotform/jotform-api-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJotForm.rb
More file actions
469 lines (399 loc) · 19.3 KB
/
JotForm.rb
File metadata and controls
469 lines (399 loc) · 19.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env ruby
# JotForm API - Ruby Client
# @copyright 2022 Jotform, Inc.
# @link https://www.jotform.com
# @version 1.0
require "net/http"
require "uri"
require "rubygems"
require "json"
class JotForm
attr_accessor :apiKey
attr_accessor :baseURL
attr_accessor :apiVersion
# Create the object
def initialize(apiKey = nil, baseURL = "https://api.jotform.com", apiVersion = "v1")
@apiKey = apiKey
@baseURL = baseURL
@apiVersion = apiVersion
end
def _executeHTTPRequest(endpoint, parameters = nil, type = "GET")
url = [@baseURL, @apiVersion, endpoint].join("/")
url = URI.parse(url)
path = url.path + '?apiKey=' + @apiKey
if type == "GET"
request = Net::HTTP::Get.new(path)
elsif type == "POST"
request = Net::HTTP::Post.new(path)
request.set_form_data(parameters)
elsif type == "DELETE"
request = Net::HTTP::Delete.new(path)
elsif type == "PUT"
request = Net::HTTP::Put.new(path, initheader = { 'Content-Type' => 'application/json'})
request.body = parameters
end
response = Net::HTTP.new(url.host).start {|http| http.request(request) }
if response.kind_of? Net::HTTPSuccess
return JSON.parse(response.body)["content"]
else
puts JSON.parse(response.body)
return nil
end
end
def _executeGetRequest(endpoint, parameters = [])
return _executeHTTPRequest(endpoint, parameters, "GET")
end
def _executePostRequest(endpoint, parameters = [])
return _executeHTTPRequest(endpoint, parameters, "POST")
end
def _executePutRequest(endpoint, parameters = [])
return _executeHTTPRequest(endpoint, parameters, "PUT")
end
def _executeDeleteRequest(endpoint, parameters = [])
return _executeHTTPRequest(endpoint, parameters, "DELETE")
end
# getUser: Get user account details for a JotForm user
# @return [json] Returns user account type, avatar URL, name, email, website URL and account limits.
def getUser
return _executeGetRequest("user")
end
# getUsage: Get number of form submissions received this month
# @return [json] Returns number of submissions, number of SSL form submissions, payment form submissions and upload space used by user.
def getUsage
return _executeGetRequest("user/usage");
end
# getForms: Get a list of forms for this account
# @return [json] Returns basic details such as title of the form, when it was created, number of new and total submissions.
def getForms
return _executeGetRequest("user/forms")
end
# getSubmissions: Get a list of submissions for this account
# @return [json] Returns basic details such as title of the form, when it was created, number of new and total submissions.
def getSubmissions
return _executeGetRequest("user/submissions")
end
# getSubusers: Get a list of sub users for this account
# @return [json] Returns list of forms and form folders with access privileges.
def getSubusers
return _executeGetRequest("user/subusers")
end
# getFolders: Get a list of form folders for this account
# @return [json] Returns name of the folder and owner of the folder for shared folders.
def getFolders
return _executeGetRequest("user/folders")
end
# getReports: List of URLS for reports in this account
# @return [json] Returns reports for all of the forms. ie. Excel, CSV, printable charts, embeddable HTML tables.
def getReports
return _executeGetRequest("user/reports")
end
# getSettings: Get user's settings for this account
# @return [json] Returns user's time zone and language.
def getSettings
return _executeGetRequest("user/settings")
end
# updateSettings: Update user's settings
# @param [hash:settings] New user setting values with setting keys
# @return [json] Returns changes on user settings
def updateSettings(settings)
return _executePostRequest('user/settings', settings)
end
# getHistory: Get user activity log
# @return [json] Returns activity log about things like forms created/modified/deleted, account logins and other operations.
def getHistory
return _executeGetRequest("user/history")
end
# getForm: Get basic information about a form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @return [json] Returns form ID, status, update and creation dates, submission count etc.
def getForm(formID)
return _executeGetRequest("form/" + formID)
end
# getFormQuestions: Get a list of all questions of a form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @return [json] Returns question properties of a form.
def getFormQuestions(formID)
return _executeGetRequest("form/" + formID + "/questions")
end
# getFormQuestion: Get details about a question
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [string:qid] You can get Question IDs when you call /form/{id}/questions.
# @return [json] Returns question properties like required and validation.
def getFormQuestion(formID, qid)
return _executeGetRequest("form/" + formID + "/question/" + qid)
end
# getFormProperties: Get a list of all properties of a form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @return [json] Returns form properties like width, expiration date, style etc.
def getFormProperties(formID)
return _executeGetRequest("form/" + formID + "/properties")
end
# getFormProperty: Get a specific property of the form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [string:propertyKey]
# @return [json] Returns given property key value.
def getFormProperty(formID, propertyKey)
return _executeGetRequest("form/" + formID + "/properties/" + propertyKey)
end
# getFormSubmissions: Get list of a form submissions
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @return [json] Returns submissions of a specific form.
def getFormSubmissions(formID)
return _executeGetRequest("form/" + formID + "/submissions")
end
# createFormSubmissions: Submit data to this form using the API
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [hash:submission] Submission data with question IDs
# @return [json] Returns posted submission ID and URL.
def createFormSubmissions(formID, submission)
clearedSubmission = {}
submission.each do |key, value|
if key.include? '_'
exploded = key.split('_')
qid = exploded.first()
type = exploded.last()
clearedSubmission["submission[" + qid + "][" + type + "]"] = value
else
clearedSubmission["submission[" + key + "]"] = value
end
end
return _executePostRequest("form/"+ formID +"/submissions", clearedSubmission)
end
# getFormFiles: List of files uploaded on a form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @return [json] Returns uploaded file information and URLs on a specific form.
def getFormFiles(formID)
return _executeGetRequest("form/" + formID + "/files")
end
# getFormWebhooks: Get list of webhooks for a form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @return [json] Returns list of webhooks for a specific form.
def getFormWebhooks(formID)
return _executeGetRequest("form/" + formID + "/webhooks")
end
# createFormWebhook: Add a new webhook
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [string:webhookURL] Webhook URL is where form data will be posted when form is submitted.
# @return [json] Returns list of webhooks for a specific form.
def createFormWebhook(formID, webhookURL)
return _executePostRequest("form/" + formID + "/webhooks", {"webhookURL" => webhookURL})
end
# TODO
def deleteFormWebhook(formID, webhookID)
return
end
# getSubmission: Get submission data
# @param [string:sid] You can get submission IDs when you call /form/{id}/submissions.
# @return [json] Returns information and answers of a specific submission.
def getSubmission(sid)
return _executeGetRequest("submission/" + sid)
end
# getReport: Get report details
# @param [string:reportID] You can get a list of reports from /user/reports.
# @return [json] Returns properties of a speceific report like fields and status.
def getReport(reportID)
return _executeGetRequest("report/" + reportID)
end
# getFolder: Get folder details
# @param [string:folderID] You can get a list of folders from /user/folders.
# @return [json] Returns a list of forms in a folder, and other details about the form such as folder color.
def getFolder(folderID)
return _executeGetRequest("folder/" + folderID)
end
# createFolder: Create a folder
# @param [hash:folderProperties] Properties of new folder.
# @return [json] New folder.
def createFolder(folderProperties)
return _executePostRequest("folder", folderProperties)
end
# deleteFolder: Delete a specific folder and its subfolder
# @param [string:folderID] You can get a list of folders from /user/folders.
# @return [json] Returns status of request.
def deleteFolder(folderID)
return _executeDeleteRequest("folder/" + folderID)
end
# updateFolder: Update a specific folder
# @param [string:folderID] You can get a list of folders from /user/folders.
# @param [json:folderProperties] New properties of the specified folder.
# @return [json] Returns status of request.
def updateFolder(folderID, folderProperties)
return _executePutRequest("folder/" + folderID, folderProperties)
end
# addFormsToFolder: Add forms to the specified folder
# @param [string:folderID] You can get the list of folders from /user/folders.
# @param [array:formIDs] You can get the list of forms from /user/forms.
# @return [json] Returns status of request.
def addFormsToFolder(folderID, formIDs)
data = {
"forms" => formIDs
}
return updateFolder(folderID, data.to_json)
end
# addFormToFolder: Add a form to the specified folder
# @param [string:folderID] You can get the list of folders from /user/folders.
# @param [string:formID] You can get the list of forms from /user/forms.
# @return [json] Returns status of request.
def addFormToFolder(folderID, formID)
data = {
"forms" => [formID]
}
return updateFolder(folderID, data.to_json)
end
# getFormReports: Get all the reports of a form, such as excel, csv, grid, html, etc.
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @return [json] Returns a list of reports in a form, and other details about the reports such as title.
def getFormReports(formID)
return _executeGetRequest("form/" + formID + "/reports")
end
# createReport: Create new report of a form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [hash:report] Report details. List type, title etc.
# @return [json] Returns report details and URL.
def createReport(formID, report)
return _executePostRequest("form/" + formID + "/reports", report)
end
# deleteSubmission: Delete a single submission
# @param [string:sid] You can get submission IDs when you call /user/submissions.
# @return [json] Returns status of request.
def deleteSubmission(sid)
return _executeDeleteRequest("submission/" + sid)
end
# editSubmission: Edit a single submission
# @param [string:sid] You can get submission IDs when you call /form/{id}/submissions.
# @param [hash:submission] New submission data with question IDs.
# @return [json] Returns status of request.
def editSubmission(sid, submission)
clearedSubmission = {};
submission.each do |key, value|
if key.include? '_' && key != 'created_at'
exploded = key.split('_')
qid = exploded.first()
type = exploded.last()
clearedSubmission["submission[" + qid + "][" + type + "]"] = value
else
clearedSubmission["submission[" + key + "]"] = value
end
end
return _executePostRequest("submission/" + sid, clearedSubmission)
end
# cloneForm: Clone a single form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @return [json] Returns status of request.
def cloneForm(formID)
return _executePostRequest("form/" + formID + "/clone", {})
end
# deleteFormQuestion: Delete a single form question
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [string:qid] Identifier for each question on a form. You can get a list of question IDs from /form/{id}/questions.
# @return [json] Returns status of request.
def deleteFormQuestion(formID, qid)
return _executeDeleteRequest("form/" + formID + "/question/" + qid)
end
# createFormQuestion: Add new question to specified form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [hash:question] New question properties like type and text.
# @return [json] Returns properties of new question.
def createFormQuestion(formID, question)
clearedQuestion = {}
question.each do |key, value|
clearedQuestion["question[" + key + "]"] = value
end
return _executePostRequest("form/" + formID + "/questions", clearedQuestion)
end
# createFormQuestions: Add new questions to specified form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [json] New question properties like type and text.
# @return [json] Returns properties of new questions.
def createFormQuestions(formID, questions)
return _executePutRequest("form/" + formID + "/questions", questions)
end
# editFormQuestion: Add or edit a single question properties
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [string:qid] Identifier for each question on a form. You can get a list of question IDs from /form/{id}/questions.
# @param [hash:questionProperties] New question properties like text and order.
# @return [json] Returns edited property and type of question.
def editFormQuestion(formID, qid, questionProperties)
question = {}
questionProperties.each do |key, value|
question["question[" + key + "]"] = value
end
return _executePostRequest("form/" + formID + "/question/" + qid, question)
end
# setFormProperties: Add or edit properties of a specific form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [hash:formProperties] New properties like label, width etc.
# @return [json] Returns edited properties.
def setFormProperties(formID, formProperties)
properties = {}
formProperties.each do |key, value|
properties["properties[" + key + "]"] = value
end
return _executePostRequest("form/" + formID + "/properties", properties)
end
# setMultipleFormProperties: Add or edit properties of a specific form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @param [json:formProperties] New properties like label width.
# @return [json] Returns edited properties.
def setMultipleFormProperties(formID, formProperties)
return _executePutRequest("form/" + formID + "/properties", formProperties)
end
# createForm: Create a new form
# @param [hash:form] Questions, properties and emails of new form.
# @return [json] Returns new form.
def createForm(form)
clearedForm = {}
form.each do |key, value|
value.each do |k, v|
if key == "properties"
clearedForm[key + "[" + k + "]"] = v
else
v.each do |a, b|
clearedForm[key + "[" + k + "][" + a + "]"] = b
end
end
end
end
return _executePostRequest("user/forms", clearedForm)
end
# createForm: Create new forms
# @param [json:form] Questions, properties and emails of forms.
# @return [json] Returns new forms.
def createForms(form)
return _executePutRequest("user/forms", form)
end
# deleteForm: Delete a specific form
# @param [string:formID] Form ID is the numbers you see on a form URL. You can get form IDs when you call /user/forms.
# @return [json] Returns roperties of deleted form.
def deleteForm(formID)
return _executeDeleteRequest("form/" + formID)
end
# registerUser: Register with username, password and email
# @param [hash:userDetails] username, password and email to register a new user.
# @return [json] Returns new user's details.
def registerUser(userDetails)
return _executePostRequest("user/register", userDetails)
end
# loginUser: Login user with given credentials
# @param [hash:credentials] Username, password, application name and access type of user.
# @return [json] Returns logged in user's settings and app key.
def loginUser(credentials)
return _executePostRequest("user/login", credentials)
end
# logoutUser: Logout user
# @return [json] Status of the request.
def logoutUser
return _executeGetRequest("user/logout")
end
# getPlan: Get details of a plan
# @param [string:planName] Name of the requested plan. FREE, GOLD etc.
# @return [json] Returns details of a plan.
def getPlan(planName)
return _executeGetRequest("system/plan/" + planName)
end
# deleteReport: Delete a specific report
# @param [string:reportID] You can get a list of reports from /user/reports.
# @return [json] Returns status of request.
def deleteReport(reportID)
return _executeDeleteRequest("report/" + reportID)
end
end