Skip to content

Commit

Permalink
google form is now being stripped and displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
twe4ked committed Feb 23, 2010
1 parent c7fbe71 commit c22c38b
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gems
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ inherited_resources --version '1.0.3'
will_paginate --version '>= 2.3.12'
formtastic --version '>= 0.9.7'
haml --version '>= 2.0.0'
nokogiri
21 changes: 21 additions & 0 deletions app/controllers/operate_form_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class OperateFormController < ApplicationController
def show
slug = params[:slug].first
if @google_form = GoogleForm.find_by_slug(slug)
form_html = @google_form.fetch_form_page

doc = Nokogiri::HTML(form_html)
doc.xpath("//*[@style]").remove_attr('style')
doc.xpath("//link").each { |n| n.unlink }
doc.xpath("//style").each { |n| n.unlink }

css_node = doc.create_element('link')
css_node["href"] = "/stylesheets/style.css"
doc.xpath("//head").first.add_child(css_node)
render :text => doc.to_html
else
redirect_to 'http://mocra.com'
end
end

end
2 changes: 2 additions & 0 deletions app/helpers/operate_form_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module OperateFormHelper
end
6 changes: 6 additions & 0 deletions app/models/google_form.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
class GoogleForm < ActiveRecord::Base

def fetch_form_page
form_url = "http://spreadsheets.google.com/viewform?formkey=#{formkey}"
open(form_url).read
end

end
2 changes: 2 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
config.gem 'formtastic', :version => '>= 0.9.7'
config.gem 'will_paginate', :version => '>= 2.3.12'
config.gem 'inherited_resources', :version => '1.0.3'
config.gem 'nokogiri'

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down
1 change: 1 addition & 0 deletions config/initializers/libraries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "open-uri"
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
map.resources :google_forms

map.root :controller => 'home', :action => 'index'

map.catch_all "*slug", :controller => "operate_form", :action => "show"

end
7 changes: 7 additions & 0 deletions features/add_new_google_forms.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@ Feature: Add new google forms
And I press "Create"
Then I should see "You can view the published form here:"

Scenario: Add a new google form by full url
Given I am on the new forms page
When I fill in "Slug" with "railsdev"
And I fill in "Form Key" with "dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA"
And I press "Create"
Then I should see "You can view the published form here:"



3 changes: 3 additions & 0 deletions features/filling_in_form.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Feature: Filling in form
Scenario: Fill in a Google Form without errors
Given I have a Google Form with slug "railsdev"
And I am on "/railsdev"
And I should see no "style" attributes
Then I should see no "style" elements
Then I should see 1 "link" element
When I fill in "Text Question" with "My Answer"
And I press "Submit"
Then I should see "Thanks for your answers."
Expand Down
73 changes: 43 additions & 30 deletions features/fixtures/sample_form.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
<!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">
<br>
<div class="errorbox-good">
<!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">

<br>
<div class="errorbox-good">
<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="0">
<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">
<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> <div class="ss-item ss-grid"><div class="ss-form-entry"><label class="ss-q-title" for="entry_2">Table of questions
</label>
<label class="ss-q-help" for="entry_2"></label>
<div class="errorbox-component errorbox-good"><table border="0" cellpadding="5" cellspacing="0"><thead><tr><td class="ss-gridnumbers" style="width: 24%;"></td>
<td class="ss-gridnumbers" style="width: 6%;"></td>
<td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">1</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">2</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">3</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">4</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">5</label></td>

<td class="ss-gridnumbers" style="width: 6%;"></td></tr></thead>
<tbody><div class="errorbox-good"><tr class="ss-gridrow ss-grid-row-odd"><td class="ss-gridrow ss-leftlabel ss-gridrow-leftlabel" style="width: 24%;">Item 1</td>
<td class="ss-gridrow" style="width: 6%;"></td>
<td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="1" class="ss-q-radio" id="group_3_1"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="2" class="ss-q-radio" id="group_3_2"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="3" class="ss-q-radio" id="group_3_3"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="4" class="ss-q-radio" id="group_3_4"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="5" class="ss-q-radio" id="group_3_5"></td>
<td class="ss-gridrow" style="width: 6%;"></td></tr></div></tbody></table></div></div></div>
<br>
<input type="hidden" name="pageNumber" value="0">
<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;
Expand All @@ -46,11 +58,12 @@
}
}
})();
</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;source=http%253A%252F%252Fspreadsheets.google.com%252Fviewform%253Fformkey%253DdFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA">Report Abuse</a>
</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;source=http%253A%252F%252Fspreadsheets.google.com%252Fviewform%253Fformkey%253DdFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA">Report Abuse</a>
-
<a href="http://www.google.com/accounts/TOS">Terms of Service</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>
73 changes: 43 additions & 30 deletions features/fixtures/sample_form_with_errors.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
<!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>
<!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">
<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> <div class="ss-item ss-grid"><div class="ss-form-entry"><label class="ss-q-title" for="entry_2">Table of questions
</label>
<label class="ss-q-help" for="entry_2"></label>
<div class="errorbox-component errorbox-good"><table border="0" cellpadding="5" cellspacing="0"><thead><tr><td class="ss-gridnumbers" style="width: 24%;"></td>
<td class="ss-gridnumbers" style="width: 6%;"></td>
<td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">1</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">2</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">3</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">4</label></td> <td class="ss-gridnumbers" style="width: 12%;"><label class="ss-gridnumber">5</label></td>

<td class="ss-gridnumbers" style="width: 6%;"></td></tr></thead>
<tbody><div class="errorbox-good"><tr class="ss-gridrow ss-grid-row-odd"><td class="ss-gridrow ss-leftlabel ss-gridrow-leftlabel" style="width: 24%;">Item 1</td>
<td class="ss-gridrow" style="width: 6%;"></td>
<td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="1" class="ss-q-radio" id="group_3_1"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="2" class="ss-q-radio" id="group_3_2"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="3" class="ss-q-radio" id="group_3_3"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="4" class="ss-q-radio" id="group_3_4"></td> <td class="ss-gridrow" style="width: 12%;"><input type="radio" name="entry.3.group" value="5" class="ss-q-radio" id="group_3_5"></td>
<td class="ss-gridrow" style="width: 6%;"></td></tr></div></tbody></table></div></div></div>
<br>
<input type="hidden" name="pageNumber" value="0">
<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;
Expand All @@ -46,11 +58,12 @@
}
}
})();
</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>
</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/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>
12 changes: 11 additions & 1 deletion features/step_definitions/google_forms_steps.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
Given /^I have a Google Form with slug "([^\"]*)"$/ do |slug|
pending # express the regexp above with the code you wish you had
steps <<-CUCUMBER
Given I am on the new forms page
When I fill in "Slug" with "#{slug}"
And I fill in "Form Key" with "dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA"
And I press "Create"
Then I should see "You can view the published form here:"
CUCUMBER

sample_form = File.read(File.join(Rails.root, "features", "fixtures", "sample_form.html"))

FakeWeb.register_uri(:get, "http://spreadsheets.google.com/viewform?formkey=dFRUNHpLZmZHbVRrdlpMRnlJclBLc0E6MA", :body => sample_form)
end

15 changes: 15 additions & 0 deletions features/step_definitions/web_steps_ext.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Then /^I should see (no|\d+) "([^\"]*)" elements?$/ do |count, element|
if count == "no"
page.should_not have_xpath("//#{element}")
else
count = count.to_i
page.should have_xpath("//#{element}")
doc = Nokogiri::HTML(page.body)
doc.xpath("//#{element}").length.should == count
end
end

Then /^I should see no "([^\"]*)" attributes$/ do |attribute|
page.should_not have_xpath("//*[@#{attribute}]")
end

2 changes: 1 addition & 1 deletion features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# order to ease the transition to Capybara we set the default here. If you'd
# prefer to use XPath just remove this line and adjust any selectors in your
# steps to use the XPath syntax.
Capybara.default_selector = :css
Capybara.default_selector = :xpath

# If you set this to false, any error raised from within your app will bubble
# up to your step definition and out to cucumber unless you catch it somewhere
Expand Down
3 changes: 2 additions & 1 deletion features/support/paths.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def path_to(page_name)
'/'
when /the new forms page/
new_google_form_path

when /"(.*)"/
$1
# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
Expand Down
3 changes: 3 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ html[xmlns] .clearfix{display:block}
* html .clearfix{height:1%}
sup,sub{vertical-align:baseline}

/* Enriching features for awesome browers. */
@import url('enrichment.css');

/* Main typographic styles */
body{font:normal 14px/140% Helvetica,Arial,sans-serif;color:#333;background-color:#c0cacc;}
p{margin:10px 0 20px;}
Expand Down
2 changes: 1 addition & 1 deletion rerun.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@

features/filling_in_form.feature:6
17 changes: 17 additions & 0 deletions spec/controllers/operate_form_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe OperateFormController do

#Delete these examples and add some real ones
it "should use OperateFormController" do
controller.should be_an_instance_of(OperateFormController)
end


describe "GET 'show'" do
it "should be successful" do
get 'show'
response.should be_success
end
end
end
11 changes: 11 additions & 0 deletions spec/helpers/operate_form_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'

describe OperateFormHelper do

#Delete this example and add some real ones or delete this file
it "should be included in the object returned by #helper" do
included_modules = (class << helper; self; end).send :included_modules
included_modules.should include(OperateFormHelper)
end

end

0 comments on commit c22c38b

Please sign in to comment.