Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 6e809288aafe3e0c021d82524a87dc39f492149e 0 parents
@pengwynn authored
5 .document
@@ -0,0 +1,5 @@
+README.rdoc
+lib/**/*.rb
+bin/*
+features/**/*.feature
+LICENSE
24 .gitignore
@@ -0,0 +1,24 @@
+## MAC OS
+.DS_Store
+
+*.gem
+dist
+
+## TEXTMATE
+*.tmproj
+tmtags
+
+## EMACS
+*~
+\#*
+.\#*
+
+## VIM
+*.swp
+
+## PROJECT::GENERAL
+coverage
+rdoc
+pkg
+
+## PROJECT::SPECIFIC
10 Gemfile
@@ -0,0 +1,10 @@
+source :gemcutter
+gem "hashie", ">= 0.1.3"
+gem "httparty", ">= 0.1.0"
+
+group :development do
+ gem "shoulda", ">= 2.10.1"
+ gem "matchy", "= 0.4.0"
+ gem "fakeweb", ">= 1.2.5"
+ gem "yard", ">= 0"
+end
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Wynn Netherland
+
+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.
17 README.md
@@ -0,0 +1,17 @@
+# Formstack
+
+Description goes here.
+
+## Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Copyright
+
+Copyright (c) 2010 Wynn Netherland. See LICENSE for details.
13 Rakefile
@@ -0,0 +1,13 @@
+require "mg"
+
+MG.new("formstack.gemspec")
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.ruby_opts = ['-rubygems'] if defined? Gem
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/*_test.rb'
+end
+
+desc "Run tests"
+task :default => :test
1  VERSION
@@ -0,0 +1 @@
+0.0.1
5 changelog.md
@@ -0,0 +1,5 @@
+# Changelog
+
+## 0.0.1 - May 23, 2010
+
+* Initial version
26 formstack.gemspec
@@ -0,0 +1,26 @@
+require 'bundler'
+require 'bundler/version'
+require 'lib/formstack'
+
+Gem::Specification.new do |s|
+ s.name = %q{formstack}
+ s.version = Formstack::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.required_rubygems_version = ">= 1.3.6"
+ s.authors = ["Wynn Netherland"]
+ s.date = %q{2010-05-23}
+ s.description = %q{Wrapper for the Formstack API}
+ s.email = %q{wynn.netherland@gmail.com}
+ s.files = Dir.glob("{lib}/**/*")
+ s.homepage = %q{http://github.com/pengwynn/formstack}
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.6}
+ s.summary = %q{Wrapper for the Formstack API}
+ s.test_files = [
+ "test/helper.rb",
+ "test/formstack_test.rb"
+ ]
+
+ s.add_bundler_dependencies
+end
+
23 lib/formstack.rb
@@ -0,0 +1,23 @@
+require 'httparty'
+require 'hashie'
+
+directory = File.expand_path(File.dirname(__FILE__))
+
+Hash.send :include, Hashie::HashExtensions
+
+module Formstack
+
+ VERSION = "0.0.1".freeze
+
+
+ class FormstackError < StandardError
+ attr_reader :data
+
+ def initialize(data)
+ @data = data
+ super
+ end
+ end
+end
+
+require File.join(directory, 'formstack', 'client')
82 lib/formstack/client.rb
@@ -0,0 +1,82 @@
+module Formstack
+ class Client
+ include HTTParty
+ base_uri "https://www.formstack.com/api/"
+ format :json
+
+ def initialize(key)
+ self.class.default_params(:api_key => key, :type => 'json')
+ end
+
+ def forms
+ self.class.get("/forms").forms.map{|f| handle_form(f)}
+ end
+
+ def form(form_id)
+ handle_form(self.class.get("/form", :query => {:id => form_id}))
+ end
+
+ def data(form_id, options={})
+ api_response = self.class.get("/data", :query => {:id => form_id}.merge(options))
+ api_response.submissions = api_response.submissions.map{|s| handle_submission(s) }
+ api_response
+ end
+ alias :submissions :data
+
+ def submission(submission_id, options={})
+ handle_submission(self.class.get("/submission", :query => {:id => submission_id}.merge(options)))
+ end
+
+
+ def submit(form_id, options={})
+ self.class.post("/submit", :body => {:id => form_id}.merge(prepare_params(form_id, options)))['id']
+ end
+
+ def edit(submission_id, options={})
+ self.class.post("/edit", :body => {:id => submission_id}.merge(prepare_params(submission_id, options)))['id']
+ end
+
+ def delete(submission_id)
+ self.class.post("/delete", :body => {:id => submission_id})['id']
+ end
+
+
+ def prepare_params(form_id, params={})
+ data = params.delete(:data)
+ form = form(form_id)
+ data.each do |field, value|
+ field_id = form.fields.any? {|f| f['name'] == field.to_s} ? form.fields.find {|f| f['name'] == field.to_s}['id'] : field
+ params["field_#{field_id}"] = value
+ end
+ params
+ end
+
+ def handle_form(form)
+ form.created = Time.parse(form.created) unless form.created.nil?
+ form
+ end
+
+ def handle_submission(submission)
+ data = submission.data
+ data.each do |datum|
+ submission[datum.field] = datum.value
+ end
+ submission.timestamp = Time.parse(submission.timestamp)
+ submission
+ end
+
+ def self.get(*args); handle_response super end
+ def self.post(*args); handle_response super end
+
+ def self.handle_response(response)
+ case response.code
+ when 500...600; raise FormstackError.new(Hashie::Mash.new(response).error)
+ else; response
+ end
+
+ Hashie::Mash.new(response).response
+
+ end
+
+ end
+end
35 test/fixtures/data.json
@@ -0,0 +1,35 @@
+{
+ "status": "ok",
+ "response": {
+ "submissions": [{
+ "id": "1001",
+ "timestamp": "2007-01-01 01:01:01",
+ "user_agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
+ "remote_addr": "127.0.0.1",
+ "data": [{
+ "field": "1111",
+ "value": "John Smith"
+ },
+ {
+ "field": "2222",
+ "value": "Apple"
+ }]
+ },
+ {
+ "id": "1001",
+ "timestamp": "2007-02-02 02:02:02",
+ "user_agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
+ "remote_addr": "127.0.0.1",
+ "data": [{
+ "field": "1111",
+ "value": "Jane Smith"
+ },
+ {
+ "field": "2222",
+ "value": "Banana"
+ }]
+ }],
+ "total": 2,
+ "pages": 1
+ }
+}
114 test/fixtures/form.json
@@ -0,0 +1,114 @@
+{
+ "status": "ok",
+ "response": {
+ "name": "Contact",
+ "views": "3",
+ "created": "2009-10-18 14:04:04",
+ "submissions": 0,
+ "url": "http:\/\/www.formstack.com\/forms\/?730120-ALeOPeiBLC",
+ "data_url": "",
+ "summary_url": "",
+ "rss_url": "",
+ "encrypted": false,
+ "thumbnail_url": "https:\/\/formstack.com\/admin\/images\/user\/form_thumbnail_730120_4adb59354915e.png",
+ "submit_button_title": null,
+ "inactive": false,
+ "javascript": "&lt;script type=&quot;text\/javascript&quot; src=&quot;http:\/\/www.formstack.com\/forms\/js.php?730120-ALeOPeiBLC-v2&quot;&gt;&lt;\/script&gt;&lt;noscript&gt;&lt;a href=&quot;http:\/\/www.formstack.com\/forms\/?730120-ALeOPeiBLC&quot; title=&quot;Online Form&quot;&gt;Online Form - Contact&lt;\/a&gt;&lt;\/noscript&gt;&lt;div style=&quot;text-align:right; font-size:x-small;&quot;&gt;&lt;a href=&quot;http:\/\/www.formstack.com\/&quot; title=&quot;Online Form Maker&quot;&gt;Formstack - Online Form Maker&lt;\/a&gt;&lt;\/div&gt;",
+ "html": "<?xml version=\"1.0\"?>\n<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\">\n<html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\" xml:lang=\"en\" lang=\"en\">\n<head>\n <title>Contact<\/title>\n <meta http-equiv=\"Content-Type\" content=\"text\/html;charset=utf-8\" \/>\n <script type=\"text\/javascript\" src=\"http:\/\/www.formstack.com\/forms\/js\/2\/yui.js\">\n<\/script>\n<script type=\"text\/javascript\" src=\"http:\/\/www.formstack.com\/forms\/js\/2\/scripts.js\">\n<\/script>\n<link rel=\"stylesheet\" type=\"text\/css\" href=\"http:\/\/www.formstack.com\/forms\/css\/2\/form.css\" media=\"all\" \/>\n<link rel=\"stylesheet\" type=\"text\/css\" href=\"http:\/\/www.formstack.com\/forms\/css\/2\/calendar.css\" media=\"all\" \/>\n<!--[if IE 6]>\n<link rel=\"stylesheet\" type=\"text\/css\" href=\"http:\/\/www.formstack.com\/forms\/css\/2\/ie6fixes.css\" \/>\n<![endif]-->\n<\/head>\n<body>\n <form method=\"post\" enctype=\"multipart\/form-data\" action=\"http:\/\/www.formstack.com\/forms\/index.php\" class=\"fsForm fsSingleColumn\" id=\"fsForm730120\">\n <input type=\"hidden\" name=\"form\" value=\"730120\" \/>\n <input type=\"hidden\" name=\"viewkey\" value=\"ALeOPeiBLC\" \/>\n <input type=\"hidden\" name=\"hidden_fields\" id=\"hidden_fields730120\" value=\"\" \/>\n <input type=\"hidden\" name=\"_submit\" value=\"1\" \/>\n <input type=\"hidden\" name=\"incomplete\" id=\"incomplete730120\" value=\"\" \/>\n <div id=\"requiredFieldsError\" style=\"display:none;\">Please fill in a valid value for all required fields<\/div>\n <div id=\"resumeConfirm\" style=\"display:none;\">Are you sure you want to leave this form and resume later?<\/div>\n <div id=\"fileTypeAlert\" style=\"display:none;\">You must upload one of the following file types for the selected field:<\/div>\n <div id=\"embedError\" style=\"display:none;\">There was an error displaying the form. Please copy and paste the embed code again.<\/div>\n <div class=\"fsPage\" id=\"fsPage730120-1\">\n <table class=\"fsSection fsTable\" id=\"fsSection7344053\" cellspacing=\"0\" cellpadding=\"0\">\n <tbody>\n <tr valign=\"top\" class=\"fsRowTop\">\n <td class=\"fsRowOpen\">&nbsp;<\/td>\n <td colspan=\"1\" class=\"fsRowBody\">\n <\/td>\n <td class=\"fsRowClose\">&nbsp;<\/td>\n <\/tr>\n <tr valign=\"top\" id=\"fsRow730120-1\" class=\"fsRow fsSectionRow\">\n <td class=\"fsRowOpen\">&nbsp;<\/td>\n <td colspan=\"1\" class=\"fsRowBody fsCell fsSectionCell\" id=\"fsCell7344053\">\n <div class=\"FSSectionTop\">\n <\/div>\n <h2 class=\"fsSectionHeading\">\n Contact Information\n <\/h2>\n <div class=\"fsSectionText\">\n Please fill out your contact information below.\n <\/div>\n <div class=\"FSSectionBottom\">\n <\/div>\n <\/td>\n <td class=\"fsRowClose\">&nbsp;<\/td>\n <\/tr>\n <tr valign=\"top\" id=\"fsRow730120-2\" class=\"fsRow fsFieldRow fsLastRow\" style=\"\">\n <td class=\"fsRowOpen\">&nbsp;<\/td>\n <td class=\"fsRowBody fsCell fsFieldCell fsFirst fsLast fsLabelVertical \" colspan=\"1\" id=\"fsCell7344054\">\n <label class=\"fsLabel fsRequiredLabel\" for=\"field7344054\">Name<span>*<\/span>\n <\/label>\n <div class=\"fsSubFieldGroup\">\n <div class=\"fsSubField\">\n <input type=\"text\" id=\"field7344054-first\" name=\"field7344054-first\" size=\"20\" value=\"\" class=\"fsField fsFieldName fsRequired\" \/>\n <br \/>\n <label class=\"fsSupporting\" for=\"field7344054-first\">First Name<\/label>\n <\/div>\n <div class=\"fsSubField\">\n <input type=\"text\" id=\"field7344054-last\" name=\"field7344054-last\" size=\"20\" value=\"\" class=\"fsField\" \/>\n <br \/>\n <label class=\"fsSupporting\" for=\"field7344054-last\">Last Name<\/label>\n <\/div>\n <\/div>\n <div class=\"clear\">\n <\/div>\n <\/td>\n <td class=\"fsRowClose\">&nbsp;<\/td>\n <\/tr>\n <tr valign=\"top\" id=\"fsRow730120-3\" class=\"fsRow fsFieldRow fsLastRow\" style=\"\">\n <td class=\"fsRowOpen\">&nbsp;<\/td>\n <td class=\"fsRowBody fsCell fsFieldCell fsFirst fsLast fsLabelVertical \" colspan=\"1\" id=\"fsCell7344057\">\n <label class=\"fsLabel\" for=\"field7344057\">Phone<\/label>\n <input type=\"text\" id=\"field7344057\" name=\"field7344057\" size=\"15\" value=\"\" class=\"fsField fsFormatPhoneUS \" \/>\n <\/td>\n <td class=\"fsRowClose\">&nbsp;<\/td>\n <\/tr>\n <tr valign=\"top\" id=\"fsRow730120-4\" class=\"fsRow fsFieldRow fsLastRow\" style=\"\">\n <td class=\"fsRowOpen\">&nbsp;<\/td>\n <td class=\"fsRowBody fsCell fsFieldCell fsFirst fsLast fsLabelVertical \" colspan=\"1\" id=\"fsCell7344058\">\n <label class=\"fsLabel fsRequiredLabel\" for=\"field7344058\">Your email<span>*<\/span>\n <\/label>\n <input type=\"text\" id=\"field7344058\" name=\"field7344058\" size=\"50\" value=\"\" class=\"fsField fsFormatEmail fsRequired\" \/>\n <\/td>\n <td class=\"fsRowClose\">&nbsp;<\/td>\n <\/tr>\n <tr valign=\"top\" id=\"fsRow730120-5\" class=\"fsRow fsFieldRow fsLastRow\" style=\"\">\n <td class=\"fsRowOpen\">&nbsp;<\/td>\n <td class=\"fsRowBody fsCell fsFieldCell fsFirst fsLast fsLabelVertical \" colspan=\"1\" id=\"fsCell7344059\">\n <label class=\"fsLabel\" for=\"field7344059\">What's on your mind?<\/label>\n <textarea id=\"field7344059\" name=\"field7344059\" rows=\"10\" cols=\"50\" class=\"fsField \">\n <\/textarea>\n <\/td>\n <td class=\"fsRowClose\">&nbsp;<\/td>\n <\/tr>\n <tr valign=\"top\" class=\"fsSpacerRow\">\n <td class=\"fsRowOpen\">&nbsp;<\/td>\n <td colspan=\"1\" class=\"fsRowBody\">\n <\/td>\n <td class=\"fsRowClose\">&nbsp;<\/td>\n <\/tr>\n <tr valign=\"top\" class=\"fsRowBottom\">\n <td class=\"fsRowOpen\">&nbsp;<\/td>\n <td colspan=\"1\" class=\"fsRowBody\">\n <\/td>\n <td class=\"fsRowClose\">&nbsp;<\/td>\n <\/tr>\n <\/tbody>\n<\/table>\n<\/div>\n<div id=\"fsSubmit730120\" class=\"fsSubmit fsPagination\">\n <input id=\"fsSubmitButton730120\" class=\"fsSubmitButton\" type=\"submit\" value=\"Submit Form\" onclick=\"form730120.submitForm(); return false;\" \/>\n<\/div>\n<script type=\"text\/javascript\">\n<!--\nvar form730120;\nvar form730120Load = function() {\nform730120 = new FSForm(730120);\nform730120.logicFields = [];\nform730120.calcFields = [];\nform730120.lastPage = 1;\nform730120.init();\n};\nvar oldonload730120 = window.onload;\nwindow.onload = typeof window.onload == 'function' ?\nfunction() { oldonload730120(); form730120Load(); } :\nwindow.onload = form730120Load;\n\/\/-->\n<\/script>\n<\/form>\n<img src=\"http:\/\/www.formstack.com\/forms\/count.php?730120\" alt=\"Form View Counter\" width=\"1\" height=\"1\" border=\"0\" \/>\n<p style=\"display:block;width:128px;height:48px;margin:18px auto;\">\n <a href=\"http:\/\/www.formstack.com\/?fa=f,730120\" target=\"_blank\" style=\"display:block;width:128px;height:48px;\" title=\"Powered by Formstack Online Forms\">\n <img src=\"http:\/\/www.formstack.com\/forms\/images\/2\/poweredByFormstack.png\" alt=\"Formstack Online Forms\" style=\"border:0; vertical-align: text-top;\" \/>\n <span style=\"display:none;\">Formstack Online Forms<\/span>\n<\/a>\n<\/p>\n<\/body>\n<\/html>\n",
+ "fields": [{
+ "id": "7344053",
+ "label": "",
+ "hide_label": "0",
+ "description": null,
+ "name": "",
+ "type": "section",
+ "options": "",
+ "default": "",
+ "required": "0",
+ "hidden": "0",
+ "readonly": "0",
+ "logic": "",
+ "calculations": "",
+ "text_maxlength": null,
+ "label_position": "default",
+ "num_columns": "0",
+ "section_break": "0",
+ "section_heading": "Contact Information",
+ "section_text": "Please fill out your contact information below."
+ },
+ {
+ "id": "7344054",
+ "label": "Name",
+ "hide_label": "0",
+ "description": null,
+ "name": "name",
+ "type": "name",
+ "options": "",
+ "default": "",
+ "required": "1",
+ "hidden": "0",
+ "readonly": "0",
+ "logic": "",
+ "calculations": "",
+ "text_maxlength": null,
+ "show_initial": "",
+ "show_middle": "",
+ "show_prefix": "",
+ "show_suffix": "",
+ "text_size": "20"
+ },
+ {
+ "id": "7344057",
+ "label": "Phone",
+ "hide_label": "0",
+ "description": "",
+ "name": "phone",
+ "type": "phone",
+ "options": "",
+ "default": "",
+ "required": "0",
+ "hidden": "0",
+ "readonly": "0",
+ "logic": "",
+ "calculations": "",
+ "text_maxlength": null,
+ "format": "US",
+ "text_size": "15"
+ },
+ {
+ "id": "7344058",
+ "label": "Your email",
+ "hide_label": "0",
+ "description": "",
+ "name": "your_email",
+ "type": "email",
+ "options": "",
+ "default": "",
+ "required": "1",
+ "hidden": "0",
+ "readonly": "0",
+ "logic": "",
+ "calculations": "",
+ "text_maxlength": "",
+ "text_size": "50"
+ },
+ {
+ "id": "7344059",
+ "label": "What's on your mind?",
+ "hide_label": "0",
+ "description": "",
+ "name": "whats_on_your_mind",
+ "type": "textarea",
+ "options": "",
+ "default": "",
+ "required": "0",
+ "hidden": "0",
+ "readonly": "0",
+ "logic": "",
+ "calculations": "",
+ "text_maxlength": "",
+ "rows": "10",
+ "cols": "50"
+ }]
+ }
+}
21 test/fixtures/forms.json
@@ -0,0 +1,21 @@
+{
+ "status": "ok",
+ "response": {
+ "forms": [{
+ "id": "730120",
+ "name": "Contact",
+ "views": "3",
+ "created": "2009-10-18 14:04:04",
+ "submissions": 0,
+ "url": "http:\/\/www.formstack.com\/forms\/?730120-ALeOPeiBLC",
+ "data_url": "",
+ "summary_url": "",
+ "rss_url": "",
+ "encrypted": false,
+ "thumbnail_url": "https:\/\/formstack.com\/admin\/images\/user\/form_thumbnail_730120_4adb59354915e.png",
+ "submit_button_title": null,
+ "inactive": false,
+ "permissions": "300"
+ }]
+ }
+}
17 test/fixtures/submission.json
@@ -0,0 +1,17 @@
+{
+ "status": "ok",
+ "response": {
+ "id": "1001",
+ "timestamp": "2007-01-01 01:01:01",
+ "user_agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)",
+ "remote_addr": "127.0.0.1",
+ "data": [{
+ "field": "1111",
+ "value": "John Smith"
+ },
+ {
+ "field": "2222",
+ "value": "Apple"
+ }]
+ }
+}
6 test/fixtures/submit.json
@@ -0,0 +1,6 @@
+{
+ "status": "ok",
+ "response": {
+ "id": 10001
+ }
+}
69 test/formstack_test.rb
@@ -0,0 +1,69 @@
+require 'helper'
+
+class FormstackTest < Test::Unit::TestCase
+
+ context "Formstack API" do
+
+ setup do
+ @client = Formstack::Client.new("OU812")
+ end
+
+ should "list forms for an account" do
+ stub_get("/forms?type=json&api_key=OU812", "forms.json")
+ forms = @client.forms
+ forms.size.should == 1
+ forms.first.name.should == 'Contact'
+ end
+
+ should "get details for a form" do
+ stub_get("/form?id=1234&type=json&api_key=OU812", "form.json")
+ form = @client.form(1234)
+ form.fields.size.should == 5
+ form.fields.first.section_heading.should == 'Contact Information'
+ end
+
+ should "get submitted data for a form" do
+ stub_get("/data?id=1234&page=1&type=json&api_key=OU812", "data.json")
+ submissions = @client.data(1234, :page => 1).submissions
+ submissions.size.should == 2
+ submissions.first['1111'].should == "John Smith"
+ submissions.first.timestamp.year.should == 2007
+ end
+
+ should "return a single submission collected for a form" do
+ stub_get("/submission?id=1001&type=json&api_key=OU812", "submission.json")
+ submission = @client.submission(1001)
+ submission['2222'].should == "Apple"
+ submission.timestamp.year.should == 2007
+ end
+
+ should "prepare data for form submission" do
+ stub_get("/form?id=1234&type=json&api_key=OU812", "form.json")
+ params = {:page => 3, :data => {:name => "Wynn Netherland", :phone => "940-867-5309", '123' => 'foo'}}
+ prepared = @client.prepare_params(1234, params)
+ prepared['field_7344054'].should == 'Wynn Netherland'
+ prepared['field_7344057'].should == '940-867-5309'
+ prepared['field_123'].should == 'foo'
+ end
+
+ should "submit data for a form" do
+ stub_post("/submit?type=json&api_key=OU812", "submit.json")
+ id = @client.submit(1234, :data => {:foo => 'bar'})
+ id.should == 10001
+ end
+
+ should "update submitted data for a form" do
+ stub_post("/edit?type=json&api_key=OU812", "submit.json")
+ id = @client.edit(1234, :data => {:foo => 'bar'})
+ id.should == 10001
+ end
+
+ should "delete submitted data for a form" do
+ stub_post("/delete?type=json&api_key=OU812", "submit.json")
+ id = @client.delete(1234)
+ id.should == 10001
+ end
+
+ end
+
+end
43 test/helper.rb
@@ -0,0 +1,43 @@
+require 'rubygems'
+require 'test/unit'
+require 'shoulda'
+
+require 'redgreen'
+require 'matchy'
+require 'fakeweb'
+
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'formstack'
+
+
+# Set the default allow_net_connect option--usually you'll want this off.
+# You don't usually want your test suite to make HTTP connections, do you?
+FakeWeb.allow_net_connect = false
+
+class Test::Unit::TestCase
+end
+
+def fixture_file(filename)
+ return '' if filename == ''
+ file_path = File.expand_path(File.dirname(__FILE__) + '/fixtures/' + filename)
+ File.read(file_path)
+end
+
+def formstack_url(url, options={})
+ url =~ /^http/ ? url : "https://www.formstack.com/api#{url}"
+end
+
+def stub_request(method, url, filename, status=nil)
+ options = {:body => ""}
+ options.merge!({:body => fixture_file(filename)}) if filename
+ options.merge!({:body => status.last}) if status
+ options.merge!({:status => status}) if status
+
+ FakeWeb.register_uri(method, formstack_url(url), options)
+end
+
+def stub_get(*args); stub_request(:get, *args) end
+def stub_post(*args); stub_request(:post, *args) end
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.