jnunemaker-wufoo 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/History ADDED
File without changes
data/MIT-LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2007 John Nunemaker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,10 @@
1
+ History
2
+ lib/wufoo/version.rb
3
+ lib/wufoo.rb
4
+ Manifest
5
+ MIT-LICENSE
6
+ Rakefile
7
+ README
8
+ test/test_helper.rb
9
+ test/test_wufoo.rb
10
+ wufoo.gemspec
data/README ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+ require 'lib/wufoo/version'
5
+
6
+ Echoe.new('wufoo', Wufoo::Version) do |p|
7
+ p.description = 'simple wrapper for the wufoo api'
8
+ p.author = 'John Nunemaker'
9
+ p.email = 'nunemaker@gmail.com'
10
+ p.extra_deps = [['httparty', '0.2.2']]
11
+ end
12
+
13
+ desc 'Preps the gem for a new release'
14
+ task :prepare do
15
+ %w[manifest build_gemspec].each do |task|
16
+ Rake::Task[task].invoke
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ class Wufoo
2
+ Version = '0.1.1'
3
+ end
data/lib/wufoo.rb ADDED
@@ -0,0 +1,89 @@
1
+ require 'rubygems'
2
+ gem 'httparty', '0.2.2'
3
+ require 'httparty'
4
+
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
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'context'
4
+ require 'stump'
5
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'wufoo')
@@ -0,0 +1,155 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
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
+ }
31
+ 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
+ end
data/wufoo.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{wufoo}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["John Nunemaker"]
9
+ s.date = %q{2008-12-16}
10
+ s.description = %q{simple wrapper for the wufoo api}
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"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Wufoo", "--main", "README"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{wufoo}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{simple wrapper for the wufoo api}
21
+ s.test_files = ["test/test_helper.rb", "test/test_wufoo.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<httparty>, ["= 0.2.2"])
29
+ s.add_development_dependency(%q<echoe>, [">= 0"])
30
+ else
31
+ s.add_dependency(%q<httparty>, ["= 0.2.2"])
32
+ s.add_dependency(%q<echoe>, [">= 0"])
33
+ end
34
+ else
35
+ s.add_dependency(%q<httparty>, ["= 0.2.2"])
36
+ s.add_dependency(%q<echoe>, [">= 0"])
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jnunemaker-wufoo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - John Nunemaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-16 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.2.2
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: echoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ description: simple wrapper for the wufoo api
34
+ email: nunemaker@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - lib/wufoo/version.rb
41
+ - lib/wufoo.rb
42
+ - README
43
+ files:
44
+ - History
45
+ - lib/wufoo/version.rb
46
+ - lib/wufoo.rb
47
+ - Manifest
48
+ - MIT-LICENSE
49
+ - Rakefile
50
+ - README
51
+ - test/test_helper.rb
52
+ - test/test_wufoo.rb
53
+ - wufoo.gemspec
54
+ has_rdoc: true
55
+ homepage: ""
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --line-numbers
59
+ - --inline-source
60
+ - --title
61
+ - Wufoo
62
+ - --main
63
+ - README
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "1.2"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project: wufoo
81
+ rubygems_version: 1.2.0
82
+ signing_key:
83
+ specification_version: 2
84
+ summary: simple wrapper for the wufoo api
85
+ test_files:
86
+ - test/test_helper.rb
87
+ - test/test_wufoo.rb