Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 263f61c8f24e30c7068c623b90eb4c14fc356b6e 0 parents
@jnunemaker authored
0  History
No changes.
19 MIT-LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2007 John Nunemaker
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
0  README
No changes.
0  Rakefile
No changes.
89 lib/wufoo.rb
@@ -0,0 +1,89 @@
+require 'rubygems'
+gem 'httparty', '0.2.2'
+require 'httparty'
+
+class Wufoo
+ include HTTParty
+
+ attr_accessor :url, :api_key, :form, :params
+
+ def initialize(url, api_key, form, params={})
+ @url, @api_key, @form = url, api_key, form
+ @params = {}.merge(params || {})
+ end
+
+ def add_params(new_params)
+ @params.merge!(new_params)
+ self
+ end
+
+ def process
+ data = params.merge({
+ :w_api_key => api_key,
+ :w_form => form,
+ })
+
+ Wufoo::Response.new(self.class.post("#{@url}/api/insert/", :query => data, :format => :json))
+ end
+
+ class Response
+ attr_accessor :data
+
+ def initialize(data)
+ @data = data
+ populate
+ end
+
+ def success?
+ return false if data.nil? || data == {}
+ data['wufoo_submit'].first['success'] == 'true'
+ end
+
+ def fail?
+ return true if data.nil? || data == {}
+ error.size > 0
+ end
+
+ def valid?
+ errors.size == 0
+ end
+
+ def error
+ @error || ''
+ end
+
+ def errors
+ @errors || []
+ end
+
+ def message
+ @message || ''
+ end
+
+ def entry_id
+ @entry_id
+ end
+
+ private
+ def populate
+ @message = data['wufoo_submit'].first['confirmation_message']
+ @entry_id = data['wufoo_submit'].first['entry_id']
+ @error = data['wufoo_submit'].first['error']
+ @raw_errors = data['wufoo_submit'].first['field_errors']
+
+ if @raw_errors && @raw_errors.size > 0
+ @errors = @raw_errors.inject([]) { |acc, error| acc << FieldError.new(error) }
+ end
+ end
+ end
+
+ class FieldError
+ attr_accessor :field_id, :code, :message
+
+ def initialize(attrs)
+ @field_id = attrs['field_id']
+ @code = attrs['error_code']
+ @message = attrs['error_message']
+ end
+ end
+end
5 test/test_helper.rb
@@ -0,0 +1,5 @@
+require 'rubygems'
+require 'test/unit'
+require 'context'
+require 'stump'
+require File.join(File.dirname(__FILE__), '..', 'lib', 'wufoo')
155 test/test_wufoo.rb
@@ -0,0 +1,155 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class TestWufoo < Test::Unit::TestCase
+ before do
+ @successful_response_data = {
+ "wufoo_submit" => [{
+ "confirmation_message" => "Success! Thanks for filling out my form!",
+ "entry_id" => "1025",
+ "success" => "true"
+ }]
+ }
+
+ @error_response_data = {
+ "wufoo_submit" => [{
+ "error" => "The supplied form URL was not found.",
+ "field_errors" => [],
+ "success" => "false"
+ }]
+ }
+
+ @field_error_response_data = {
+ "wufoo_submit" => [{
+ "error" => "",
+ "field_errors" => [
+ {"field_id" => "field0", "error_message" => "Invalid email address.", "error_code" => "2"},
+ {"field_id" => "field1", "error_message" => "Field is required.", "error_code" => "0"},
+ ],
+ "success" => "false"
+ }]
+ }
+ end
+
+ test 'initialize' do
+ wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form')
+ assert_equal('http://dummy.wufoo.com', wufoo.url)
+ assert_equal('foobar', wufoo.api_key)
+ assert_equal('my-crazy-form', wufoo.form)
+ assert_equal({}, wufoo.params)
+ end
+
+ test 'initialize with params' do
+ wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form', {'0' => 'Foo'})
+ assert_equal({'0' => 'Foo'}, wufoo.params)
+ end
+
+ test 'add_params' do
+ wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params('0' => 'Foo')
+ assert_equal({'0' => 'Foo'}, wufoo.params)
+ end
+
+ test 'add_params returns self' do
+ assert_kind_of(Wufoo, Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params('0' => 'Foo'))
+ end
+
+ context 'processing response that was successful' do
+ before do
+ Wufoo.stub!(:post, :return => @successful_response_data)
+ wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params({'0' => 'Foobar!'})
+ @response = wufoo.process
+ end
+
+ test 'should have data' do
+ assert_equal(@successful_response_data, @response.data)
+ end
+
+ test 'should be success?' do
+ assert @response.success?
+ end
+
+ test 'should not be fail?' do
+ assert ! @response.fail?
+ end
+
+ test 'should be valid?' do
+ assert @response.valid?
+ end
+
+ test 'should have message' do
+ assert_equal('Success! Thanks for filling out my form!', @response.message)
+ end
+
+ test 'should have entry_id' do
+ assert_equal('1025', @response.entry_id)
+ end
+
+ test 'should not have error' do
+ assert_equal('', @response.error)
+ end
+
+ test 'should not have errors' do
+ assert_equal([], @response.errors)
+ end
+ end
+
+ context 'processing a response that failed' do
+ before do
+ Wufoo.stub!(:post, :return => @error_response_data)
+ wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params({'0' => 'Foobar!'})
+ @response = wufoo.process
+ end
+
+ test 'should have data' do
+ assert_equal(@error_response_data, @response.data)
+ end
+
+ test 'should not be success?' do
+ assert ! @response.success?
+ end
+
+ test 'should be a fail?' do
+ assert @response.fail?
+ end
+
+ test 'should be valid?' do
+ assert @response.valid?
+ end
+
+ test 'should have error' do
+ assert_equal('The supplied form URL was not found.', @response.error)
+ end
+ end
+
+ context 'processing a response with field errors' do
+ before do
+ Wufoo.stub!(:post, :return => @field_error_response_data)
+ wufoo = Wufoo.new('http://dummy.wufoo.com', 'foobar', 'my-crazy-form').add_params({'0' => 'Foobar!'})
+ @response = wufoo.process
+ end
+
+ test 'should have data' do
+ assert_equal(@field_error_response_data, @response.data)
+ end
+
+ test 'should not be success?' do
+ assert ! @response.success?
+ end
+
+ test 'should not be fail?' do
+ assert ! @response.fail?
+ end
+
+ test 'should not be valid?' do
+ assert ! @response.valid?
+ end
+
+ test 'should have errors' do
+ field_ids = ['field0', 'field1']
+ messages = ['Invalid email address.', 'Field is required.']
+ codes = ['2', '0']
+ assert_equal(field_ids, @response.errors.collect { |e| e.field_id })
+ assert_equal(messages, @response.errors.collect { |e| e.message })
+ assert_equal(codes, @response.errors.collect { |e| e.code })
+ end
+ end
+end

0 comments on commit 263f61c

Please sign in to comment.
Something went wrong with that request. Please try again.