formstack-api 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (23) hide show
  1. checksums.yaml +4 -4
  2. data/lib/formstack/api/version.rb +1 -1
  3. data/lib/formstack/api.rb +169 -1
  4. data/spec/fs-logo.png +0 -0
  5. data/spec/support/vcr.rb +34 -7
  6. data/spec/vcr/Formstack_API/_copy_form/adds_COPY_at_the_end_of_the_form_name.yml +434 -0
  7. data/spec/vcr/Formstack_API/_create_field/creates_a_field_with_embeded_code.yml +43 -0
  8. data/spec/vcr/Formstack_API/_create_field/creates_a_text_field.yml +43 -0
  9. data/spec/vcr/Formstack_API/_delete_submission/deletes_a_submission.yml +142 -0
  10. data/spec/vcr/Formstack_API/_edit_submission_data/edits_a_submission_succesfully.yml +373 -0
  11. data/spec/vcr/Formstack_API/_edit_submission_data/raises_an_exception_if_fields_are_non_numeric.yml +66 -0
  12. data/spec/vcr/Formstack_API/_edit_submission_data/raises_an_exception_when_data_array_and_ids_don_t_match.yml +66 -0
  13. data/spec/vcr/Formstack_API/_edit_submission_data/raises_an_exception_when_data_array_and_ids_don_t_match_reversed.yml +66 -0
  14. data/spec/vcr/Formstack_API/_edit_submission_data/raises_an_exception_with_a_bad_timestamp.yml +66 -0
  15. data/spec/vcr/Formstack_API/_form_submissions/gets_submissions_default.yml +388 -245
  16. data/spec/vcr/Formstack_API/_form_submissions/raises_an_error_if_page_number_is_not_working.yml +79 -0
  17. data/spec/vcr/Formstack_API/_form_submissions/returns_data_if_specified.yml +245 -0
  18. data/spec/vcr/Formstack_API/_form_submissions/returns_no_data_by_default.yml +65 -0
  19. data/spec/vcr/Formstack_API/_get_submission_details/gets_the_right_submission_from_the_right_form.yml +43 -0
  20. data/spec/vcr/Formstack_API/_submit_form/creates_a_submission_into_a_form.yml +46 -0
  21. data/spec/vcr/Formstack_API/_submit_form/uploads_an_image.yml +44 -0
  22. data/spec/wrapper_spec.rb +217 -20
  23. metadata +32 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a09c170f5568dff06fdb8fdf3c24d4053aa3607e
4
- data.tar.gz: 554fe3ca8294a748799b653f1392914c26136854
3
+ metadata.gz: ad366b35bf42497cc4d1735aeac2b9232523b97e
4
+ data.tar.gz: 302454bbbe75c49f7be732f234b6be5f19c81b34
5
5
  SHA512:
6
- metadata.gz: 658128a10ab946674094b6834d963a29bf4de3c95c12946ebde22d8c08ff29d5a1fa45655e53aa97c33d869aaa3eed2e8b7ae0d222c5c8d1d94ceeefad2fe5d2
7
- data.tar.gz: e1f76a65b2e54d89a3732a88d8dc2600a87f99986f92e293517fc86865267a3958db4f0468a736446174cf6ba62f5a96ca02e55bb670a1aac8e6eb821af4428f
6
+ metadata.gz: 58bcdfdbea8345d3668b493a7686dc296df7440847e76f97fd5a46384b23c2226c85d3da2455e40c2ef89f1ba13e4e8a15c9e6b0325ca7a666acc9ff17f4320a
7
+ data.tar.gz: dd1f98dfafda3c1d8765c7bcede742a42a36d79a3c401d0cad1f13ae9c965f9639ff34dd10129088ea46b36b1982e83b9d4ade893181b91ed98333f78e3b1d66
@@ -1,5 +1,5 @@
1
1
  module Formstack
2
2
  module Api
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
data/lib/formstack/api.rb CHANGED
@@ -8,6 +8,27 @@ module Formstack
8
8
 
9
9
  include HTTParty
10
10
 
11
+ FIELD_TYPES = [
12
+ 'text',
13
+ 'textarea',
14
+ 'name',
15
+ 'address',
16
+ 'email',
17
+ 'phone',
18
+ 'creditcard',
19
+ 'datetime',
20
+ 'file',
21
+ 'number',
22
+ 'select',
23
+ 'radio',
24
+ 'checkbox',
25
+ 'matrix',
26
+ 'richtext',
27
+ 'embed',
28
+ 'product',
29
+ 'section',
30
+ ]
31
+
11
32
  def initialize(access_token)
12
33
  @access_token = access_token
13
34
  @api_url = 'https://www.formstack.com/api/v2/'
@@ -35,6 +56,10 @@ module Formstack
35
56
  response = self.class.get(url, arguments )
36
57
  when 'POST'
37
58
  response = self.class.post(url, arguments )
59
+ when 'PUT'
60
+ response = self.class.put(url, arguments )
61
+ when 'DELETE'
62
+ response = self.class.delete(url, arguments )
38
63
  end
39
64
 
40
65
  if response.code < 200 || response.code >= 300
@@ -118,17 +143,160 @@ module Formstack
118
143
  'expand_data' => expand_data,
119
144
  }
120
145
 
146
+ arguments.delete('data') unless data
147
+ arguments.delete('expand_data') unless expand_data
148
+
149
+ arguments = API.clean_arguments(arguments)
150
+
121
151
  search_fields.each_with_index do |field, idx|
122
152
  arguments["search_field_#{idx}"] = search_field_ids[idx]
123
153
  arguments["search_value_#{idx}"] = search_field_values[idx]
124
154
  end
125
155
 
126
- response = request(end_point, 'GET', arguments)
156
+ response = request(end_point, 'GET', { body: arguments })
127
157
  response['submissions']
128
158
  end
129
159
 
160
+ def submit_form(form_id,
161
+ field_ids = [],
162
+ field_values = [],
163
+ timestamp = '',
164
+ user_agent = '',
165
+ ip_address = '',
166
+ payment_status = '',
167
+ read = false)
168
+
169
+ arguments = {
170
+ 'timestamp' => timestamp,
171
+ 'user_agent' => user_agent,
172
+ 'remote_addr' => ip_address,
173
+ 'payment_status' => payment_status,
174
+ 'read' => read ? 1 : 0
175
+ }
176
+
177
+ is_numeric?(form_id)
178
+
179
+ unless timestamp.empty?
180
+ begin
181
+ Time.parse(timestamp)
182
+ rescue Exception => e
183
+ raise 'You must use a valid Date/Time string formatted in YYYY-MM-DD HH:MM:SS'
184
+ end
185
+ end
186
+
187
+ raise 'There must be a one-to-one relationship between Field IDs and their values' unless field_ids.size == field_values.size
188
+
189
+ arguments = API.clean_arguments(arguments)
190
+ arguments = API.add_field_data(arguments, field_ids, field_values)
191
+
192
+ response = request("form/#{form_id}/submission.json", 'POST', { body: arguments } )
193
+ end
194
+
195
+ def get_submission_details(submission_id, encryption_password = '')
196
+ arguments = {}
197
+ arguments['encryption_password'] = encryption_password unless encryption_password.empty?
198
+
199
+ raise 'Submission ID must be numeric' unless submission_id.is_a? Numeric
200
+
201
+ response = request("submission/#{submission_id}.json", 'GET', arguments)
202
+ end
203
+
204
+ def edit_submission_data(submission_id,
205
+ field_ids = [],
206
+ field_values = [],
207
+ timestamp = '',
208
+ user_agent = '',
209
+ ip_address = '',
210
+ payment_status = '',
211
+ read = false)
212
+
213
+ arguments = {
214
+ 'timestamp' => timestamp,
215
+ 'user_agent' => user_agent,
216
+ 'remote_addr' => ip_address,
217
+ 'payment_status' => payment_status,
218
+ 'read' => read ? 1 : 0
219
+ }
220
+
221
+ raise 'Submission ID must be numeric' unless submission_id.is_a? Numeric
222
+
223
+ unless timestamp.empty?
224
+ begin
225
+ Time.parse(timestamp)
226
+ rescue Exception => e
227
+ raise 'You must use a valid Date/Time string formatted in YYYY-MM-DD HH:MM:SS'
228
+ end
229
+ end
230
+
231
+ arguments = API.clean_arguments(arguments)
232
+ arguments = API.add_field_data(arguments, field_ids, field_values)
233
+
234
+ request("submission/#{submission_id}.json", 'PUT', { body: arguments } )
235
+ end
236
+
237
+ def delete_submission(submission_id)
238
+ raise 'Submission ID must be numeric' unless submission_id.is_a? Numeric
239
+
240
+ request("submission/#{submission_id}", 'DELETE' )
241
+ end
242
+
243
+ def create_field(form_id, field_type = '', label = '', hide_label = false,
244
+ description = '', use_callout = false,
245
+ field_specific_attributes = [], default_value = '',
246
+ options = [], options_values = [], required = false,
247
+ read_only = false, hidden = false, unique = false,
248
+ column_span = nil, sort = nil)
249
+
250
+ is_numeric?(form_id)
251
+
252
+ raise 'Provided Field Type is not in the list of known Field types' unless FIELD_TYPES.include? field_type
253
+
254
+ arguments = {}
255
+
256
+ arguments['field_type'] = field_type
257
+ arguments['label'] = label unless label.empty?
258
+ arguments['hide_label'] = 1 if hide_label
259
+ arguments['description'] = description unless description.empty?
260
+ arguments['description_callout'] = 1 if use_callout
261
+ arguments['default_value'] = default_value unless default_value.empty?
262
+ arguments['options'] = options unless options.empty?
263
+ arguments['options_values'] = options_values unless options_values.empty?
264
+ arguments['required'] = 1 if required
265
+ arguments['read_only'] = 1 if read_only
266
+ arguments['hidden'] = 1 if hidden
267
+ arguments['unique'] = 1 if unique
268
+ arguments['colspan'] = column_span unless column_span.nil?
269
+ arguments['sort'] = sort unless sort.nil?
270
+
271
+ request("form/#{form_id}/field", 'POST', { body: arguments } )
272
+ end
273
+
130
274
  private
131
275
 
276
+ def self.add_field_data(arguments, field_ids, field_values)
277
+
278
+ if field_ids.empty?
279
+ raise 'There must be a one-to-one relationship between Field IDs and their values' if field_ids.size != field_values.size
280
+ end
281
+
282
+ field_ids.each_with_index do |id, i|
283
+ raise 'Field IDs must be numeric' unless id.is_a? Numeric
284
+ arguments["field_#{field_ids[i]}"] = field_values[i]
285
+ end
286
+
287
+ arguments
288
+ end
289
+
290
+ def self.clean_arguments(args)
291
+ arguments = {}
292
+
293
+ args.each_pair do |key, value|
294
+ arguments[key] = value unless value == ''
295
+ end
296
+
297
+ arguments
298
+ end
299
+
132
300
  def is_numeric?(form_id)
133
301
  unless form_id.is_a? Numeric
134
302
  raise 'Form ID must be numeric'
data/spec/fs-logo.png ADDED
Binary file
data/spec/support/vcr.rb CHANGED
@@ -1,19 +1,46 @@
1
1
  require 'vcr'
2
2
  require 'rspec'
3
3
 
4
- # ACCESS_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
5
- # FORM_ID = 0000000
6
- # INACCESSIBLE_FORM_ID = 0000001
7
- ACCESS_TOKEN = '982cf05d56baa2f12db45bea51d68968'
8
- FORM_ID = 1832745
9
- INACCESSIBLE_FORM_ID = 1832744
4
+ ACCESS_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
5
+ INACCESSIBLE_FORM_ID = 0000001
6
+ DELETE_SUBMISSION_FORM = 0000000
7
+ UPLOAD_FORM_ID = 0000000
8
+ UPLOAD_FIELD_IDS = 00000000
9
+ LOGO_PATH = File.join(File.dirname(__FILE__), '../fs-logo.png')
10
+ SUBMIT_FORM_ID = DELETE_SUBMISSION_FORM
11
+ CREATE_FIELD_FORM_ID = DELETE_SUBMISSION_FORM
12
+ SUBMISSION_DETAILS_ID = 000000000
13
+ SUBMISSION_DETAILS_FORM_ID = 0000000
14
+ EDIT_SUBMISSION_FIELD_ID = 00000000
15
+ EDIT_SUBMISSION_ARRAY_FIELD_ID = 00000000
16
+
17
+ # ACCESS_TOKEN = '982cf05d56baa2f12db45bea51d68968'
18
+ # INACCESSIBLE_FORM_ID = 1832744
19
+ # DELETE_SUBMISSION_FORM = 1873710
20
+ # UPLOAD_FORM_ID = 1875940
21
+ # UPLOAD_FIELD_IDS = 28762308
22
+ # LOGO_PATH = File.join(File.dirname(__FILE__), '../fs-logo.png')
23
+ # SUBMIT_FORM_ID = DELETE_SUBMISSION_FORM
24
+ # CREATE_FIELD_FORM_ID = DELETE_SUBMISSION_FORM
25
+ # SUBMISSION_DETAILS_ID = 171938670
26
+ # SUBMISSION_DETAILS_FORM_ID = 1873710
27
+ # EDIT_SUBMISSION_FIELD_ID = 28705506
28
+ # EDIT_SUBMISSION_ARRAY_FIELD_ID = 28705506
10
29
 
11
30
  VCR.configure do |c|
12
31
  c.cassette_library_dir = 'spec/vcr'
13
32
  c.hook_into :webmock # or :fakeweb
14
33
  c.filter_sensitive_data('<ACCESS_TOKEN>') { ACCESS_TOKEN }
15
- c.filter_sensitive_data('<FORM_ID>') { FORM_ID }
34
+ c.filter_sensitive_data('<FORM_ID>') { SUBMIT_FORM_ID }
16
35
  c.filter_sensitive_data('<INACCESSIBLE_FORM_ID>') { INACCESSIBLE_FORM_ID }
36
+ c.filter_sensitive_data('<SUBMISSION_DETAILS_ID>') { SUBMISSION_DETAILS_ID }
37
+ c.filter_sensitive_data('<UPLOAD_FORM_ID>') { UPLOAD_FORM_ID }
38
+ c.filter_sensitive_data('<UPLOAD_FIELD_IDS>') { UPLOAD_FIELD_IDS }
39
+ c.filter_sensitive_data('<LOGO_PATH>') { LOGO_PATH }
40
+ c.filter_sensitive_data('<SUBMISSION_DETAILS_FORM_ID>') { SUBMISSION_DETAILS_FORM_ID }
41
+ c.filter_sensitive_data('<DELETE_SUBMISSION_FORM>') { DELETE_SUBMISSION_FORM }
42
+ c.filter_sensitive_data('<EDIT_SUBMISSION_FIELD_ID>') { EDIT_SUBMISSION_FIELD_ID }
43
+ c.filter_sensitive_data('<EDIT_SUBMISSION_ARRAY_FIELD_ID>') { EDIT_SUBMISSION_ARRAY_FIELD_ID }
17
44
  c.default_cassette_options = { record: :new_episodes }
18
45
  c.configure_rspec_metadata!
19
46
  end