Class: WuParty::Form

Inherits:
Entity show all
Defined in:
lib/wuparty.rb

Overview

Wraps an individual Wufoo Form.

Instantiation

There are two ways to instantiate a Form object:

  1. Via the parent WuParty object that represents the account.

  2. Via the WuParty::Form class directly.

wufoo = WuParty.new(ACCOUNT, API_KEY)
form = wufoo.form(FORM_ID)
# or...
form = WuParty::Form.new(FORM_ID, :account => ACCOUNT, :api_key => API_KEY)

The first technique makes a call to the Wufoo API to get the form details, while the second technique lazily loads the form details, once something is accessed via [].

Form Details

Access form details like it is a Hash, e.g.:

form['Name']
form['RedirectMessage']

Instance Attribute Summary

Attributes inherited from Entity

#details, #id

Instance Method Summary collapse

Methods inherited from Entity

#initialize

Constructor Details

This class inherits a constructor from WuParty::Entity

Instance Method Details

#[](id) ⇒ Object

Access form details.



244
245
246
247
# File 'lib/wuparty.rb', line 244

def [](id)
  @details ||= @party.form(@id)
  @details[id]
end

#add_webhook(url, metadata = false, handshakeKey = "") ⇒ Object



249
250
251
# File 'lib/wuparty.rb', line 249

def add_webhook(url,  = false, handshakeKey = "")
  @party.add_webhook(@details["Hash"], url, , handshakeKey)
end

#comments(options = {}) ⇒ Object

Returns comment details for the form. See Wufoo API documentation for possible options, e.g. to filter comments for a specific form entry:

form.comments('entryId' => 123)


375
376
377
378
# File 'lib/wuparty.rb', line 375

def comments(options={})
  options = {:query => options} if options.any?
  @party.get("forms/#{@id}/comments", options)['Comments']
end

#count(options = {}) ⇒ Object

Return entries already submitted to the form.

Supports: Same as Entries above with filtering. form.count(:filters => [[‘Field1’, ‘Is_equal_to’, ‘Tim’]])

See wufoo.com/docs/api/v3/entries/get/#filter for details



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/wuparty.rb', line 329

def count(options={})
  query = {}

  if options[:filters]
    query['match'] = options[:filter_match] || 'AND'
    options[:filters].each_with_index do |filter, index|
      query["Filter#{ index + 1 }"] = filter.join(' ')
    end
  end   

  if options[:system]
    query[:system] = true
  end

  @party.get("forms/#{@id}/entries/count", :query => query)['EntryCount']
end

#delete_webhook(webhook_id) ⇒ Object



253
254
255
# File 'lib/wuparty.rb', line 253

def delete_webhook(webhook_id)
  @party.delete_webhook(@details["Hash"], webhook_id)
end

#entries(options = {}) ⇒ Object

Return entries already submitted to the form.

Supports:

- filtering:
entries(:filters => [['Field1', 'Is_equal_to', 'Tim']])
entries(:filters => [['Field1', 'Is_equal_to', 'Tim'], ['Field2', 'Is_equal_to', 'Morgan']], :filter_match => 'OR')

- sorting:
entries(:sort => 'EntryId DESC')

- limiting:
entries(:limit => 5)

See wufoo.com/docs/api/v3/entries/get/#filter for details



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
# File 'lib/wuparty.rb', line 291

def entries(options={})
  query = {}

  if options[:filters]
    query['match'] = options[:filter_match] || 'AND'
    options[:filters].each_with_index do |filter, index|
      query["Filter#{ index + 1 }"] = filter.join(' ')
    end
  end

  if options[:limit]
    query[:pageSize] = options[:limit]
  end

  if options[:pageStart]
    query[:pageStart] = options[:pageStart]
  end

  if options[:system]
    query[:system] = true
  end

  if options[:sort]
    field, direction = options[:sort].split(' ')
    query[:sort] = field
    query[:sortDirection] = direction || 'ASC'
  end
  
  @party.get("forms/#{@id}/entries", :query => query)['Entries']
end

#fieldsObject

Returns field details for the form.



239
240
241
# File 'lib/wuparty.rb', line 239

def fields
  @party.get("forms/#{@id}/fields")['Fields']
end

#flattened_fields(all = false) ⇒ Object

Returns fields and subfields, as a flattened array, e.g.

[{'ID' => 'Field1', 'Title' => 'Name - First', 'Type' => 'shortname', 'Required' => true }, # (subfield)
 {'ID' => 'Field2', 'Title' => 'Name - Last',  'Type' => 'shortname', 'Required' => true }, # (subfield)
 {'ID' => 'Field3', 'Title' => 'Birthday',     'Type' => 'date',      'Required' => flase}] # (field)

By default, only fields that can be submitted are returned. Pass true as the first arg to return all fields.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/wuparty.rb', line 262

def flattened_fields(all=false)
  flattened = []
  fields.each do |field|
    next unless all or field['ID'] =~ /^Field/
    if field['SubFields']
      field['SubFields'].each do |sub_field|
        flattened << {'ID' => sub_field['ID'], 'Title' => field['Title'] + ' - ' + sub_field['Label'], 'Type' => field['Type'], 'Required' => field['IsRequired'] == '1'}
      end
    else
      flattened << {'ID' => field['ID'], 'Title' => field['Title'], 'Type' => field['Type'], 'Required' => field['IsRequired'] == '1'}
    end
  end
  flattened
end

#submit(data) ⇒ Object

Submit form data to the form. Pass data as a hash, with field ids as the hash keys, e.g.

submit('Field1' => 'Tim', 'Field2' => 'Morgan')

Return value is a Hash that includes the following keys:

  • Status

  • ErrorText

  • FieldErrors

You must submit values for required fields (including all sub fields), and dates must be formatted as YYYYMMDD.



356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/wuparty.rb', line 356

def submit(data)
  options = {}
  data.each do |key, value|
    if value.is_a?(Hash)
      type = MIME::Types.of(value[:path]).first.content_type rescue 'application/octet-stream'
      options[:multipart] ||= {}
      options[:multipart][key] = {:type => type, :path => value[:path]}
    else
      options[:body] ||= {}
      options[:body][key] = value
    end
  end
  @party.post("forms/#{@id}/entries", options)
end