jueves, 29 de noviembre de 2012

Llenando una encuesta en google docs

Este es un script en ruby para llenar encuestas de opcion unica o multiple, usando la gema mechanize.
require 'mechanize'
def rand_array tam # array random de 0 y 1
num = Proc.new{ rand(100)%2 }
ans = Array.new(tam, &num)
end
100.times do # submit 100 encuestas
agent = Mechanize.new
page = agent.get "http://goo.gl/pJjx6" # Encuesta en google docs
############## Checkboxes y Radiobuttons ########
check_names = page.form.checkboxes.collect{ |c| c.name}.uniq
check_names.each do |name|
boxes = page.form.checkboxes_with(name) # opciones de una preg
numbers = rand_array boxes.length
numbers.each_with_index{|i, index| boxes[index].check if i==1 }# marcando, opcion multiple
end
radio_names = page.form.radiobuttons.collect{ |r| r.name}.uniq
radio_names.each do |name|
radios = page.form.radiobuttons_with(name) # opciones de una preg
number = rand radios.length
page.form.radiobuttons_with(name)[number].check # marcando, opcion unica
end
#################################################
page.form.click_button # submit encuesta
agent = nil
end
view raw filling_poll.rb hosted with ❤ by GitHub