Url parsing in ruby

J

John Ydil

Hello,

I use ruby to make some scriptings inside an html page.
I made a form to pass variable in an URL.
I need this variable in my ruby code, how can i get this variable ?

my URL looks like http://localhost/mypage.rbx?var=nas002001

I need the value after var=

Thanks for your help.

Ydil
 
R

Robert Klemme

2010/3/3 John Ydil said:
Hello,

I use ruby to make some scriptings inside an html page.
I made a form to pass variable in an URL.
I need this variable in my ruby code, how can i get this variable ?

my URL looks like http://localhost/mypage.rbx?var=nas002001

I need the value after var=

Thanks for your help.

Here's one way to do it:

$ irb19 -r uri
irb(main):001:0> u = URI.parse "http://localhost/mypage.rbx?var=nas002001"
=> #<URI::HTTP:0x1029adb0 URL:http://localhost/mypage.rbx?var=nas002001>
irb(main):002:0> v = u.query[/(?<=\A|&)var=([^&]*)/, 1]
=> "nas002001"

Note, error checking is missing from this example and it's probably
also not very robust.

Cheers

robert
 
R

Robert Klemme

2010/3/4 John Ydil said:
I knew URI.parse but to use it, you need to know the URL.
In my case, i need to parse the URL in the web browser,in the address
bar, not a given URL.
Or maybe there's something a don't understand about how to use it.

Maybe we should start over with you describing what you are trying to
do. Your initial posting read like "I have an URL and need to extract
a particular part of it". That's what Brian and I have answered. I'm
afraid, you'll probably have to provide more context or explain where
exactly you are stuck.

Kind regards

robert
 
J

John Ydil

Maybe we should start over with you describing what you are trying to
do. Your initial posting read like "I have an URL and need to extract
a particular part of it". That's what Brian and I have answered. I'm
afraid, you'll probably have to provide more context or explain where
exactly you are stuck.

Kind regards

robert

Ok,I'm coding a web page with Ruby.
On the first page I have a form which send var to another page. This var
is send in the URL. (http://localhost/mypage.rbx?itf_name=nas001001)

So on my second page i need to check the URL of this page to retrieve
the variable in order to use it in my script. The var in my exemple is
nas001001.

Sorry if i wasn't clear, i hope it's better now.

Thanks again
 
J

John Ydil

Robert said:
And how do you interface with the web server? If you are using Cgi you
can use
http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/index.html

Kind regards

robert

I think this lib is for generate html, I didn't see method to get the
URL

How it works :
I told my web server (apache) to use ruby (.rbx extension)
In my web page, I use ruby to write html which is interpreted by apache.
It looks like :
mywebpage.rbx

#!/usr/bin/env ruby

print "Content-type: text/html\r\n\r\n"
require 'rubygems'
require 'net/ssh'

HOST = '192.168.0.121'
USER = 'root'
PASS = 'xxxx'

Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
result = ssh.exec!('/root/checknas.rb --list')
print "<html><body><title> Info </title>"
print "{html stuff}"
print "</body></html>"
end


Maybe I don't have the good way of doing this but at least, it works, my
web pages are displayed.
 
R

Robert Klemme

I think this lib is for generate html, I didn't see method to get the
URL

This is not true. Please read the documentation I referred you to.
How it works :
I told my web server (apache) to use ruby (.rbx extension)

Barring any other information I have to assume that this is the CGI interfa=
ce.
In my web page, I use ruby to write html which is interpreted by apache.
It looks like :
mywebpage.rbx

#!/usr/bin/env ruby

print "Content-type: text/html\r\n\r\n"
require 'rubygems'
require 'net/ssh'

HOST =3D '192.168.0.121'
USER =3D 'root'
PASS =3D 'xxxx'

Net::SSH.start( HOST, USER, :password =3D> PASS ) do|ssh|
=C2=A0result =3D ssh.exec!('/root/checknas.rb --list')
print "<html><body><title> Info </title>"
print "{html stuff}"
print "</body></html>"
end


Maybe I don't have the good way of doing this but at least, it works, my
web pages are displayed.

Class CGI helps with creating valid HTML as well so I suggest you use
it. It's just the proper tool for the job.

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

John Ydil

Robert said:
This is not true. Please read the documentation I referred you to.


Barring any other information I have to assume that this is the CGI
interface.


Class CGI helps with creating valid HTML as well so I suggest you use
it. It's just the proper tool for the job.

Kind regards

robert

I did it !

require "cgi"


cgi_request = CGI::new("html4")

puts "Content-Type: text/html; charset=UTF-8"
puts

itf = cgi_request['itf_name']
puts itf

Thanks for your help

Regards

John
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top