Can't use variable from cgi with hpricot

I'm trying to get a url from a form and return the page title of said
url. I'm using cgi to get the web address from the url, which looks like
this:

http://somesite.com/result.rhtml?link=http%3A%2F%2Fpracticalpedal.com

The result page embedded ruby looks like this:

<%
require 'rubygems'
require 'cgi'
require 'open-uri'
require 'hpricot'

cgi = CGI.new

@linky = cgi['link'].to_str

@result = open(@linky)
@doc = Hpricot(@result)
@title = (@doc/"title").inner_html
@result.close

puts @title
%>

This works if I assign the @linky variable like so:

@linky = "http://practicalpedal.com"

But not when getting it with cgi. I'm very new at all of this, as is
probably obvious, but I can't figure this out. Any pointers in the right
direction would be appreciated.

···

--
Posted via http://www.ruby-forum.com/.

Wiley Davis wrote:

I'm trying to get a url from a form and return the page title of said
url. I'm using cgi to get the web address from the url, which looks like
this:

http://somesite.com/result.rhtml?link=http%3A%2F%2Fpracticalpedal.com

The result page embedded ruby looks like this:

<%
require 'rubygems'
require 'cgi'
require 'open-uri'
require 'hpricot'

cgi = CGI.new

@linky = cgi['link'].to_str

Disclaimer: I have never used embedded ruby before. I have used the CGI
lib, however, so here's my take.

cgi['link'] returns a string so there is no need for the TO_STR

Other than that, your code looks ok to me, so I would check your form
data. Something like the following should get recognized by cgi.

<FORM action="form.rb" method="put">
      <INPUT type="text" name="link">
      <INPUT type="submit" value="Submit">
</FORM>

CGI is a fickle little LIB, but she'll start to do your bidding once she
feels that you know what you are doing. Pay particular attention to the
<INPUT type="text" name ="link"> tag
If you don't specify NAME and use ID or some other thing, CGI wont read
your code right. At least that has been my experience.

If you just can't get CGI to read the form data, you can try to access
the data using ARGV as if the string was a command line variable to your
script.

I hope this helps you out. Good luck.

···

--
Posted via http://www.ruby-forum.com/\.

Wiley Davis wrote:

I'm trying to get a url from a form and return the page title of said
url. I'm using cgi to get the web address from the url, which looks like
this:

http%3A%2F%2Fpracticalpedal.com

@linky = "http://practicalpedal.com"

A cursory examination of those strings reveals that they are not
identical.

This works if I assign the @linky variable like so:
@linky = "http://practicalpedal.com"

...and what about:

@linky = "http%3A%2F%2Fpracticalpedal.com"

?? That's the string you are getting from cgi. You can test that by
writing @linky to a .txt file and then examining the contents of the
file.

require 'cgi'

str = "http://practicalpedal.com"
new_str = CGI.escape(str)
p new_str

--output:--
"http%3A%2F%2Fpracticalpedal.com"

Look familiar?

x = CGI.unescape(new_str)
p x

--output:--
"http://practicalpedal.com"

The rule: certain characters have to be 'transformed' in order to pass
them in a query string attached to the end of a url. That means the
special characters have to be 'untransformed' on the other side.

···

--
Posted via http://www.ruby-forum.com/\.

A cursory examination of those strings reveals that they are not
identical.

The rule: certain characters have to be 'transformed' in order to pass
them in a query string attached to the end of a url. That means the
special characters have to be 'untransformed' on the other side.

Thanks for pointing out the unescape, something I hadn't thought of.
Unfortunately, as I'm using it at least, that didn't fix the problem. It
works when I do this:

@linky = "http%3A%2F%2Fpracticalpedal.com"
@linky = CGI.unescape(@linky)
@result = open(@linky)
@doc = Hpricot(@result)
@title = (@doc/"title").inner_html
@result.close
puts @title

But it gives me a 500 Internal Server Error when I do this:

cgi = CGI.new
@linky = cgi['link']
@linky = CGI.unescape(@linky)
@result = open(@linky)
@doc = Hpricot(@result)
@title = (@doc/"title").inner_html
@result.close
puts @title

I'm pretty sure the @linky = cgi['link'] is working because when I
remove the hpricot part of the code and just display the variable with a
puts @linky, I get the correct string as output.

···

--
Posted via http://www.ruby-forum.com/\.

Other than that, your code looks ok to me, so I would check your form
data. Something like the following should get recognized by cgi.

Just to be sure, I checked the form and it looks okay.

CGI is a fickle little LIB, but she'll start to do your bidding once she
feels that you know what you are doing.

Darn... that'll be a while :slight_smile:

If you just can't get CGI to read the form data, you can try to access
the data using ARGV as if the string was a command line variable to your
script.

As noted below, cgi seems to be reading the string. I can read it and
display it with a:

puts @linky

but whenever i try to pass it along to hpricot, it gives me a server
error. Odd.

···

--
Posted via http://www.ruby-forum.com/\.

Wiley Davis wrote:

Other than that, your code looks ok to me, so I would check your form
data. Something like the following should get recognized by cgi.

Just to be sure, I checked the form and it looks okay.

CGI is a fickle little LIB, but she'll start to do your bidding once she
feels that you know what you are doing.

Darn... that'll be a while :slight_smile:

If you just can't get CGI to read the form data, you can try to access
the data using ARGV as if the string was a command line variable to your
script.

As noted below, cgi seems to be reading the string. I can read it and
display it with a:

puts @linky

but whenever i try to pass it along to hpricot, it gives me a server
error. Odd.

What's the server error?

I installed eruby, and I was able to configure eruby to work with my own
Apache2 install on mac osx 10.4.11,

(per the instructions in pickaxe2 p. 243 (a sym link didn't work, I
actually had to copy the file into cgi-bin))

and despite my earlier post, your code works fine for me using Apache
and localhost, although you can use open() with a block, and then you
don't have to manually close the file:

test.rhtml

···

---------

<html>
<head>
  <title>html page</title>
</head>
<body>
  <div>hi</div>
  <div>world</div>
<div>
<%
require 'rubygems'
require 'cgi'
require 'open-uri'
require 'hpricot'

cgi = CGI.new
linky = cgi['link']

open(linky) do |f|
    doc = Hpricot(f)
    title = doc/"title"
    puts title.inner_html
end

%>
</div>

</body>
</html>

--web browser displays:--
hi
world
pedalpower.com
--
Posted via http://www.ruby-forum.com/\.