Class: WufooParty::Form

Inherits:
Entity
  • Object
show all
Defined in:
lib/wufoo_party.rb

Overview

Wraps an individual Wufoo Form.

Instantiation

There are two ways to instantiate a Form object:

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

  2. Via the WufooParty::Form class directly.

wufoo = WufooParty.new(ACCOUNT, API_KEY)
form = wufoo.form(FORM_ID)
# or...
form = WufooParty::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

Instance Method Summary collapse

Methods inherited from Entity

#initialize

Constructor Details

This class inherits a constructor from WufooParty::Entity

Instance Method Details

#[](id) ⇒ Object

Access form details.



215
216
217
218
# File 'lib/wufoo_party.rb', line 215

def [](id)
  @details ||= @party.form(@id)
  @details[id]
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)


291
292
293
294
# File 'lib/wufoo_party.rb', line 291

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

#entries(filters = [], filter_match = 'AND') ⇒ Object

Return entries already submitted to the form.

If you need to filter entries, pass an array as the first argument:

entries([[field_id, operator, value], ...])

e.g.:

entries([['EntryId', 'Is_after', 12], ['EntryId', 'Is_before', 17]])
entries([['Field1', 'Is_equal', 'Tim']])

The second arg is the match parameter (AND/OR) and defaults to ‘AND’, e.g.

entries([['Field2', 'Is_equal', 'Morgan'], ['Field2', 'Is_equal', 'Smith']], 'OR')

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



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/wufoo_party.rb', line 250

def entries(filters=[], filter_match='AND')
  if filters.any?
    options = {'match' => filter_match}
    filters.each_with_index do |filter, index|
      options["Filter#{index+1}"] = filter.join(' ')
    end
    options = {:query => options}
  else
    options = {}
  end
  @party.get("forms/#{@id}/entries", options)['Entries']
end

#fieldsObject

Returns field details for the form.



210
211
212
# File 'lib/wufoo_party.rb', line 210

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.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/wufoo_party.rb', line 225

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.



272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/wufoo_party.rb', line 272

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