Form Filler

E

eastcoastcoder

Is there an equivalent in Ruby to the Perl modules which can "fill out
an html/http form"?

Given a hash, I'd like to either get the query string to append to the
URL for GET's, or the POST contents to append for POST's, as if the
hash was the form[field] = value. The routine should handle the url
encoding and the like.

Any luck?
(If not, anyone up for a quick few lines of Ruby ninjahood?)
 
A

ara.t.howard

Is there an equivalent in Ruby to the Perl modules which can "fill out
an html/http form"?

Given a hash, I'd like to either get the query string to append to the
URL for GET's, or the POST contents to append for POST's, as if the
hash was the form[field] = value. The routine should handle the url
encoding and the like.

Any luck?
(If not, anyone up for a quick few lines of Ruby ninjahood?)

harp:~ > cat a.rb
require "cgi"

class ::Hash
def query() map{|k,v| [CGI::escape(k), CGI::escape(v)].join "="}.join "&" end
end

query = { "foo" => "< > &", "bar" => "http://b/a space" }

puts query.query


harp:~ > ruby a.rb
foo=%3C+%3E+%26&bar=http%3A%2F%2Fb%2Fa+space


hth.

-a
 
K

Kev Jackson

harp:~ > cat a.rb
require "cgi"

class ::Hash
def query() map{|k,v| [CGI::escape(k), CGI::escape(v)].join
"="}.join "&" end
end

query = { "foo" => "< > &", "bar" => "http://b/a space" }

puts query.query


harp:~ > ruby a.rb
foo=%3C+%3E+%26&bar=http%3A%2F%2Fb%2Fa+space
6 minutes from post to solution - is that a record?

Kev
 
D

David Vallner

D=C5=88a Utorok 14 Febru=C3=A1r 2006 05:19 Kev Jackson nap=C3=ADsal:
harp:~ > cat a.rb
require "cgi"

class ::Hash
def query() map{|k,v| [CGI::escape(k), CGI::escape(v)].join
"=3D"}.join "&" end
end

query =3D { "foo" =3D> "< > &", "bar" =3D> "http://b/a space" }

puts query.query


harp:~ > ruby a.rb
foo=3D%3C+%3E+%26&bar=3Dhttp%3A%2F%2Fb%2Fa+space

6 minutes from post to solution - is that a record?

Kev

Ara's time machine is almost as good as Guido's time machine, it seems.

Oh, and someone apparently hacked up a Ruby WWW::Mechanize which is suppose=
d=20
to do this.

David Vallner
 
E

eastcoastcoder

Excellent, most appreciated.

Forgive my RFC ignorance, but is the same string appended to the URL
for GETs and sent as the bod for POST's? Or is there a difference in
the encoding somehow?
 
A

ara.t.howard

Excellent, most appreciated.

Forgive my RFC ignorance, but is the same string appended to the URL
for GETs and sent as the bod for POST's? Or is there a difference in
the encoding somehow?

it certainly is different. google around for more info. you'll need to use
something like http-access2 for posting form data. check out my rubyforge
script (on rubyforge no less) for a pretty complete example of posting
including file uploads and redirects.

regards.

-a
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top