wufoo_party 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = WufooParty
2
+
3
+ Lightweight wrapper for Wufoo API over HTTP using HTTParty.
4
+
5
+ == About
6
+
7
+ This is a very simple class that utilizes HTTParty -- a wonderful gem for
8
+ consuming REST APIs.
9
+
10
+ This lib supports both Wufoo's query and submit api version 2.0.
11
+
12
+ == Installation
13
+
14
+ sudo gem install wufoo_party
15
+
16
+ == Usage
17
+
18
+ require 'wufoo_party'
19
+
20
+ ACCOUNT = 'accountname'
21
+ API_KEY = 'AAAA-BBBB-CCCC-DDDD'
22
+ FORM_ID = 'my-form-id'
23
+
24
+ wufoo = WufooParty.new(ACCOUNT, API_KEY)
25
+ # you must use field numbers, shown on the 'API Information' page in Wufoo
26
+ wufoo.submit(FORM_ID, {
27
+ '1' => 'Tim',
28
+ '2' => 'Morgan'
29
+ })
30
+ result = wufoo.query(FORM_ID)
31
+ pp result['form']['Entries'].last['Pretty']
32
+ # {"Last"=>"Morgan",
33
+ # "Name"=>"Tim"}
34
+
35
+ == Feedback
36
+
37
+ I’d love to hear from you if you have suggestions for improvement, bug fixes, or whatever. Email me at tim@timmorgan.org or fork the project and send a pull request.
38
+
39
+ To run the tests, do this:
40
+
41
+ WUFOO_ACCOUNT=accountname \
42
+ WUFOO_API_KEY=AAAA-BBBB-CCCC-DDDD \
43
+ WUFOO_FORM_ID=my-form-id \
44
+ ruby -rrubygems test/wufoo_party_test.rb
@@ -0,0 +1,67 @@
1
+ require 'httparty'
2
+
3
+ class WufooParty
4
+ include HTTParty
5
+ format :json
6
+
7
+ QUERY_ENDPOINT = 'http://%s.wufoo.com/api/query/'
8
+ SUBMIT_ENDPOINT = 'http://%s.wufoo.com/api/insert/'
9
+ API_VERSION = '2.0'
10
+
11
+ def initialize(account, api_key)
12
+ @account = account
13
+ @api_key = api_key
14
+ @field_numbers = {}
15
+ end
16
+
17
+ # Perform a query on a specific form_id.
18
+ # Returns details about the form, as well as individual form submission details.
19
+ def query(form_id)
20
+ args = {
21
+ 'w_api_key' => @api_key,
22
+ 'w_version' => API_VERSION,
23
+ 'w_form' => form_id
24
+ }
25
+ result = self.class.post(QUERY_ENDPOINT % @account, :body => args)
26
+ add_title_to_fields!(result)
27
+ result
28
+ end
29
+
30
+ # Perform a submission to a specific form_id.
31
+ # Returns status of operation.
32
+ def submit(form_id, data={})
33
+ args = {
34
+ 'w_api_key' => @api_key,
35
+ 'w_form' => form_id
36
+ }.merge(data)
37
+ self.class.post(SUBMIT_ENDPOINT % @account, :body => args)
38
+ end
39
+
40
+ # Converts an array of field specs (returned by Wufoo) into a simple hash of <tt>{FIELDNUM => FIELDNAME}</tt>
41
+ def get_field_numbers(field_data)
42
+ field_data.inject({}) do |hash, field|
43
+ if field['SubFields']
44
+ hash.merge!(get_field_numbers(field['SubFields']))
45
+ else
46
+ hash[field['ColumnId']] = field['Title']
47
+ end
48
+ hash
49
+ end
50
+ end
51
+
52
+ # Given the result of a query, adds a 'Pretty' key to each form entry
53
+ # that includes the field titles.
54
+ def add_title_to_fields!(query_result)
55
+ if query_result['form'] and query_result['form']['Entries']
56
+ nums = get_field_numbers(query_result['form']['Fields'])
57
+ query_result['form']['Entries'].each do |entry|
58
+ entry['Pretty'] = {}
59
+ entry.keys.each do |key|
60
+ if key =~ /^Field(\d+)$/ and title = nums[$1]
61
+ entry['Pretty'][title] = entry[key]
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/../lib/wufoo_party'
2
+ require 'test/unit'
3
+
4
+ class WufooPartyTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ if ENV['WUFOO_ACCOUNT'] and ENV['WUFOO_API_KEY'] and ENV['WUFOO_FORM_ID']
8
+ @wufoo = WufooParty.new(ENV['WUFOO_ACCOUNT'], ENV['WUFOO_API_KEY'])
9
+ @form_id = ENV['WUFOO_FORM_ID']
10
+ else
11
+ puts 'Must set WUFOO_ACCOUNT, WUFOO_API_KEY and WUFOO_FORM_ID env variables before running.'
12
+ exit(1)
13
+ end
14
+ end
15
+
16
+ def test_connection
17
+ assert @wufoo.query(@form_id)['form']
18
+ end
19
+
20
+ def test_submission
21
+ start_count = @wufoo.query(@form_id)['form']['EntryCount'].to_i
22
+ result = @wufoo.submit(@form_id) # blank submission - only works if no required fields
23
+ assert_equal 'true', result['wufoo_submit'][0]['success']
24
+ end_count = @wufoo.query(@form_id)['form']['EntryCount'].to_i
25
+ assert_equal start_count+1, end_count
26
+ end
27
+
28
+ def test_get_field_numbers
29
+ assert_equal(
30
+ {'1' => 'Name', '2' => 'Last', '3' => 'Foo'},
31
+ @wufoo.get_field_numbers([
32
+ {"Title" => "Name", "SubFields" => [
33
+ {"Title" => "Name", "ColumnId" => "1"},
34
+ {"Title" => "Last", "ColumnId" => "2"}
35
+ ]},
36
+ {"Title" => "Foo", "ColumnId" => "3"}
37
+ ])
38
+ )
39
+ end
40
+
41
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wufoo_party
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Morgan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-07 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: tim@timmorgan.org
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.rdoc
35
+ - lib/wufoo_party.rb
36
+ - test/wufoo_party_test.rb
37
+ has_rdoc: true
38
+ homepage: http://seven1m.github.com/wufoo_party
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Lightweight wrapper for Wufoo API over HTTP using HTTParty
65
+ test_files: []
66
+