Class: WuParty::Form
Overview
Wraps an individual Wufoo Form.
Instantiation
There are two ways to instantiate a Form object:
-
Via the parent WuParty object that represents the account.
-
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
Instance Method Summary collapse
-
#[](id) ⇒ Object
Access form details.
- #add_webhook(url, metadata = false, handshakeKey = "") ⇒ Object
-
#comments(options = {}) ⇒ Object
Returns comment details for the form.
-
#count(options = {}) ⇒ Object
Return entries already submitted to the form.
- #delete_webhook(webhook_id) ⇒ Object
-
#entries(options = {}) ⇒ Object
Return entries already submitted to the form.
-
#fields ⇒ Object
Returns field details for the form.
-
#flattened_fields(all = false) ⇒ Object
Returns fields and subfields, as a flattened array, e.g.
-
#submit(data) ⇒ Object
Submit form data to the form.
Methods inherited from Entity
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(={}) = {:query => } if .any? @party.get("forms/#{@id}/comments", )['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(={}) query = {} if [:filters] query['match'] = [:filter_match] || 'AND' [:filters].each_with_index do |filter, index| query["Filter#{ index + 1 }"] = filter.join(' ') end end if [: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(={}) query = {} if [:filters] query['match'] = [:filter_match] || 'AND' [:filters].each_with_index do |filter, index| query["Filter#{ index + 1 }"] = filter.join(' ') end end if [:limit] query[:pageSize] = [:limit] end if [:pageStart] query[:pageStart] = [:pageStart] end if [:system] query[:system] = true end if [:sort] field, direction = [:sort].split(' ') query[:sort] = field query[:sortDirection] = direction || 'ASC' end @party.get("forms/#{@id}/entries", :query => query)['Entries'] end |
#fields ⇒ Object
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) = {} data.each do |key, value| if value.is_a?(Hash) type = MIME::Types.of(value[:path]).first.content_type rescue 'application/octet-stream' [:multipart] ||= {} [:multipart][key] = {:type => type, :path => value[:path]} else [:body] ||= {} [:body][key] = value end end @party.post("forms/#{@id}/entries", ) end |