Permalink
Browse files

Refactored to now use client and submission so that other wufoo api p…

…ieces can be built into this gem.
  • Loading branch information...
1 parent 7eefc74 commit e5822a261a7557556c832b3503fd57ee6d9fb6a0 @jnunemaker committed Dec 16, 2008
Showing with 305 additions and 237 deletions.
  1. +32 −0 examples/submission.rb
  2. +5 −85 lib/wufoo.rb
  3. +18 −0 lib/wufoo/client.rb
  4. +81 −0 lib/wufoo/submission.rb
  5. +1 −1 lib/wufoo/version.rb
  6. +9 −0 test/test_client.rb
  7. +28 −1 test/test_helper.rb
  8. +129 −0 test/test_submission.rb
  9. +2 −150 test/test_wufoo.rb
@@ -0,0 +1,32 @@
+require 'yaml'
+require File.dirname(__FILE__) + '/../lib/wufoo'
+config = YAML::load(File.read(ENV['HOME'] + '/.wufoo'))
+
+client = Wufoo::Client.new('http://orderedlist.wufoo.com', config['api_key'])
+submission = Wufoo::Submission.new(client, 'orderedlistcom-project-request')
+
+response = submission.add_params({
+ '1' => 'John Nunemaker',
+ '12' => 'nunemaker@gmail.com',
+ '4' => '1231231234',
+ '5' => 'http://addictedtonew.com',
+ '6' => 'ASAP!',
+ '10' => '1,000,000',
+ '11' => 'My cool project!',
+}).process
+
+if response.success?
+ puts response.message
+else
+ # Something was wrong with the request
+ # (missing api key, invalid form, etc)
+ if response.fail?
+ puts response.error
+ end
+
+ # validation errors
+ unless response.valid?
+ errors = response.errors.collect { |e| "#{e.field_id} (#{e.code}): #{e.message}" }
+ puts errors * "\n"
+ end
+end
View
@@ -1,89 +1,9 @@
+$:.unshift File.dirname(__FILE__)
+
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
+require 'wufoo/client'
+require 'wufoo/submission'
+require 'wufoo/version'
View
@@ -0,0 +1,18 @@
+module Wufoo
+ class Client
+ include HTTParty
+
+ attr_accessor :url, :api_key
+
+ def initialize(url, api_key)
+ @url, @api_key = url, api_key
+ end
+
+ def post(path, data)
+ data.merge!({
+ :w_api_key => api_key,
+ })
+ self.class.post("#{@url}#{path}", :query => data, :format => :json)
+ end
+ end
+end
@@ -0,0 +1,81 @@
+module Wufoo
+ class Submission
+ attr_accessor :client, :form, :params
+
+ def initialize(client, form, params={})
+ @client = client
+ @form = form
+ @params = {}.merge(params || {})
+ end
+
+ def add_params(new_params)
+ @params.merge!(new_params)
+ self
+ end
+
+ def process
+ Response.new(@client.post('/api/insert/', params.merge({:w_form => form})))
+ 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
+end
@@ -1,3 +1,3 @@
-class Wufoo
+module Wufoo
Version = '0.1.1'
end
View
@@ -0,0 +1,9 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class TestClient < Test::Unit::TestCase
+ test 'initialize' do
+ client = Wufoo::Client.new('http://foobar.wufoo.com', 'somecrazyapikey')
+ assert_equal('http://foobar.wufoo.com', client.url)
+ assert_equal('somecrazyapikey', client.api_key)
+ end
+end
View
@@ -2,4 +2,31 @@
require 'test/unit'
require 'context'
require 'stump'
-require File.join(File.dirname(__FILE__), '..', 'lib', 'wufoo')
+require File.join(File.dirname(__FILE__), '..', 'lib', 'wufoo')
+
+def successful_response_data
+ {"wufoo_submit" => [{
+ "confirmation_message" => "Success! Thanks for filling out my form!",
+ "entry_id" => "1025",
+ "success" => "true"
+ }]}
+end
+
+def error_response_data
+ {"wufoo_submit" => [{
+ "error" => "The supplied form URL was not found.",
+ "field_errors" => [],
+ "success" => "false"
+ }]}
+end
+
+def 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
Oops, something went wrong.

0 comments on commit e5822a2

Please sign in to comment.