HTTP GET requests in a Ruby CGI Script

Website: http://unchartedmess.com/plaingoogle
I’ve attached the script code and the website is listed above. I’m
working on a google frontend that filters the resulting html code from
the google searches and displays a list of plaintext links. This is for
some automation programs I am also working on.
Is there a way (or work-around) to run a Net::HTTP.get_response inside a
CGI script. I want to submit the form, which passes two variables to the
script. The script will run a google search, then sort and filter the
resulting code. Then it will display the page with the results. Any help
would be great, thanks.

Ben A. wrote:

Website: http://unchartedmess.com/plaingoogle
I’ve attached the script code and the website is listed above. I’m
working on a google frontend that filters the resulting html code from
the google searches and displays a list of plaintext links. This is for
some automation programs I am also working on.
Is there a way (or work-around) to run a Net::HTTP.get_response inside a
CGI script.

Sure. What problems are you having? The following ruby script works
for me using Apache as my web server:

#!/usr/bin/env ruby

require ‘net/http’
url = “http://www.google.com
resp = Net::HTTP.get_response(URI.parse(url) )

print “Content-type: text/html\n\n”
print “

The following is google’s web page:
\n”
print resp.body

I put that file in the directory:

/Library/Apache2/cgi-bin

Subsequently, I can call that ruby script using the url:

http://localhost/cgi-bin/ruby_test.rb

For instance, if I load this html page in my browser:

Ruby cgi test
click me

and then I click on the link, the ruby script executes and my browser
displays:

The following is google’s web page:

I think I’ve got it. I think it was a problem with calling a function
before sending the content-type bit. Darn. I had just worked up a php
script linked to a sql database. Oh-well. The ruby script was a lot more
flexible. Thanks!
Here’s another question: Is is it possible to dynamically generate a php
script? I would like to do the following:
Take their search terms,
Check if the terms are already in the database
if they are -> pull the results from the database and display them
otherwise, pull the results out of google, and put them in the database,
then display them.
Not sure if you could go from a ruby script to a php page and back
again. Generating the html content in the script is a b*tch.
Thanks again.

Ben A. wrote:

I think I’ve got it. I think it was a problem with calling a function
before sending the content-type bit. Darn. I had just worked up a php
script linked to a sql database. Oh-well. The ruby script was a lot more
flexible. Thanks!
Here’s another question: Is is it possible to dynamically generate a php
script?

Sure. php code is just text, and you can write text to a file in most
computer programming languages. Here is a ruby example:

File.open(“prog1.php”, “w”) do |file|
file.puts("<?php echo 'hello' ?>")
end

I would like to do the following:
Take their search terms,
Check if the terms are already in the database
if they are -> pull the results from the database and display them
otherwise, pull the results out of google, and put them in the database,
then display them.
Not sure if you could go from a ruby script to a php page and back
again.

I think you should be able to execute a php script doing something like
this:

system(“path1 path2”)

where path1 is the path to php and path2 is the path to prog1.php.

On Mar 29, 9:39 pm, Ben A. [email protected] wrote:

I think I’ve got it. I think it was a problem with calling a function
before sending the content-type bit. Darn.

Not sure about that, there were at least two errors in your script:
0.upto(results-1) { |i|
=> 0.upto(results.to_i-1) { |i|
and:
print tempbod.split(//).length-1
=> print body.split(//).length-1

Any error stop the script!

– Maurice

mdiam wrote:

On Mar 29, 9:39�pm, Ben A. [email protected] wrote:

I think I’ve got it. I think it was a problem with calling a function
before sending the content-type bit. Darn.

Not sure about that, there were at least two errors in your script:
0.upto(results-1) { |i|
=> 0.upto(results.to_i-1) { |i|
and:
print tempbod.split(//).length-1
=> print body.split(//).length-1

Any error stop the script!

– Maurice

The results is not a string, it’s an int. So thats not an error. The
second print statement has been removed so it’s not an issue.

Actually, when I look at the code again, I had changed the call to that
function from body = google(search, num) to body = google(search,
num.to_i)
Everything works, more or less. And you can find the current page at
http://www.googleplainly.com if you’re interested.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs