HTTP GET requests in a Ruby CGI Script

B

Ben Aroia

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.

Attachments:
http://www.ruby-forum.com/attachment/1640/ruby-test.rb
 
7

7stud --

Ben said:
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 "<div>The following is google's web page:</div>\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:

<html>
<head>
<title>Ruby cgi test</title>
</head>
<body>

<div>
<a href="http://localhost/cgi-bin/ruby_test.rb">click me</a>
</div>

</body>
</html>

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

The following is google's web page:
<google home page is displayed here>
 
B

Ben Aroia

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.
 
7

7stud --

Ben said:
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.
 
M

mdiam

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
 
B

Ben Aroia

mdiam said:
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.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top