Permalink
Browse files

form action is rewritten to post back to our server

  • Loading branch information...
1 parent 1a6d915 commit d80a3ae9dbd2849631280dac9070506e843988ab @drnic drnic committed Feb 23, 2010
@@ -3,7 +3,7 @@
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
- protect_from_forgery # See ActionController::RequestForgeryProtection for details
+ # protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
@@ -13,6 +13,10 @@ def show
# add 'clearfix' to wrap
doc.xpath("//*[@class='ss-form-container']").add_class('clearfix');
+ google_form = doc.xpath("//form").first
+ google_form_action = google_form["action"]
+ google_form["action"] = submit_operate_form_url(:id => @google_form.id, :google_form => google_form_action)
+
css_node = doc.create_element('link')
css_node["href"] = "/stylesheets/style.css"
css_node["rel"] = "stylesheet"
@@ -23,5 +27,18 @@ def show
redirect_to 'http://mocra.com'
end
end
-
+
+ def update
+ if @google_form = GoogleForm.find(params[:id])
+ params.delete(:action)
+ params.delete(:controller)
+ google_form_action = params.delete(:google_form)
+ result_html = @google_form.submit(google_form_action, params)
+ if result_html =~ %r{<title>Thanks!<\/title>}
+ render :text => "Thanks for your answers."
+ else
+ show
+ end
+ end
+ end
end
@@ -5,4 +5,9 @@ def fetch_form_page
open(form_url).read
end
+ def submit(google_form_action, params)
+ res = Net::HTTP.post_form(URI.parse(google_form_action), params)
+ res.body
+ end
+
end
View
@@ -3,6 +3,7 @@
map.root :controller => 'home', :action => 'index'
+ map.submit_operate_form "operate_form/:id", :controller => "operate_form", :action => "update"
map.catch_all "*slug", :controller => "operate_form", :action => "show"
end
@@ -5,6 +5,7 @@ Feature: Filling in form
Scenario: Fill in a Google Form without errors
Given I have a Google Form with slug "railsdev"
+ And I expect Google Form POST to be successful
And I am on "/railsdev"
And I should see no "style" attributes
And I should see no "ss-legal" classes
@@ -1,56 +1,14 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><link rel="shortcut icon" href="http://www.google.com/images/spreadsheets/forms/favicon.ico" type="image/x-icon">
-<meta http-equiv="Content-type" content="text/html; charset=utf-8">
-<title>Sample form for Testing</title>
-<link href='//spreadsheets.google.com/client/css/3386678453-published_form_compiled.css' type='text/css' rel='stylesheet'><link href='//spreadsheets.google.com/client/css/3386678453-published_form_compiled.css' type='text/css' rel='stylesheet'>
-</head>
-<body class="ss-base-body" dir="ltr"><div class="ss-form-container">
-<div class="ss-form-heading"><h1 class="ss-form-title">Sample form for Testing</h1>
-<p></p>
-
-
-<hr class="ss-email-break" style="display:none;">
-<div class="ss-required-asterisk">* Required</div></div>
-<div class="ss-form"><form action="http://spreadsheets.google.com/formResponse?formkey=dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA&amp;ifq" method="POST" id="ss-form"><div class="errorheader"><b>Looks like you have a question or two that still needs to be filled out.</b></div>
-
-<br>
-<div class="errorbox-bad"><div class="errormsg"></div>
-<div class="ss-item ss-item-required ss-text"><div class="ss-form-entry"><label class="ss-q-title" for="entry_0">Text Question
-<span class="ss-required-asterisk">*</span></label>
-<label class="ss-q-help" for="entry_0">Text Question Help Text</label>
-<input type="text" name="entry.0.single" value="" class="ss-q-short" id="entry_0"></div></div></div>
-<br>
-<input type="hidden" name="pageNumber" value="">
-<input type="hidden" name="backupCache" value="">
-<div class="ss-item ss-navigate"><div class="ss-form-entry">
-<input type="submit" name="submit" value="Submit"></div></div></form>
-<script type="text/javascript">
-
- (function() {
-var error = false;
-var divs = document.getElementById('ss-form').
-getElementsByTagName('div');
-for (var j in divs) {
-if (divs[j].className == 'errorbox-bad') {
-error = true;
-divs[j].lastChild.firstChild.lastChild.focus();
-break;
-}
-}
-if (!error) {
-for (var i in divs) {
-if (divs[i].className == 'ss-form-entry' && divs[i].
-firstChild.className == 'ss-q-title') {
-divs[i].lastChild.focus();
-break;
-}
-}
-}
-})();
- </script></div>
-<div class="ss-footer"><div class="ss-attribution"></div>
-<div class="ss-legal"><span class="ss-powered-by">Powered by <a href="http://docs.google.com">Google Docs</a></span>
-<span class="ss-terms"><small><a href="http://spreadsheets.google.com/reportabuse?formkey=dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA&amp;ifq&amp;source=http%253A%252F%252Fspreadsheets.google.com%252FformResponse%253Fformkey%253DdFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA%2526ifq">Report Abuse</a>
--
-<a href="http://www.google.com/accounts/TOS">Terms of Service</a>
--
-<a href="http://www.google.com/google-d-s/terms.html">Additional Terms</a></small></span></div></div></div></body></html>
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><meta http-equiv="Content-type" content="text/html; charset=utf-8">
+<title>Thanks!</title>
+<link href='//spreadsheets.google.com/client/css/2573608412-form_response_compiled.css' type='text/css' rel='stylesheet'>
+<style type="text/css">
+ .ss-logo {background:transparent url("http://www.google.com/images/spreadsheets/forms/sprites_2.png") no-repeat}
+ </style></head>
+<body><div class="ss-container"><div class="ss-resp-card"><div class="ss-custom-resp">Thanks!<br /><br />Your response will now appear in my spreadsheet.</div>
+<p></p>
+
+<div class="ss-footer-links"><a class="ss-bottom-link" href="http://spreadsheets.google.com/viewform?formkey=dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA&amp;ifq">Go back to the form</a>
+<span class="ss-bottom-link">|</span>
+<a class="ss-bottom-link" href="http://spreadsheets.google.com/newform">Create your own form</a></div>
+<hr class="ss-break">
+<div class="ss-footer"><a class="ss-logo-link" href="http://docs.google.com/"><div class="ss-logo">&nbsp;&nbsp;</div></a></div></div></div></body></html>
@@ -12,3 +12,8 @@
FakeWeb.register_uri(:get, "http://spreadsheets.google.com/viewform?formkey=dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA", :body => sample_form)
end
+
+Given /^I expect Google Form POST to be successful/ do
+ thankyou = File.read(File.join(Rails.root, "features", "fixtures", "sample_form_thanks.html"))
+ FakeWeb.register_uri(:post, "http://spreadsheets.google.com/formResponse", :body => thankyou)
+end
View
@@ -1 +1 @@
-features/filling_in_form.feature:6
+

0 comments on commit d80a3ae

Please sign in to comment.