jnunemaker-wufoo 0.1.1 → 0.2.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/Manifest CHANGED
@@ -1,10 +1,14 @@
1
+ examples/submission.rb
1
2
  History
3
+ lib/wufoo/client.rb
4
+ lib/wufoo/submission.rb
2
5
  lib/wufoo/version.rb
3
6
  lib/wufoo.rb
4
7
  Manifest
5
8
  MIT-LICENSE
6
9
  Rakefile
7
10
  README
11
+ test/test_client.rb
8
12
  test/test_helper.rb
13
+ test/test_submission.rb
9
14
  test/test_wufoo.rb
10
- wufoo.gemspec
@@ -0,0 +1,32 @@
1
+ require 'yaml'
2
+ require File.dirname(__FILE__) + '/../lib/wufoo'
3
+ config = YAML::load(File.read(ENV['HOME'] + '/.wufoo'))
4
+
5
+ client = Wufoo::Client.new('http://orderedlist.wufoo.com', config['api_key'])
6
+ submission = Wufoo::Submission.new(client, 'orderedlistcom-project-request')
7
+
8
+ response = submission.add_params({
9
+ '1' => 'John Nunemaker',
10
+ '12' => 'nunemaker@gmail.com',
11
+ '4' => '1231231234',
12
+ '5' => 'http://addictedtonew.com',
13
+ '6' => 'ASAP!',
14
+ '10' => '1,000,000',
15
+ '11' => 'My cool project!',
16
+ }).process
17
+
18
+ if response.success?
19
+ puts response.message
20
+ else
21
+ # Something was wrong with the request
22
+ # (missing api key, invalid form, etc)
23
+ if response.fail?
24
+ puts response.error
25
+ end
26
+
27
+ # validation errors
28
+ unless response.valid?
29
+ errors = response.errors.collect { |e| "#{e.field_id} (#{e.code}): #{e.message}" }
30
+ puts errors * "\n"
31
+ end
32
+ end
data/lib/wufoo.rb CHANGED
@@ -1,89 +1,9 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
1
3
  require 'rubygems'
2
4
  gem 'httparty', '0.2.2'
3
5
  require 'httparty'
4
6
 
5
- class Wufoo
6
- include HTTParty
7
-
8
- attr_accessor :url, :api_key, :form, :params
9
-
10
- def initialize(url, api_key, form, params={})
11
- @url, @api_key, @form = url, api_key, form
12
- @params = {}.merge(params || {})
13
- end
14
-
15
- def add_params(new_params)
16
- @params.merge!(new_params)
17
- self
18
- end
19
-
20
- def process
21
- data = params.merge({
22
- :w_api_key => api_key,
23
- :w_form => form,
24
- })
25
-
26
- Wufoo::Response.new(self.class.post("#{@url}/api/insert/", :query => data, :format => :json))
27
- end
28
-
29
- class Response
30
- attr_accessor :data
31
-
32
- def initialize(data)
33
- @data = data
34
- populate
35
- end
36
-
37
- def success?
38
- return false if data.nil? || data == {}
39
- data['wufoo_submit'].first['success'] == 'true'
40
- end
41
-
42
- def fail?
43
- return true if data.nil? || data == {}
44
- error.size > 0
45
- end
46
-
47
- def valid?
48
- errors.size == 0
49
- end
50
-
51
- def error
52
- @error || ''
53
- end
54
-
55
- def errors
56
- @errors || []
57
- end
58
-
59
- def message
60
- @message || ''
61
- end
62
-
63
- def entry_id
64
- @entry_id
65
- end
66
-
67
- private
68
- def populate
69
- @message = data['wufoo_submit'].first['confirmation_message']
70
- @entry_id = data['wufoo_submit'].first['entry_id']
71
- @error = data['wufoo_submit'].first['error']
72
- @raw_errors = data['wufoo_submit'].first['field_errors']
73
-
74
- if @raw_errors && @raw_errors.size > 0
75
- @errors = @raw_errors.inject([]) { |acc, error| acc << FieldError.new(error) }
76
- end
77
- end
78
- end
79
-
80
- class FieldError
81
- attr_accessor :field_id, :code, :message
82
-
83
- def initialize(attrs)
84
- @field_id = attrs['field_id']
85
- @code = attrs['error_code']
86
- @message = attrs['error_message']
87
- end
88
- end
89
- end
7
+ require 'wufoo/client'
8
+ require 'wufoo/submission'
9
+ require 'wufoo/version'
@@ -0,0 +1,18 @@
1
+ module Wufoo
2
+ class Client
3
+ include HTTParty
4
+
5
+ attr_accessor :url, :api_key
6
+
7
+ def initialize(url, api_key)
8
+ @url, @api_key = url, api_key
9
+ end
10
+
11
+ def post(path, data)
12
+ data.merge!({
13
+ :w_api_key => api_key,
14
+ })
15
+ self.class.post("#{@url}#{path}", :query => data, :format => :json)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,81 @@
1
+ module Wufoo
2
+ class Submission
3
+ attr_accessor :client, :form, :params
4
+
5
+ def initialize(client, form, params={})
6
+ @client = client
7
+ @form = form
8
+ @params = {}.merge(params || {})
9
+ end
10
+
11
+ def add_params(new_params)
12
+ @params.merge!(new_params)
13
+ self
14
+ end
15
+
16
+ def process
17
+ Response.new(@client.post('/api/insert/', params.merge({:w_form => form})))
18
+ end
19
+
20
+ class Response
21
+ attr_accessor :data
22
+
23
+ def initialize(data)
24
+ @data = data
25
+ populate
26
+ end
27
+
28
+ def success?
29
+ return false if data.nil? || data == {}
30
+ data['wufoo_submit'].first['success'] == 'true'
31
+ end
32
+
33
+ def fail?
34
+ return true if data.nil? || data == {}
35
+ error.size > 0
36
+ end
37
+
38
+ def valid?
39
+ errors.size == 0
40
+ end
41
+
42
+ def error
43
+ @error || ''
44
+ end
45
+
46
+ def errors
47
+ @errors || []
48
+ end
49
+
50
+ def message
51
+ @message || ''
52
+ end
53
+
54
+ def entry_id
55
+ @entry_id
56
+ end
57
+
58
+ private
59
+ def populate
60
+ @message = data['wufoo_submit'].first['confirmation_message']
61
+ @entry_id = data['wufoo_submit'].first['entry_id']
62
+ @error = data['wufoo_submit'].first['error']
63
+ @raw_errors = data['wufoo_submit'].first['field_errors']
64
+
65
+ if @raw_errors && @raw_errors.size > 0
66
+ @errors = @raw_errors.inject([]) { |acc, error| acc << FieldError.new(error) }
67
+ end
68
+ end
69
+ end
70
+
71
+ class FieldError
72
+ attr_accessor :field_id, :code, :message
73
+
74
+ def initialize(attrs)
75
+ @field_id = attrs['field_id']
76
+ @code = attrs['error_code']
77
+ @message = attrs['error_message']
78
+ end
79
+ end
80
+ end
81
+ end
data/lib/wufoo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
- class Wufoo
2
- Version = '0.1.1'
1
+ module Wufoo
2
+ Version = '0.2.0'
3
3
  end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+ test 'initialize' do
5
+ client = Wufoo::Client.new('http://foobar.wufoo.com', 'somecrazyapikey')
6
+ assert_equal('http://foobar.wufoo.com', client.url)
7
+ assert_equal('somecrazyapikey', client.api_key)
8
+ end
9
+ end
data/test/test_helper.rb CHANGED
@@ -2,4 +2,31 @@ require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'context'
4
4
  require 'stump'
5
- require File.join(File.dirname(__FILE__), '..', 'lib', 'wufoo')
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'wufoo')
6
+
7
+ def successful_response_data
8
+ {"wufoo_submit" => [{
9
+ "confirmation_message" => "Success! Thanks for filling out my form!",
10
+ "entry_id" => "1025",
11
+ "success" => "true"
12
+ }]}
13
+ end
14
+
15
+ def error_response_data
16
+ {"wufoo_submit" => [{
17
+ "error" => "The supplied form URL was not found.",
18
+ "field_errors" => [],
19
+ "success" => "false"
20
+ }]}
21
+ end
22
+
23
+ def field_error_response_data
24
+ {"wufoo_submit" => [{
25
+ "error" => "",
26
+ "field_errors" => [
27
+ {"field_id" => "field0", "error_message" => "Invalid email address.", "error_code" => "2"},
28
+ {"field_id" => "field1", "error_message" => "Field is required.", "error_code" => "0"},
29
+ ],
30
+ "success" => "false"
31
+ }]}
32
+ end
@@ -0,0 +1,129 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class TestSubmission < Test::Unit::TestCase
4
+ before do
5
+ @client = Wufoo::Client.new('http://foobar.wufoo.com', 'somecrazyapikey')
6
+ end
7
+
8
+ test 'initialize' do
9
+ submission = Wufoo::Submission.new(@client, 'my-crazy-form')
10
+ assert_equal(@client, submission.client)
11
+ assert_equal('my-crazy-form', submission.form)
12
+ assert_equal({}, submission.params)
13
+ end
14
+
15
+ test 'initialize with params' do
16
+ submission = Wufoo::Submission.new(@client, 'my-crazy-form', {'0' => 'Foo'})
17
+ assert_equal({'0' => 'Foo'}, submission.params)
18
+ end
19
+
20
+ test 'add_params' do
21
+ submission = Wufoo::Submission.new(@client, 'my-crazy-form').add_params('0' => 'Foo')
22
+ assert_equal({'0' => 'Foo'}, submission.params)
23
+ end
24
+
25
+ test 'add_params returns self' do
26
+ assert_kind_of(Wufoo::Submission, Wufoo::Submission.new(@client, 'my-crazy-form').add_params('0' => 'Foo'))
27
+ end
28
+
29
+ context 'processing response that was successful' do
30
+ before do
31
+ @client.stub!(:post, :return => successful_response_data)
32
+ submission = Wufoo::Submission.new(@client, 'my-crazy-form').add_params({'0' => 'Foobar!'})
33
+ @response = submission.process
34
+ end
35
+
36
+ test 'should have data' do
37
+ assert_equal(successful_response_data, @response.data)
38
+ end
39
+
40
+ test 'should be success?' do
41
+ assert @response.success?
42
+ end
43
+
44
+ test 'should not be fail?' do
45
+ assert ! @response.fail?
46
+ end
47
+
48
+ test 'should be valid?' do
49
+ assert @response.valid?
50
+ end
51
+
52
+ test 'should have message' do
53
+ assert_equal('Success! Thanks for filling out my form!', @response.message)
54
+ end
55
+
56
+ test 'should have entry_id' do
57
+ assert_equal('1025', @response.entry_id)
58
+ end
59
+
60
+ test 'should not have error' do
61
+ assert_equal('', @response.error)
62
+ end
63
+
64
+ test 'should not have errors' do
65
+ assert_equal([], @response.errors)
66
+ end
67
+ end
68
+
69
+ context 'processing a response that failed' do
70
+ before do
71
+ @client.stub!(:post, :return => error_response_data)
72
+ submission = Wufoo::Submission.new(@client, 'my-crazy-form').add_params({'0' => 'Foobar!'})
73
+ @response = submission.process
74
+ end
75
+
76
+ test 'should have data' do
77
+ assert_equal(error_response_data, @response.data)
78
+ end
79
+
80
+ test 'should not be success?' do
81
+ assert ! @response.success?
82
+ end
83
+
84
+ test 'should be a fail?' do
85
+ assert @response.fail?
86
+ end
87
+
88
+ test 'should be valid?' do
89
+ assert @response.valid?
90
+ end
91
+
92
+ test 'should have error' do
93
+ assert_equal('The supplied form URL was not found.', @response.error)
94
+ end
95
+ end
96
+
97
+ context 'processing a response with field errors' do
98
+ before do
99
+ @client.stub!(:post, :return => field_error_response_data)
100
+ submission = Wufoo::Submission.new(@client, 'my-crazy-form').add_params({'0' => 'Foobar!'})
101
+ @response = submission.process
102
+ end
103
+
104
+ test 'should have data' do
105
+ assert_equal(field_error_response_data, @response.data)
106
+ end
107
+
108
+ test 'should not be success?' do
109
+ assert ! @response.success?
110
+ end
111
+
112
+ test 'should not be fail?' do
113
+ assert ! @response.fail?
114
+ end
115
+
116
+ test 'should not be valid?' do
117
+ assert ! @response.valid?
118
+ end
119
+
120
+ test 'should have errors' do
121
+ field_ids = ['field0', 'field1']
122
+ messages = ['Invalid email address.', 'Field is required.']
123
+ codes = ['2', '0']
124
+ assert_equal(field_ids, @response.errors.collect { |e| e.field_id })
125
+ assert_equal(messages, @response.errors.collect { |e| e.message })
126
+ assert_equal(codes, @response.errors.collect { |e| e.code })
127
+ end
128
+ end
129
+ end
data/test/test_wufoo.rb CHANGED
@@ -1,155 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/test_helper'
2
2
 
3
3
  class TestWufoo < Test::Unit::TestCase
4
- before do
5
- @successful_response_data = {
6
- "wufoo_submit" => [{
7
- "confirmation_message" => "Success! Thanks for filling out my form!",
8
- "entry_id" => "1025",
9
- "success" => "true"
10
- }]
11
- }
12
-
13
- @error_response_data = {
14
- "wufoo_submit" => [{
15
- "error" => "The supplied form URL was not found.",
16
- "field_errors" => [],
17
- "success" => "false"
18
- }]
19
- }
20
-
21
- @field_error_response_data = {
22
- "wufoo_submit" => [{
23
- "error" => "",
24
- "field_errors" => [
25
- {"field_id" => "field0", "error_message" => "Invalid email address.", "error_code" => "2"},
26
- {"field_id" => "field1", "error_message" => "Field is required.", "error_code" => "0"},
27
- ],
28
- "success" => "false"
29
- }]
30
- }
4
+ test 'should have version' do
5
+ assert_not_nil Wufoo::Version
31
6
  end
32
-
33
- test 'initialize' do
34
- wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form')
35
- assert_equal('http://dummy.wufoo.com', wufoo.url)
36
- assert_equal('foobar', wufoo.api_key)
37
- assert_equal('my-crazy-form', wufoo.form)
38
- assert_equal({}, wufoo.params)
39
- end
40
-
41
- test 'initialize with params' do
42
- wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form', {'0' => 'Foo'})
43
- assert_equal({'0' => 'Foo'}, wufoo.params)
44
- end
45
-
46
- test 'add_params' do
47
- wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params('0' => 'Foo')
48
- assert_equal({'0' => 'Foo'}, wufoo.params)
49
- end
50
-
51
- test 'add_params returns self' do
52
- assert_kind_of(Wufoo, Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params('0' => 'Foo'))
53
- end
54
-
55
- context 'processing response that was successful' do
56
- before do
57
- Wufoo.stub!(:post, :return => @successful_response_data)
58
- wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params({'0' => 'Foobar!'})
59
- @response = wufoo.process
60
- end
61
-
62
- test 'should have data' do
63
- assert_equal(@successful_response_data, @response.data)
64
- end
65
-
66
- test 'should be success?' do
67
- assert @response.success?
68
- end
69
-
70
- test 'should not be fail?' do
71
- assert ! @response.fail?
72
- end
73
-
74
- test 'should be valid?' do
75
- assert @response.valid?
76
- end
77
-
78
- test 'should have message' do
79
- assert_equal('Success! Thanks for filling out my form!', @response.message)
80
- end
81
-
82
- test 'should have entry_id' do
83
- assert_equal('1025', @response.entry_id)
84
- end
85
-
86
- test 'should not have error' do
87
- assert_equal('', @response.error)
88
- end
89
-
90
- test 'should not have errors' do
91
- assert_equal([], @response.errors)
92
- end
93
- end
94
-
95
- context 'processing a response that failed' do
96
- before do
97
- Wufoo.stub!(:post, :return => @error_response_data)
98
- wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params({'0' => 'Foobar!'})
99
- @response = wufoo.process
100
- end
101
-
102
- test 'should have data' do
103
- assert_equal(@error_response_data, @response.data)
104
- end
105
-
106
- test 'should not be success?' do
107
- assert ! @response.success?
108
- end
109
-
110
- test 'should be a fail?' do
111
- assert @response.fail?
112
- end
113
-
114
- test 'should be valid?' do
115
- assert @response.valid?
116
- end
117
-
118
- test 'should have error' do
119
- assert_equal('The supplied form URL was not found.', @response.error)
120
- end
121
- end
122
-
123
- context 'processing a response with field errors' do
124
- before do
125
- Wufoo.stub!(:post, :return => @field_error_response_data)
126
- wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params({'0' => 'Foobar!'})
127
- @response = wufoo.process
128
- end
129
-
130
- test 'should have data' do
131
- assert_equal(@field_error_response_data, @response.data)
132
- end
133
-
134
- test 'should not be success?' do
135
- assert ! @response.success?
136
- end
137
-
138
- test 'should not be fail?' do
139
- assert ! @response.fail?
140
- end
141
-
142
- test 'should not be valid?' do
143
- assert ! @response.valid?
144
- end
145
-
146
- test 'should have errors' do
147
- field_ids = ['field0', 'field1']
148
- messages = ['Invalid email address.', 'Field is required.']
149
- codes = ['2', '0']
150
- assert_equal(field_ids, @response.errors.collect { |e| e.field_id })
151
- assert_equal(messages, @response.errors.collect { |e| e.message })
152
- assert_equal(codes, @response.errors.collect { |e| e.code })
153
- end
154
- end
155
7
  end
data/wufoo.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{wufoo}
5
- s.version = "0.1.1"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["John Nunemaker"]
9
9
  s.date = %q{2008-12-16}
10
10
  s.description = %q{simple wrapper for the wufoo api}
11
11
  s.email = %q{nunemaker@gmail.com}
12
- s.extra_rdoc_files = ["lib/wufoo/version.rb", "lib/wufoo.rb", "README"]
13
- s.files = ["History", "lib/wufoo/version.rb", "lib/wufoo.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "test/test_helper.rb", "test/test_wufoo.rb", "wufoo.gemspec"]
12
+ s.extra_rdoc_files = ["lib/wufoo/client.rb", "lib/wufoo/submission.rb", "lib/wufoo/version.rb", "lib/wufoo.rb", "README"]
13
+ s.files = ["examples/submission.rb", "History", "lib/wufoo/client.rb", "lib/wufoo/submission.rb", "lib/wufoo/version.rb", "lib/wufoo.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "test/test_client.rb", "test/test_helper.rb", "test/test_submission.rb", "test/test_wufoo.rb", "wufoo.gemspec"]
14
14
  s.has_rdoc = true
15
15
  s.homepage = %q{}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Wufoo", "--main", "README"]
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.rubyforge_project = %q{wufoo}
19
19
  s.rubygems_version = %q{1.3.1}
20
20
  s.summary = %q{simple wrapper for the wufoo api}
21
- s.test_files = ["test/test_helper.rb", "test/test_wufoo.rb"]
21
+ s.test_files = ["test/test_client.rb", "test/test_helper.rb", "test/test_submission.rb", "test/test_wufoo.rb"]
22
22
 
23
23
  if s.respond_to? :specification_version then
24
24
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jnunemaker-wufoo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -37,18 +37,25 @@ executables: []
37
37
  extensions: []
38
38
 
39
39
  extra_rdoc_files:
40
+ - lib/wufoo/client.rb
41
+ - lib/wufoo/submission.rb
40
42
  - lib/wufoo/version.rb
41
43
  - lib/wufoo.rb
42
44
  - README
43
45
  files:
46
+ - examples/submission.rb
44
47
  - History
48
+ - lib/wufoo/client.rb
49
+ - lib/wufoo/submission.rb
45
50
  - lib/wufoo/version.rb
46
51
  - lib/wufoo.rb
47
52
  - Manifest
48
53
  - MIT-LICENSE
49
54
  - Rakefile
50
55
  - README
56
+ - test/test_client.rb
51
57
  - test/test_helper.rb
58
+ - test/test_submission.rb
52
59
  - test/test_wufoo.rb
53
60
  - wufoo.gemspec
54
61
  has_rdoc: true
@@ -83,5 +90,7 @@ signing_key:
83
90
  specification_version: 2
84
91
  summary: simple wrapper for the wufoo api
85
92
  test_files:
93
+ - test/test_client.rb
86
94
  - test/test_helper.rb
95
+ - test/test_submission.rb
87
96
  - test/test_wufoo.rb